OSGi example in Eclipse 4.3 for JBoss 7

Do you have any experiences with OSGi? Do you know how to create OSGi bundle in Eclipse? Do you know how to start JBoss AS with OSGi support? You will know now ;)

Eclipse configuration

We need to configure Eclipse first. In my case it is Eclipse 4.3. You need to configure OSGi Framework under Preferences > Plug-in Development > Target Platform. Currently all plug-ins are included and it can cause many exceptions. So select (Active) platform, click Edit.

Click Content tab and select only these plug-ins:
  • org.eclipse.osgi
  • org.eclipse.equinox.console
  • org.apache.felix.gogo.runtime
  • org.apache.felix.gogo.shell

Creating bundle

Now we need to create our bundle (plug-in) under New > Plug-in Project like this:

Choose "Hello OSGi Bundle" as template. Your project will contain one simple activator which writes some message to System.out in case of start and stop of bundle.

Start OSGi framework in Eclipse

It is time to test our bundle. Right click on MANIFEST.MF file and choose Run As > OSGi Framework. If everything is OK, you should see start message from activator in console.

Try ss command to list all bundles:

We can try to export your new bundle to jar file. Righ click on bundle-test project, Export > Plug-in Development > Deployable plug-ins and fragments, choose your directory where jar will be created. Done.

Deploy bundle to JBoss AS

Start your JBoss 7 like: standalone.bat -c standalone-osgi.xml and copy your bundle jar into deployment directory. Quite simple, isn't it.

If you need Maven in your project, you can do it. Here is example of pom.xml:

<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/xsd/maven-4.0.0.xsd">


One note to Maven, you need to remove MANIFEST.MF file and change directory structure ;).

Hopefully it will help you to begin with OSGi in Eclipse. In case of any question ask please.
▼ Click here to say thanks ▼

1 comment :

  1. Thank you for the explanation! I looked for this information hours and I tried to do a lot of things but nothing worked. On your post I can always find what I need. Thank you so much !
    digital signature