Friday, March 23, 2012

Making an OSGi bundle out of a third party jar.

Apache Karaf 2.2.6 will contain a new deployer demo which illustrates making an OSGi bundle out of a third party jar.

The sample provided is not the only way to make a third party jar into a bundle, however, the methodology shown provides a developer a lot of control over the bundle produced while not having to perform all actions by hand.

The demo takes the Apache commons-lang jar and wraps it as an OSGi bundle. To do this a POM file is created that picks up the commons-lang jar, shades it, then packages it into a bundle. The shading and packaging into an OSGi bundle steps are performed by configuring the Shade and Bundle plugins.

The Shade plugin provides the capability to package an artifact in a special jar, including its dependencies and shade (rename) the packages of some of the dependencies. To achieve fine grained control over which classes from the selected dependencies are included, artifact filters can be used. It's beyond the scope of this post, or the Karaf documentation, to discuss how filters can be setup, more information can be found on the Maven Shade plugin example here

The Bundle plugin wraps the BND tool from Peter Kriens, providing Maven a convenient way to produce a bundle by describing its content. A developer can configure the plugin to set the bundles imports and exports, among other bundle properties. Again, it's beyond the scope of this post, or the Karaf documentation, to discuss in depth all the features of the bundle plugin. For more information please visit

The primary take away from the deployer wrap demo is to illustrate a repeatable process for wrapping your third party jars;

  1. Define target jars to package into bundle.
  2. Shade jars (renaming packages as needed).
  3. Build bundle by describing properties.
The final step is to deploy the newly wrapped bundle into the OSGi framework and test out your configuration. With careful tuning most of your third party jars should be convertible into OSGi bundles - this being said, some jars may require even more special care to operate safely in an OSGi environment. In these cases it would be advisable to request the provider of the third party jar to produce an OSGi bundle.

No comments: