OSGi实战(1)使用Maven创建Bundle

1、maven插件

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>1.0</version>
	<packaging>bundle</packaging>
	<name>...</name>
	<description>...</description>

	...

	<dependencies>
		<dependency>
			<groupId>org.apache.felix</groupId>
			<artifactId>org.apache.felix.framework</artifactId>
			<version>2.0.4</version>
		</dependency>	
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
						<Bundle-Name>Service listener example</Bundle-Name>
						<Bundle-Description>A bundle that displays messages at startup and when service events occur</Bundle-Description>
						<Bundle-Vendor>Apache Felix</Bundle-Vendor>
						<Bundle-Version>1.0.0</Bundle-Version>
						<Bundle-Activator>tutorial.example1.HelloWorldActivator</Bundle-Activator>
						<Export-Package>tutorial.example1.api</Export-Package>
						<Private-Package>tutorial.example1.*</Private-Package>
						<Import-Package>org.osgi.framework</Import-Package>
					</instructions>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

2、编写测试Activator

public class HelloWorldActivator implements BundleActivator {

    private static BundleContext context;

    static BundleContext getContext() {
	return context;
    }
 
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        HelloWorldActivator.context = bundleContext;
	System.out.println('Hello World!');
    }
 
    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        HelloWorldActivator.context = null;
    }
}

3、打包测试

1)执行maven打包命令

mvn package
或在Eclipse中,右击maven工程 -> Run As -> Maven package。

打包生成OSGi Bundle example-1.0.jar。


2)在felix中安装测试

wichtounet@Linux-Desktop:~/Desktop/osgi/felix$ java -jar bin/felix.jar
_______________
Welcome to Apache Felix Gogo

g! install file:../example-1.0.jar
Bundle ID: 17
g! start 17
Hello World!
stop 17
g! 
启动Bundle以后,控制台显示“Hello World!”,说明安装启动成功。

参考:

http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html

http://www.javacodegeeks.com/2011/11/osgi-and-spring-dynamic-modules-simple.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+JavaCodeGeeks+%28Java+Code+Geeks%29&utm_content=Google+Reader

http://eureka.ykyuen.info/2010/03/12/maven-create-a-osgi-bundle-using-maven-bundle-plugin/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值