首页 > 代码库 > 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.java

package 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


运行结果:







源代码下载