logo.pngThis is the second step in a series of items about exploring the felix osgi container and some sidesteps to make life easier while developing osgi bundles. You can find the first step here: https://www.gridshore.nl/2008/02/10/starting-with-osgi-using-apache-felix-step-1/

This is so easy, I do not want to spend to much time here. There is a special maven 2 plugin to create a “bundle”, check out the following page that describes the plugin : maven-bundle-plugin.

Create a new maven project using the most basic archetype.

mvn archetype:create -DgroupId=<your.groupid> -DartifactId=<your.artifactId>

Change the packaging of the pom to be "bundle". Add a dependency to the core and configure the plug in. The possible parameters are described extensively at the mentioned web page. I’ll explain the what and why of the code below, not the theory. The following pom file is the pom of the project example-client in the FelixTryout project on my google code page. As you can see there is a dependency on the training-service and on jetty. i am not going to talk about jetty here. More on that in one of the next steps.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.gridshore.samples.bundles</groupId>
    <artifactId>example-client</artifactId>
    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>example-client</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>${pom.groupId}</groupId>
            <artifactId>training-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>6.1.7</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>1.2.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            nl.gridshore.samples.bundles.exampleclient.api
                        </Export-Package>
                        <Private-Package>
                            nl.gridshore.samples.bundles.exampleclient.impl
                        </Private-Package>
                        <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                        <Bundle-Activator>
                            nl.gridshore.samples.bundles.exampleclient.impl.Activator
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The export-package contains the packages that are exposed to other bundles. This results in the following manifest file:

Manifest-Version: 1.0
Built-By: jettro
Created-By: Apache Maven Bundle Plugin
Bundle-Activator: nl.gridshore.samples.bundles.exampleclient.impl.Acti
 vator
Import-Package: javax.servlet;version="2.5",javax.servlet.http;version
 ="2.5",nl.gridshore.samples.bundles.trainingservice.api,org.mortbay.j
 etty;version="6.1",org.mortbay.jetty.servlet;version="6.1",org.osgi.f
 ramework;version="1.3"
Bnd-LastModified: 1202823580612
Bundle-Version: 1.0.0.SNAPSHOT
Bundle-Name: example-client
Build-Jdk: 1.5.0_13
Private-Package: nl.gridshore.samples.bundles.exampleclient.impl
Bundle-ManifestVersion: 2
Bundle-SymbolicName: example-client
Tool: Bnd-0.0.227

Most of the items are pretty obvious if you look at the maven configuration. There is one thing that is more advanced. Have a look at the import-Package, this contains the packages that are imported by the classes. This is done automatically by the bundle plugin. You can prevent imports by configuring the "ImportPackage" and use the negative import.

<Import-Package>!org.foo.impl</Import-Package>

That is about it, mvn clean install and your plugin is installed into the maven repository. Well, of course you do need to implement some java code. Just like I said before, you can find the implementation on google code. I will also introduce the concepts in the forthcoming steps or posts.

Using maven to create an osgi bundle (osgi felix sample step 2)
Tagged on:             

2 thoughts on “Using maven to create an osgi bundle (osgi felix sample step 2)

  • July 29, 2009 at 5:52 pm
    Permalink

    Not sure anymore, it has been a while. But there are multiple resources available. I know felix has it’s own repository, spring source has one. And lot’s of other provide bundles.

  • July 29, 2009 at 3:52 pm
    Permalink

    Hi,
    Could you let me know which repositories (repository and/or pluginRepository) do you use, please?

    T.

Comments are closed.