Wrap Java Libraries as OSGi Bundles with Maven

If a 3rd party library isn't supplied as an OSGi bundle you need to do some extra steps as OSGi developer.

In this example we are wrapping the following libraries from the Spring Data ecosystem:

The parent project uses Tycho.

Tycho - Building Eclipse plug-ins with maven

We started from the eclipse/tycho-demo and developed our pom-first Maven build for wrapping the 3rd-party libraries.

The demo utilizes Apache Felix Maven Bundle Plugin (BND).

Step 1: Declare your dependency to the plain library:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.1.9.RELEASE</version>
</dependency>

Step 2: Tune the OSGi bundle generation via the following configuration:

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
    <instructions>
        <Embed-Dependency>spring-data-commons;scope=compile|runtime</Embed-Dependency>
        <Import-Package>
            com.fasterxml.jackson.*;resolution:="optional",
            com.google.common.base;resolution:="optional",
...
            *
        </Import-Package>
        <Export-Package>*</Export-Package>
        </instructions>
</configuration>
</plugin>

In a future post, we'll flesh out the example and show how to use Spring Data JDBC in an OSGi environment. Stay tuned...


Header cover Photo by Ms Jemini