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://eureka.ykyuen.info/2010/03/12/maven-create-a-osgi-bundle-using-maven-bundle-plugin/