OSGi quick start

OSGi has been looking promising for a long time, but I have felt that getting started have been really complicated and deploying dependencies have been messy, in particular for transitive dependencies. While I was looking into deployment options for Apache Camel I realized that a lot has happened with OSGi since last I looked. In particular Apache Karaf really simplifies OSGi deployment with two useful provisioning features:

  • Maven URL handler that allows deployment directly from a Maven repository
  • Features that allow grouping of OSGi bundles

Although these features are not part of the OSGi standard, it at least makes my life easier and I believe that something similar will become part of the standard since Spring DM and Apache Aries also have similar concepts. I’ll just show how to create a blank OSGi project and deploy in Karaf:

mvn org.ops4j:maven-pax-plugin:create-bundle -Dpackage=com.jayway.osgi.helloworld
cd com.jayway.osgi.helloworld/
mvn clean install

Now you need to download Karaf, unzip and then:

osgi:install --start mvn:com.jayway/com.jayway.osgi.helloworld/1.0-SNAPSHOT

The output will be (it is not a completely blank project!):

STARTING com.jayway.osgi.helloworld
REGISTER com.jayway.osgi.helloworld.ExampleService

To generate an Eclipse project:

mvn org.ops4j:maven-pax-plugin:eclipse

Some links:

Leave a Reply