Skip to content

Gradle Plugin for non Workspace builds

BJ Hargrave edited this page Dec 9, 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.1 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 or Baseline task types.

At least Gradle 2.0 is required.

Using Bnd Gradle Plugin

To get the Bnd Gradle plugin on your buildscript, use the following:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.1.0'
  }
}

Apply the biz.aQute.bnd.builder plugin to your project

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
}

In either usage mode, there are three properties, in addition to the Jar task 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'

configurations {
  bundleCompile
}
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',
                   '-include': 'other.bnd')
    }
}

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.

Create a task of the Baseline type

You can also create a new task of the Baseline type. This task type will baseline a bundle against a different version of the bundle. For example:

import aQute.bnd.gradle.Baseline

apply plugin: 'java'

configurations {
  baseline
}
dependencies {
    baseline('group': group, 'name': archivesBaseName, 'version': '(,)') {
      transitive false
    }
  }
}
task baseline(type: Baseline) {
  bundle jar
  baseline configurations.baseline
}

There are four properties which can be configured for a Baseline task:

ignoreFailures

If true the build will not fail due to baseline problems; instead an error message will be logged. Otherwise, the build will fail. The default is 'false'.

baselineReportDirName

This is the name of the baseline reports directory. Can be a name or a path relative to ReportingExtension.getBaseDir(). The default name is 'baseline'.

bundle

This is the bundle to be baselined. It can either be a File or a task that produces a bundle. This property must be set.

baseline

This is the baseline bundle. It can either be a File or a Configuration. If a Configuration is specified, it must contain a single file; otherwise an exception will fail the build. This property must be set.

Clone this wiki locally