首页 > 代码库 > osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld
osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld
环境准备:
jdk版本
jdk:1.7
karaf:
版本:apache-karaf-3.0.1
下载地址:
http://pan.baidu.com/s/1qWM4Y1u
http://karaf.apache.org/
配置本地仓库:
参考:http://blog.csdn.net/wobendiankun/article/details/25333113
启动karaf:
karaf_home/bin/karaf.bat
启动成功如下:
安装 mvn-hello-provider 到本地仓库
hello.java
package com.demo.hello; public class Hello { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Hello() { this.name="jack"; } public void sayHi(){ System.out.println("hi:\t"+this.name); } }
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.hello</groupId> <artifactId>mvn-hello-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>bundle</packaging> <name>mvn-hello-provider</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>4.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package>com.demo.hello</Export-Package> </instructions> </configuration> </plugin> </plugins> </build> </project>
运行命令:
mvn clean install
安装 mvn-hello-consumer 到本地仓库
HelloClientActivator.javapackage com.demo.hello.activator; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import com.demo.hello.Hello; public class HelloClientActivator implements BundleActivator{ Hello hello; public void start(BundleContext context) throws Exception { hello=new Hello(); hello.sayHi(); } public void stop(BundleContext context) throws Exception { hello=null; } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.hello</groupId> <artifactId>mvn-hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>bundle</packaging> <name>mvn-hello-consumer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>com.demo.hello</groupId> <artifactId>mvn-hello-provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package>com.demo.hello.client</Export-Package> <Private-Package>com.demo.hello.*</Private-Package> <Import-Package>com.demo.hello, org.osgi.framework </Import-Package> <Bundle-Activator>com.demo.hello.activator.HelloClientActivator</Bundle-Activator> </instructions> </configuration> </plugin> </plugins> </build> </project>运行命令:
mvn clean install
部署bundle到karaf
注意部署顺序
先部署,mvn-hello-provider,再mvn-hello-consumer
在karaf命令行中运行命令:
bundle:install -s mvn:com.demo.hello/mvn-hello-provider/0.0.1-SNAPSHOTbundle:install -s mvn:com.demo.hello/mvn-hello-consumer/0.0.1-SNAPSHOT
运行结果:
源代码下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。