1. Beginner - Creating wrapper OSGi bundles

{scrollbar}

Most of the time, you have existing JAR file (build by yourself or provided).

To be able to use the ServiceMix Kernel features, you need to wrap this JAR file on an OSGi bundle format.

It's easily possible using Apache Felix Bundle Maven plugin and BND tool.

First of all, create your OSGi bundle pom.xml :

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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>myLib-osgi</artifactId> <name>MyLib :: Bundles :: ${pom.artifactId}</name> <description> ${pom.artifactId} wrapper OSGi bundle. </description> <version>1.0-SNAPSHOT</version> <packaging>bundle</packaging> <!-- Dependencies OSGi bundle version --> <properties> <commons-codec-version>1.3</commons-codec-version> <commons-collections-version>3.1</commons-collections-version> <commons-digester-version>1.6</commons-digester-version> </properties> <dependencies> <dependency> <groupId>mylib</groupId> <artifactId>mylib</artifactId> <version>${pom.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> <Export-Package> my.lib.package.*;version=${pom.version} </Export-Package> <Import-Package> javax.*, org.apache.commons.codec.*;version=${pom.commons-codec-version}, org.apache.commons.collections.*;version=${pom.commons-collections-version}, org.apache.commons.digester.*;version=${pom.commons-digester-version}, org.apache.poi.*;version=${pom.poi-version}, org.apache.xerces.*;version=${pom.xerces-version}, com.enterprisedt.net.ftp;version=${pom.edtftpj-version}, fr.dyade.koala.xml.koml;version=${pom.koml-version}, org.jconfig.*;version=${pom.jconfig-version}, socks.*;version=${pom.socks-version}, cz.dhl.*;version=${pom.jvftplib-version}, org.apache.log4j, org.apache.xml.serialize, org.apache.xpath, org.xml.sax, org.w3c.dom, sun.misc;resolution:=optional </Import-Package> </instructions> </configuration> </plugin> </plugins> </build> </project>

You just need to call mvn package and your OSGi bundle is ready.

But you can see that the bundle required a lot of others bundles to work (like org.apache.commons, etc).

If you want to create a "standalone" bundle which embed all required others libraries, you can use <Embed-Dependency/> like this :

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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>myLib-osgi</artifactId> <name>MyLib :: Bundles :: ${pom.artifactId}</name> <description> ${pom.artifactId} wrapper OSGi bundle. </description> <version>1.0-SNAPSHOT</version> <packaging>bundle</packaging> <dependencies> <dependency> <groupId>mylib</groupId> <artifactId>mylib</artifactId> <version>${pom.version}</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> <Export-Package> my.lib.package.*;version=${pom.version} </Export-Package> <Import-Package> javax.*, </Import-Package> <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> </instructions> </configuration> </plugin> </plugins> </build> </project> {scrollbar}
  • No labels