How to make an OSGI bundle using maven bundle plugin

OSGI (Open systems Gateway Initiative) specification defines a complete and dynamic component model which can be remotely installed, started, updated, stopped and uninstalled without restarting JVM. I'm not going to discuss the specification details of OSGI. You can find a lot of information from
The objective of this post is to create a simple OSGI bundle using Apache Felix maven bundle plugin.

Apache Maven2

Step 1
First we need to create a simple java class and which must be included in a proper directory structure therefore we can use Maven2 to build the source.

Create the following directory structure in your file system.

Create the following interface.

package org.wso2;

public interface GreetingService {
public void sayGreeting(String s);

Create an Impl directory under wso2 and add the following class in there.

package org.wso2.Impl;

import org.wso2.GreetingService;

public class GreetingServiceImpl implements GreetingService {

public void sayGreeting(String s){
System.out.println("Welcome " +s);


Step 2

We have created an interface and its implementation. We have not done anything new for you so far.
Since we are going to create an OSGI bundle, we need to add a BundleActivator though it is not mandatory to have. Bundle Activator class is used to start and stop the bundle when it is installed on an OSGI framework such as Knopflerfish.
Lets create an implementation of BundleActivator at the same package where GreetingServiceImpl is placed.

package org.wso2.Impl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.wso2.Impl.GreetingServiceImpl;

public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
GreetingServiceImpl greetingServiceImpl = new GreetingServiceImpl();


public void stop(BundleContext bundleContext) throws Exception {


Now we can create the pom.xml to compile above classes and make an OSGI bundle (Basically we need to generate manifest headers in MANIFEST.MF of the generated bundle jar)
Here is the complete pom.xml. Note the highlighted section.

<project xmlns=""




<name>Simple OSGI example</name>
<description>A bundle to demonstrate maven bundle plugin.</description>



The <Export-Package> element tells the plugin which of the available packages to copy into the bundle and export. The <Private-Package> instruction indicates which of the available packages to copy into the bundle but not export. In other words, private packages are not exported by the bundle.
Here, we define our impl package, org.wso2.Impl as a private package.
These elements will be added as manifest headers in resulting bundle jar as we will see shortly.

Step 4
Save pom.xml at the root directory of the above folder tree. (e.g:- example directory) and run it with mvn clean install

Bundle will be created at the target directory. Open it and look at META-INF/MANIFEST.MF

Manifest-Version: 1.0

Built-By: Charitha

Created-By: Apache Maven Bundle Plugin

Bundle-Activator: org.wso2.Impl.Activator

Import-Package: org.osgi.framework;version="1.3",org.wso2

Bnd-LastModified: 1223869521406

Export-Package: org.wso2

Bundle-Version: 1.0

Bundle-Name: Simple OSGI example

Bundle-Description: A bundle to demonstrate maven bundle plugin.

Build-Jdk: 1.5.0_14

Private-Package: org.wso2.Impl

Bundle-ManifestVersion: 2

Bundle-SymbolicName: org.wso2

Tool: Bnd-0.0.238

Have a look at the Export-Package and Private-Package headers which were added to the manifest as we defined them in pom.xml.
Now our bundle is ready to deploy in an OSGI framework. We will see how this bundle can be deployed on Knopflerfish in a future blog post.


Unknown said…
Thanks Charitha. This is very useful. I am planning to use Knopflerfish and Maven2.... can the maven bundle plugin be easily modified for Knopflerfish? Are you planning to write your post on this soon?
Anonymous said…
Thank you so much!!polo shirt men'ssweate,Burberry Polo Shirts lacoste sweater, ralph lauren Columbia Jackets,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping experience on can find the father who desire fashionable, intellectual mens clothing simultaneously.
Anonymous said…
Perfect!!You are a outstanding person!Have you ever wore chaussures puma,Here are the most popular puma CAT,Puma shoes store gives some preview of puma speed cat,and casual but no sweat puma basket.

Popular posts from this blog

Common mistakes to avoid in WSO2 ESB - 1 - "org.apache.axis2.AxisFault: The system cannot infer the transport information from the URL"

Working with HTTP multipart requests in soapUI

WS-Reliable messaging with WSO2 WSAS -1