Java Tips & Tricks: Building OSGi WABs with Gradle

Published Mar 23, 2016Last updated Jan 14, 2017

You might spend a few hours researching different ideas on how to build OSGi WABs (OSGi-fied WAR archive for deployment web applications in OSGi containers). Here's the solution that works for me so far:

apply plugin: 'osgi'
apply plugin: 'war'

war {
    manifest = osgiManifest {
        instruction 'Web-ContextPath', '/context-path'
        instruction 'Webapp-Context', '/context-path'
        instruction 'Bundle-ClassPath', '.;/WEB-INF/classes'

        classesDir = project.sourceSets.main.output.classesDir
        classpath = project.configurations.runtime

Two critical elements:

  1. Setting Bundle-ClassPath not to '/WEB-INF/classes' but '.;/WEB-INF/classes' (notice the dot). This way bnd's Analyzer will be able to find compiled classes for imported/exported packages analysis.
  2. Configuring classesDir and class path. osgi plugin does this by default to the main jar task, but nothing else, so it needs to be done explicitly.

Now, just run:

gradle war

(And yes, I am using Gradle 2.3!)

Hopefully it'll work for you!

Discover and read more posts from Yurii Rashkovskii
get started