Java APIs to create OSGi related artifacts

This is all about creating OSGi related artifacts like Bundles, Fragments and Deployment Packages with Java Api. It is very convinient to create such artifacts on-the-fly inside Tests (like in Pax Exam).
On the other hand, this library can be a foundation of real end user tools that need to create those artifacts.


Add this to your pom:


All artifacts are available on Maven Central for general use.


Sources are on Github at https://github.com/ops4j/org.ops4j.pax.tinybundles


Please report issues in the PaxTinyBundles Jira project.

Create Bundles

To get full benefit of the fluent api, you should include

import static org.ops4j.pax.tinybundles.core.TinyBundles.*;

in your import section.

With this small library you can create small bundles like so:

InputStream inp = bundle()
            .activator( MyFirstActivator.class )
            .add( HelloWorld.class )
            .add( HelloWorldImpl.class )
            .symbolicName( "mybundle" )
            .set( Constants.EXPORT_PACKAGE, "demo" )
            .set( Constants.IMPORT_PACKAGE, "demo" )
            .build( );

Have a look at https://github.com/ops4j/org.ops4j.pax.tinybundles/blob/master/src/test/java/org/ops4j/pax/tinybundles/Examples.java to see a complete list of use case examples.

We found this quite useful when for example testing extender-bundles like PaxWeb or SpringDM.
As you see, resources are pulled from "current classpath" which means that you can easily construct stuff like you do in ordinary java apps and pull them together using a fluent api like the above.
It is also trivial to create thounsands of (different) bundles to test high load for example.

Create Bundles using bnd builder

The bnd builder allows to automatically generate the imports of a bundle. Since version 3.0.0 it also automatically processes declarative services annotations. So your bundles can easily consume and offer OSGi services without manually writing an Activator. 

There is a small caveat though when using the bnd builder in pax exam you can easily get a ClassNotFoundException with the BuilderStrategy interface. To avoid this put the code that generates the bundle into a separate class and make the method static.

bnd builder and DS annotations
bundle( ).add(DsService.class).build( withBnd() )
public class DsService
    public void activate() {

Create Fragments

Basically same as Bundles with just the "Bundle-Host" Header added in "set" section.