Skip to content

Gradle Plugin for non Workspace builds

BJ Hargrave edited this page Sep 4, 2015 · 20 revisions

Sometimes developers want to build bundles but they don't use a full Bnd Workspace based build environment! Go figure?!

So now Bnd 3.0 offers Gradle support for building bundles in typical Gradle build environments. There are two ways this support can be used. You can apply the biz.aQute.bnd.builder plugin to your project or use the new Bundle task type.

At least Gradle 2.0 is required.

Under Development

Note: Since Bnd 3.0 is still under development and not released to JCenter or Maven Central, the latest Bnd builds from Cloudbees need to be used. To get the Bnd Gradle code on your buildscript, use the following:

buildscript {
  repositories {
    ivy {
      url 'https://bndtools.ci.cloudbees.com/job/bnd.master/lastSuccessfulBuild/artifact/dist/bundles'
      layout 'pattern', {
            artifact '[module]/[artifact]-[revision].[ext]' /* OSGi repo pattern */
      }
    }
  }
  dependencies {
    classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.0.0'
  }
}

Apply a plugin

You can apply the biz.aQute.bnd.builder plugin to your project. This plugin extends the normal java plugin by extending the jar task. Some new properties are added to the jar task and the task actions are extended to use Bnd to generate a bundle. The bundle will contain all the content configured into the jar task plus whatever additional content is included via the Bnd instructions.

apply plugin: 'biz.aQute.bnd.builder'

Create a task of the Bundle type

You can also create a new task of the Bundle type. This task type is an extension of the Jar task type that adds new properties and uses Bnd to generate a bundle. The bundle will contain all the content configured into the task plus whatever additional content is included via the Bnd instructions. For example:

import aQute.bnd.gradle.Bundle

apply plugin: 'java'

task bundle(type: Bundle) {
  from sourceSets.main.output
}

New properties

In either usage mode, there are three new properties which can be configured.

bndfile

This is the File object to use for the bnd file to use to create the bundle. The default value is project.file('bnd.bnd'). If the bnd file does not exist, this is OK. But without some instructions to Bnd, your bundle will not be very interesting.

configuration

This is the Configuration object to use as the classpath for the Bnd builder. The default value is the project.configurations.compile Configuration. You will only need to specify this property if you want to use a different Configuration for the classpath or the default Configuration does not exist.

sourceSet

This is the SourceSet object to use as the sourcepath for the Bnd builder. The default value is the project.sourceSets.main SourceSet. You will only need to specify this property if you use -sources: true in your Bnd instructions and you want to use a different SourceSet for the sourcepath or the default SourceSet does not exist.

Example

import aQute.bnd.gradle.Bundle

apply plugin: 'java'

sourceSets {
  bundle
}

task bundle(type: Bundle) {
  from sourceSets.bundle.output
  bndfile = project.file('bundle.bnd')
  configuration = configurations.bundleCompile
  sourceSet = sourceSets.bundle
}

Instructing Bnd on how to build your bundle

The normal way to instruct Bnd on how to build the bundle is to use a bnd file. This file will include the Bnd instructions like Export-Package, etc. However, you can also use the manifest property to instruct Bnd. For example:

apply plugin: 'biz.aQute.bnd.builder'

jar {
    manifest {
        attributes('Export-Package': 'com.acme.api.*',
                   '-sources': 'true')
    }
}

You can even use a combination of the manifest property and a bnd file. But the bnd file takes priority over the manifest property. So if the same header is in both places, the one in the bnd file will be used and the one in the manifest property will be ignored.

Clone this wiki locally