exploring osgi, maven

I’ve started writing, hopefully a series of blogs about actually building a virgo app. Problem with greenpages is that nowhere are you shown how to roll your own.

So what I aim to do accomplish here is

  • a simple hello world app
  • built in eclipse w/ m2eclipse, sts
  • deployed from eclipse to virgo

Later on what I am most curious about is

  • managing online upgrades to existing deployment (as in, have a single deployment running at all times, do incremental upgrades on it)
  • eventually managing the deployment time user friendly
    • building safeguards against simulated application states (views) where data loss would happen, if we’d happen to upgrade for example before user submits a form
    • arranging so that safeguarded states cannot be entered after a upgrade preration has been initiated
    • could it be possible to give users a read-only access to the application while the new instance is starting up? (google code style)

Hello world tutorial

Starting out, for simplest hello world I found this, rather old tutorial which presents how to build the simplest bundle, and deploy it in an equinox instance. To update the original tutorial by Aneesh Kumar KB:

I will not referate that (good) tutorial here; go read it and try it. For me, the activator class was quickly compiled and bundled in, my first ever OSGi bundle is ready, easily started and stopped. That was not so hard.

You should note that if you kill the java process (CTRL-C) and restart it (after having the bundle installed and started) it will now start at launch. See the directory configuration under the directory you placed osgi runtime in. More about that in runtime options manual.

Hello world, maven style

Oki, so now lets do that with maven. As far as I know there are two ways of doing OSGi with maven:

  • Tycho — for what would seem like MANIFEST.MF first, pom.xml second development
  • maven-bundle-plugin — which does MANIFEST.MF generation from pom.xml

For me the obvious choice is to use later, the maven-bundle-plugin. Obvious because so far I’ve used maven to build many released and unreleased software products, both standalone and webapp and most importantly, I’ve finally realized how to use it effectively.

Creating maven project, no eclipse yet! Project structure:

  • test-parent (groupId=osgi.test, artifactId=test-parent, packaging=pom)
    • hello-bundle (artifactId=hello-bundle, packaging=jar)

Set up the directory structure, throw the previous Activator.java in test-parent/hello-bundle/src/main/java/osgi/test and add the following plugin:


<plugin>
  <groupId>org.apache.felix</groupId>
  <artifactId>maven-bundle-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
    <instructions>
      <!--<Export-Package>we do not export</Export-Package>-->
      <!--<Private-Package>no privates either</Private-Package>-->
      <Bundle-Activator>osgi.test.Activator</Bundle-Activator>
    </instructions>
  </configuration>
</plugin>

When you launch maven to compile it, from the parent project, you’ll find that of course we need to add osgi framework dependency. Now… How does one find a prepackaged publicly downloadable equinox for maven2 dependency.

It took a while, but of course SpringSource has the bundle! As it’s so not nice to include code in wordpress blog, copy-paste the repository from faq question 8. The closest depedency to current 3.6 was 3.6.1* (more copypasting).

For what scope to use on the dependency, I’d guess “provided”, as it does not need to be included in any wars we might decide to build. (Looking how fast the bundle downloads, now might be good time to invest some energy on local artifactory or nexus instance.) Next up, compile again and it works now!

Even package works! A quick look at the generated archive reveals that maven-bundle-plugin was not bothered by our configuration; there are no OSGi specific MANIFEST.MF entries. This is because we specified packaging=jar, whereas it should be packaging=bundle.

Now lets see what happens.. At first it’d seem like maven downloads all the plugins in the world, but it’s finally ready. Again we have our jar in target/ and now it even has proper (I think) MANIFEST.MF entries. Now might be good time to read up how Bundle-SymbolicName is generated by the plugin (unless specified).

If you now attempt to launch the bundle and you notice that there’s a missing dependency: Import-Package: osgi.test; version=”0.0.0″, it’s because you mistyped the activator package name. maven-bundle-plugin will think you need to import that package, so that the activator can be run from there.

So, there you have it. Too bad wordpress does not allow me to link upload jars or tar.gz. Please comment if you’d like download final files. Original copyright of the amazing Activator.java belongs to http://aneeshkumarkb.blogspot.com/.

Next time I’ll try to modify this into HelloWorld servlet and try to get it up on virgo.

Advertisements

2 Comments

  1. Art Doler
    Posted 2012-04-19 at 18:00 | Permalink | Reply

    Did you ever successfully manage to deploy a maven project into Virgo? I’ve got the bundle building, but I’m a little stymied on the next steps to take.

    • Posted 2012-04-27 at 13:21 | Permalink | Reply

      Actually no. After the initial exploration I’ve mostly been busy on trying to understand design for OSGi applications so I haven’t made any real progress.

      Have you written down anything you’ve encountered? #osgi, #java @ irc.freenode.net could be great places to start.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: