Mavenizing “OSGi in Action” examples
Table of Contents
Mavenizing “OSGi in Action” examples #
If you used Ant for some time a strange new feeling makes you uncomfortable once your project grow beyond some point (of no return). The plethora of build files resembles spaghetti programming and that not good. The issue with Ant is that no defaults for your install are used and you need to create your build system from scratch. Every time.
Maven contains a standard build process and you don’t need to start from scratch, just place your files in a predefined location (or tell Maven that you placed into a non-standard-to-maven path) and violà. Also Maven manages the dependencies for you, just declare and use them. These are the main reasons I use Maven.
One of the goals is to not change sample code or code paths, just to make it work using Ant or Maven, no matter what build system you prefer.
Chapter 1, Modularity example #
To do it simpler just modified Chapter 1 Modularity example. Starting with the main module the stuff is pretty standard, by creating the pom file, declaring it as a jar, telling Maven where the source files are located and declaring Apache Felix (OSGi implementation) as a dependency. That’s all.
Regarding the two remaining modules the first thing to note is that they must be declared as bundles otherwise maven-bundle-plugin won’t add the proper tags to manifest.
bundle
Now to created a bundle (a jar file + more entries in MANIFEST) the must be used. Not big deal, just declare and use it.
org.apache.felix
maven-bundle-plugin
true
org.foo.hello;version=”[1.0,2.0)”
Previous pom snippet is for client (consumer) module/bundle where the package to use is org.foo.hello from version 1.0 to 2.0 (excluded). The next one is for provider module/bundle that exports package org.foo.hello version 1.0
org.apache.felix
maven-bundle-plugin
true
org.foo.hello;version=”1.0"
A minor modification, related to the example and not to Maven, is that the code expects two jars in the bundles directory. This is accomplished by using the maven-dependency-plugin plugin.
Juicy bit : class loading #
To make the whole thing run, because I didn’t create a jar containing Felix classes, I just wanted to add the whole bits and bolts in the command line. You can see in the run script that the only classes that are added to initial classpath are Felix and the Main class, neither producer nor consumer : the basic idea behind OSGi is to isolate application classes from classloader manipulation then what really happens here is that Felix is started and takes control of classloader management adding it to client and provider the concerning bits as declared in the related MANIFEST entries.
__cp=”org.foo.hello.main/target/launcher-1.0.jar”
__cp=”${__cp}:${__mavenRepositoryHome}/org/apache/felix/org.apache.felix.framework/3.0.7/org.apache.felix.framework-3.0.7.jar”
java -cp ${__cp} org.foo.hello.main.Main
To take a look at the whole thing you can clone the Rosebud repository.
Enjoy !
EDIT