Bundles, Bnd, PAX, Maven and More

OSGi à la Carte


A web search on OSGi will yield a bewildering number of open source projects. Beyond the familiar Equinox and Felix, as OSGi moves into the Enterprise Java space the number of OSGi related projects is increasing. If you are interested in OSGi, many of these projects will be useful to you but it may not be immediately obvious how. This article hopes to alleviate this shortcoming with a whirlwind tour through the most useful open source tools the authors have encountered.

OSGi as a technology has been around since the late '90s, originally in the embedded space. More widespread interest has arisen after Eclipse adopted OSGi as the integration and plugin technology underlying the Eclipse IDE. This move was followed by the adoption of OSGi as the modularization technology at the heart of most JEE application servers. Even then OSGi was much rarer in the space of individual applications. Only with the advent of a dedicated enterprise OSGi programming model with Spring dmServer have we seen widespread interest in OSGi.

Historically, OSGi has often been perceived as a hard technology to get started with – especially since OSGi certainly is an invasive technology. Correctly using OSGi and its modularity model requires discipline and forethought from developers and framework authors. Especially assumptions made concerning classloaders often no longer hold in an OSGi environment. However, the benefits of using OSGi for modularization as well as its inherent support for dynamics far outweighs the initial challenges in using it.

This article hopes to remove some of the biggest hurdles of using OSGi by introducing an appropriate set of tools, many of which are of recent vintage. The first set of tools is intended to help with the task of developing bundles. Next, we look at the tools that facilitate integration (i.e. in-container) testing with OSGi. Finally, we have a look at deployment and distribution options.

It is assumed that the reader has some familiarity with basic OSGi concepts like bundles, bundle manifests, services and OSGi lifecycle. Readers completely new to OSGi are referred to introductory resources such as Neil Bartlett's "OSGi in practice" as well as the OSGi home page.

Developing bundles

At the very simplest level an OSGi bundle is just a traditional JAR file whose manifest has some additional headers that define the identity of the bundles as well as its (package) capabilities and requirements – its imports and exports. However, the often cited bold claim "No more NoClassDefFoundErrors with OSGi" relies on correct metadata. A missing import for a needed package can easily mean that OSGi at the very first usage actually introduces more missing classes rather than less.

So at the minimum tools for OSGi development should help the developer to produce correct metadata and well-factored bundles. Beyond that development tooling should help with test running the bundles and ideally have support for the OSGi programming models such as the component models, Web Application Bundles, persistence bundles and the like.




Eclipse PDE and Libra


Perhaps the most well-known tooling for OSGi is the Eclipse Plugin Development environment, which comes in many common distributions of the Eclipse IDE (for example the distribution for JEE development). It provides all the necessities of OSGi development such as a content aware bundle manifest editor and a compiler plugin that enforces OSGi visibility rules (unless side-stepped by a custom build path) and reports errors for missing imports. On top of that a collection of bundles can be launched directly from the IDE via an OSGi framework definition. PDE’s greatest asset is also its greatest weakness: explicitness. A developer needs to carefully craft the OSGi manifest and update it whenever the code changes.


On the other hand, the resulting manifest will never contain any surprises. Also, some of the default suggestions regarding version ranges and the usage of Require-Bundle do not conform to widely accepted OSGi best practices.



For developing more enterprise focussed OSGi applications, for example using JPA end Servlet functionality in bundles, Eclipse Libra promises to be an interesting option. Loosely related to IBM's free (but not open sourced) OSGi development tools this project aims to integrate existing high quality Eclipse tools for JEE development with OSGi specific functionality as defined in the corresponding chapters of the OSGi Enterprise Specification.


Bnd and BndTools

While PDE’s explicit approach to authoring manifests certainly works, there is another more convenient option, which is to let most of the hard stuff (particularly the Import-Package) be generated.


After all, the source code, as well as the byte code, have most of the information needed to determine dependencies. Peter Kriens’ Bnd tool is built on that premise that most of the manifest can be described in a terse domain specific language (DSL) and the rest can be filled in at the time when the bundle is assembled by introspecting the classes. With this approach it also becomes trivial to convert third-party JARs into functional bundles (assuming they do not make assumptions about classloaders in the code,). Bnd also honors OSGi best practices such as generating uses directives for exported packages.


More than for just converting JAR files, the Bnd tool provides a holistic solution for bundle development, supporting complex bundle assemblies, which pull classes from multiple sources, bundle verification as well as launching a framework for running an application or running integration tests. The invocations below show very basic usage for converting a JAR to a bundle (Listing 1).



Although the above example shows Bnd as a stand-alone command line tool, it can be integrated into Ant or Maven builds. Neil Bartlett has even created a complete Eclipse development environment, BndTools, based on Bnd. The complete package includes very capable editors for defining bundles (even several for a single Eclipse project), support for running a collection of bundles and even hot swapping code changes into the running framework. On top of that there are prebuilt templates for defining Declarative services components (Figure 1) and integration tests.



Maven with Felix Bundle plugin and Pax Construct


The above projects are IDE centric or build agnostic, like Bnd. Some readers may be looking for a very mature build solution, especially when confronted with large OSGi applications. This can be found in the Maven build infrastructure that is used across almost all Apache OSGi projects. Maven as a build choice for OSGi projects offers significant freedom for choosing an IDE as all major IDEs feature Maven plugins. Also Maven’s dependency management aligns advantageously with OSGi concepts of modularity. Maven itself does not come with direct support for OSGi packaging, however the Apache Felix Bundle plugin adds that capability and is widely used across Apache projects. The Felix bundle plugin uses Bnd under the covers, so the configuration options for defining bundles are the same as for using Bnd directly or through the BndTools.


Despite many advantages of Maven, setting up the right infrastructure for OSGi is a time-consuming process, which especially to a Maven novice can seem daunting. Fortunately, the Pax Construct project provides a very useful starter kit. It helps to create Maven 2 OSGi projects and define individual bundles. Pax Construct also has excellent support for launching a test OSGi environment from Maven.




Valentin Mahrwald
Valentin Mahrwald
Holly Cummins
Holly Cummins

What do you think?

JAX Magazine - 2014 - 03 Exclucively for iPad users JAX Magazine on Android


Latest opinions