Deploying OSGI bundles automatically with Maven

Working with bundles if kind of frustrating if you have to play with couple of them. Each time you build them, they need to be deployed in the container, and doing it by hand can be a tendentious job.

Instead, a much better approach would be to change the build process to make this happen automatically. For this there are couple of approaches, from which I find as easiest to put in practice to use the container’s auto-deploy feature. Both Felix and GlassFish (these I’ve played with), but probably the others have this feature to monitor a folder for new jar files, and when they appear in the folder, they automatically deploy them. When bundles are removed from the folder, they are un-deployed from the container. If the jar file changes, it is either updated or redeployed.

GlassFish already supports this deployment process. The folder where is watching for new bundles is located at:

    glassfishv3/glassfish/domains/domain1/autodeploy/

for .war applications and

    glassfishv3/glassfish/domains/domain1/autodeploy/bundles

for OSGI bundles.

In order to have this deployment active in Felix, you will need to install an additional OSGI module, called File Install. Can be downloaded from Felix’s web page. You just have to put it in

<felix install>/bundles/

folder and will be deployed next time container starts.
The folder where Felix will watch for bundles can be configured using the  felix.fileinstall.dir property from the file located at:

<felix install>/conf/config.properties

Having these said, all what remains to be done is to configure the latest step of build to copy all the bundles which need to be deployed in the target folder where it will be deployed by the OSGI container. This step depends on what IDE, build system,etc are you using.

For users with Maven, you can add for example an additional module called let’s say deploy, who’s job would be to deploy the application. You will have to add all artifacts and dependencies which need to be installed in this module’s POM file, then you can add a build step like in the file bellow in order to have these artifacts copied in the target folder:

<!-- usual POM stuff -->
...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-bundles</id>
          <phase>validate</phase>
          <goals>
            <goal>copy</goal>
          </goals>
          <configuration>
            <artifactItems>
              <!--  Our bundles which need to be deployed -->
              <artifactItem>
                <groupId>group id of your artifact</groupId>
                <artifactId>id of your artifact</artifactId>
                <version>version of your artifact</version>
              </artifactItem>
              <artifactItem>
                <groupId>group of your artifact</groupId>
                <artifactId>id of your artifact</artifactId>
                <version>version of your artifact</version>
              </artifactItem>
                 ...
            </artifactItems>
            <outputDirectory>target folder where container looks for OSGI bundles</outputDirectory>
            <overWriteSnapshots>true</overWriteSnapshots>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Of course this can be made in many ways, this is one which worked for me. Feel free to add comments and suggestions over the process

  1. You might also be interested in Nimble which automates the resolution, provisioning and clean up of all of your dependencies based on your top level artifact e.g. a bundle or a WAR.

    Nimble offers an OSGi framework independent shell, scripting environment, dependency resolver and provisioner. Included with Nimble is the Paremus OSGi Shell (Posh), an implementation of the draft RFC147 Command Line spec, it provides a bash-like, scriptable command line interface and downloads and caches bundles on demand, and auto-loads shell commands as they are needed.

    Nimble is free to download and use in dev, test and production with a 30 day renewable license.

    You can find out more and download it from http://www.paremus.com/nimble

    Mike
    (Paremus)

Leave a Comment