osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld
2014-06-13 00:14
459 查看
环境准备:
jdk版本
jdk:1.7karaf:
版本:apache-karaf-3.0.1
下载地址:
http://pan.baidu.com/s/1qWM4Y1uhttp://karaf.apache.org/
配置本地仓库:
参考:http://blog.csdn.net/wobendiankun/article/details/25333113启动karaf:
karaf_home/bin/karaf.bat启动成功如下:
安装 mvn-hello-provider 到本地仓库
hello.javapackage 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-SNAPSHOT
bundle:install -s mvn:com.demo.hello/mvn-hello-consumer/0.0.1-SNAPSHOT
运行结果:
源代码下载
相关文章推荐
- osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld
- osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo
- osgi实战学习之路:4.Bundle
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- osgi实战学习之路:8. Service-3之ServiceTracker
- 【SSM学习之路】使用Nexus搭建Maven私服
- OSGI学习之路之Require-Bundle与Imported-Package
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- osgi实战学习之路:6. Service-1
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- osgi实战学习之路:8. Service-3之ServiceTracker
- 基于maven+OSGi+karaf-REST环境搭建
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
- OSGi实战(1)使用Maven创建Bundle
- SpringBoot实战学习(一) Maven安装、Spring搭建项目
- maven,OSGI,karaf 学习笔记
- 【SSM学习之路】Maven安装配置和环境搭建(eclipse)
- osgi实战学习之路:3. osgi分层概念及相互合作demo