使用maven开发OSGI样例
一:创建maven项目,在pom.xml里面增加例如以下依赖
<dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi</artifactId> <version>3.0.0</version> <type>jar</type> </dependency>
com.lala.Activator.java为
package com.lala; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("-----------start----------"); } public void stop(BundleContext context) throws Exception { System.out.println("-----------stop----------"); } }
二:配置插件
方法1:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifestEntries> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId} </Bundle-SymbolicName> <Bundle-Version>${project.version}</Bundle-Version> <Bundle-Vendor>${project.groupId}</Bundle-Vendor> <Bundle-Activator>com.lala.Activator</Bundle-Activator> <Export-Package> com.lala.api </Export-Package> <Import-Package> org.osgi.framework </Import-Package> </manifestEntries> </archive> </configuration> </plugin>
然后。运行mvn package,就可以生成含有MANIFEST.MF文件的jar
方法2:
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.5.4</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.ArtifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.ArtifactId}</Bundle-SymbolicName> <Bundle-Version>${project.version}</Bundle-Version> <Bundle-Vendor>${project.groupId}</Bundle-Vendor> <Export-Package>com.lala.api</Export-Package> <Bundle-Activator>com.lala.Activator</Bundle-Activator> <Import-Package>org.osgi.framework</Import-Package> </instructions> </configuration> </plugin>
mvn clean org.apache.felix:maven-bundle-plugin:bundle , 就可以生成含有MANIFEST.MF文件的jar
或者把package声明为bundle
如:<packaging>bundle</packaging>
然后。就能够直接使用mvn clean package