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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章