FROM: http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html
Simple Example
Rather than going straight to a detailed list of plugin features, we will first look at a simple example of how to use the plugin to give an immediate flavor. A detailed "how to" will follow.
Assume that we have a simple bundle project that has a pubic API package an several implementation packages, such as:
org.foo.myproject.api org.foo.myproject.impl1 org.foo.myproject.impl2 ...
If we also assume that we have a bundle activator in one of the implementation packages, then the
...... org.apache.felix maven-bundle-plugin true org.foo.myproject.api org.foo.myproject.* org.foo.myproject.impl1.Activator
The
Features
The BND library underlying the plugin defines instructions to direct its behavior. For this Maven plugin, these instructions are issued in the plugin configuration section of the POM file, as was illustrated above. BND recognizes three types of instructions:
- Manifest headers - Any instruction that starts with a capital letter will appear in the resulting bundle's manifest file; the value for the header will either be copied, augmented, or generated by BND depending on the instruction.
- Variables - Any instruction starting with a lowercase letter is assumed to be a variable in the form of a name-value pair, such as version=3.0, that can be used for property substitution, but is not copied to the manifest.
- Directives - Any instruction starting with a '-' character is considered to be a directive that informs BND to perform some special processing and is not copied to the manifest.
The remainder of this section covers the most important aspects of BND's instructions; for complete details refer to the BND documentation.
Instructions
The
The list of package patterns is ordered and earlier patterns are applied before later patterns. For example, if you specify "org.foo.*,!org.foo.impl" the second pattern has no effect since all org.foopackages have already been selected by the first pattern. Instead, you should specify "!org.foo.impl,org.foo.*", which will export all org.foo packages except org.foo.impl.
Following standard OSGi R4 syntax, package patterns can include both directives and attributes, which will be copied appropriately into the generated Export-Package manifest header. Besides explicitly listing package version attributes, BND will also determine package versions by examining the source JAR file or from packageinfo files in the package directory.
The
The
clause ::= assignment | inline | simple assignment ::= PATH '=' PATH simple ::= PATH inline ::= '@' PATH
For the
If a resource clause is specified inside of "{ ... }" brackets, then variable substitution will be performed on the resource, where variables in the resources are denoted with "${ ... }" syntax.
By default the bundle plugin converts the project's Maven resource directories into a single
The
Default Behavior
To use this plugin, very little information is required by BND. As part of the Maven integration, the plugin tries to set reasonable defaults for various instructions. For example:
-
is computed using the shared Maven2OsgiConverter component, which uses the following algorithm:
Get the symbolic name as groupId + "." + artifactId, with the following exceptions:- if artifact.getFile is not null and the jar contains a OSGi Manifest with Bundle-SymbolicName property then that value is returned
- if groupId has only one section (no dots) and artifact.getFile is not null then the first package name with classes is returned. eg. commons-logging:commons-logging -> org.apache.commons.logging
- if artifactId is equal to last section of groupId then groupId is returned. eg. org.apache.maven:maven -> org.apache.maven
- if artifactId starts with last section of groupId that portion is removed. eg. org.apache.maven:maven-core -> org.apache.maven.core
The computed symbolic name is also stored in the $(maven-symbolicname) property in case you want to add attributes or directives to it.
-
is now assumed to be the set of packages in your local Java sources, excluding the default package '.' and any packages containing 'impl' or 'internal'.
(before version 2 of the bundleplugin it was based on the symbolic name) - Since 2.2.0 you can also use {local-packages} inside
and it will be expanded to the set of local packages. -
is now assumed to be the set of packages in your local Java sources (note that any packages in both and will be exported).
(before version 2 of the bundleplugin it was assumed to be empty by default) -
is assumed to be "*", which imports everything referred to by the bundle content, but not contained in the bundle.
Any exported packages are also imported by default, to ensure a consistent class space. -
is generated from the project's Maven resources, typically "src/main/resources/", which will copy the specified project directory hierarchy into the resulting bundle JAR file, mirroring standard Maven behavior. -
is assumed to be "${pom.version}" but is normalized to the OSGi version format of "MAJOR.MINOR.MICRO.QUALIFIER", for example "2.1-SNAPSHOT" would become "2.1.0.SNAPSHOT". -
is assumed to be "${pom.name}". -
is assumed to be "${pom.description}". -
is assumed to be "${pom.licenses}". -
is assumed to be "${pom.organization.name}". -
is assumed to be "${pom.organization.url}".
Since the plugin creates bundles for OSGi R4, it hard-codes Bundle-ManifestVersion to be '2'. Additionally, it generates imports for every export to ensure package substitutability, which is very important when working with collaborating services. It is possible to override any of these values (except Bundle-ManifestVersion) just by specifying the desired value in the plugin configuration section of the POM file.
Detailed "How To"
Get Maven2
The first step in the process of using the plugin is downloading and installing the latest version of the Maven2 runtime. The latest Maven2 release and instuctions for getting started with Maven2 can be found at the Maven website.
Using the Plugin
To use the maven-bundle-plugin, you first need to add the plugin and some appropriate plugin configuration to your bundle project's POM. Below is an example of a simple OSGi bundle POM for Maven2:
4.0.0 my-osgi-bundles examplebundle bundle 1.0 Example Bundle org.apache.felix org.osgi.core 1.0.0 org.apache.felix maven-bundle-plugin true com.my.company.api com.my.company.* com.my.company.Activator
There are two main things to note: (1) the
Real-World Example
Consider this more real-world example using Felix' Log Service implementation. The Log Service project is comprised of a single package: org.apache.felix.log.impl. It has a dependency on the core OSGi interfaces as well as a dependency on the compendium OSGi interfaces for the specific log service interfaces. The following is its POM file:
4.0.0 org.apache.felix org.apache.felix.log bundle Apache Felix Log Service 0.8.0-SNAPSHOT This bundle provides an implementation of the OSGi R4 Log service. ${pom.groupId} org.osgi.core 0.8.0-incubator ${pom.groupId} org.osgi.compendium 0.9.0-incubator-SNAPSHOT org.apache.felix maven-bundle-plugin true org.osgi.service.log org.apache.felix.log.impl ${pom.artifactId} ${pom.artifactId}.impl.Activator org.osgi.service.log.LogService,org.osgi.service.log.LogReaderService
Notice that the
Manifest-Version: 1 Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt Bundle-Activator: org.apache.felix.log.impl.Activator Import-Package: org.osgi.framework;version=1.3, org.osgi.service.log;v ersion=1.3 Include-Resource: src/main/resources Export-Package: org.osgi.service.log;uses:=org.osgi.framework;version= 1.3 Bundle-Version: 0.8.0.SNAPSHOT Bundle-Name: Apache Felix Log Service Bundle-Description: This bundle provides an implementation of the OSGi R4 Log service. Private-Package: org.apache.felix.log.impl Bundle-ManifestVersion: 2 Export-Service: org.osgi.service.log.LogService,org.osgi.service.log.L ogReaderService Bundle-SymbolicName: org.apache.felix.log
The resulting bundle JAR file has the following content (notice how the LICENSE and NOTICE files were automatically copied from the src/main/resources/ directory of the project):
META-INF/MANIFEST.MF LICENSE META-INF/ META-INF/maven/ META-INF/maven/org.apache.felix/ META-INF/maven/org.apache.felix/org.apache.felix.log/ META-INF/maven/org.apache.felix/org.apache.felix.log/pom.properties META-INF/maven/org.apache.felix/org.apache.felix.log/pom.xml NOTICE org/ org/apache/ org/apache/felix/ org/apache/felix/log/ org/apache/felix/log/impl/ org/apache/felix/log/impl/Activator.class org/apache/felix/log/impl/Log.class org/apache/felix/log/impl/LogEntryImpl.class org/apache/felix/log/impl/LogException.class org/apache/felix/log/impl/LogListenerThread.class org/apache/felix/log/impl/LogNode.class org/apache/felix/log/impl/LogNodeEnumeration.class org/apache/felix/log/impl/LogReaderServiceFactory.class org/apache/felix/log/impl/LogReaderServiceImpl.class org/apache/felix/log/impl/LogServiceFactory.class org/apache/felix/log/impl/LogServiceImpl.class org/osgi/ org/osgi/service/ org/osgi/service/log/ org/osgi/service/log/LogEntry.class org/osgi/service/log/LogListener.class org/osgi/service/log/LogReaderService.class org/osgi/service/log/LogService.class org/osgi/service/log/package.html org/osgi/service/log/packageinfo
Adding OSGi metadata to existing projects without changing the packaging type
If you want to keep your project packaging type (for example "jar") but would like to add OSGi metadata
you can use the manifest goal to generate a bundle manifest. The maven-jar-plugin can then be used to
add this manifest to the final artifact. For example:
maven-jar-plugin ${project.build.outputDirectory}/META-INF/MANIFEST.MF org.apache.felix maven-bundle-plugin bundle-manifest process-classes manifest
If you want to use packaging types other than "jar" and "bundle" then you also need to enable support
for them in the bundleplugin configuration, for example if you want to use the plugin with WAR files:
org.apache.felix maven-bundle-plugin bundle-manifest process-classes manifest jar bundle war
You'll also need to configure the other plugin to pick up and use the generated manifest, which is written to ${project.build.outputDirectory}/META-INF/MANIFEST.MF by default (unless you choose a different manifestLocation in the maven-bundle-plugin configuration). Continuing with our WAR example:
org.apache.maven.plugins
maven-war-plugin
${project.build.outputDirectory}/META-INF/MANIFEST.MF
Building the Plugin
The plugin is hosted at the Apache Felix project. The following steps describe how to build and install the plugin into your local Maven2 repository.
Using the SVN client of your choice, checkout the maven-bundle-plugin project.
$ svn co http://svn.apache.org/repos/asf/felix/trunk/bundleplugin
Using Maven2, build and install the maven-bundle-plugin by issuing the following Maven2 command in the project directory that was created as a result of the previous step.
$ mvn install
Goals
The maven-bundle-plugin also provides additional functionality via some Maven goals. Command-line execution of a goal is performed as follows:
mvn org.apache.felix:maven-bundle-plugin:GOAL
Where GOAL is one of the following:
- bundle - build an OSGi bundle jar for the current project
configuration options:- manifestLocation defaults to ${project.build.outputDirectory}/META-INF
- unpackBundle unpack bundle contents to output directory, defaults to false
- excludeDependencies comma-separated list of dependency artifactIds to exclude from the classpath passed to Bnd, use "true" to exclude everything.
Version 2 of the bundleplugin now supports the same style of filter clauses in excludeDependencies as Embed-Dependency. - classifier attach bundle to the project using the given classifier
- supportedProjectTypes defaults to "jar","bundle"
- bundleall - build OSGi bundle jars for all transitive dependencies
configuration options:- wrapImportPackage defaults to "*"
- supportedProjectTypes defaults to "jar","bundle"
- wrap - as above, but limited to the first level of dependencies
configuration options:- wrapImportPackage defaults to "*"
- supportedProjectTypes defaults to "jar","bundle"
- manifest - create an OSGi manifest for the current project
configuration options:- manifestLocation defaults to ${project.build.outputDirectory}/META-INF
- supportedProjectTypes defaults to "jar","bundle"
- install - adds the current bundle project to the local OBR
configuration options:- obrRepository path to local OBR, defaults to
/repository.xml - supportedProjectTypes defaults to "jar","bundle"
- obrRepository path to local OBR, defaults to
More GOALs are available in the 1.4.0 release:
- ant - create an Ant build script to rebuild the bundle
- install-file - adds a local bundle file to the local OBR
configuration options:- obrRepository path to local OBR, defaults to
/repository.xml - groupId Maven groupId for the bundle, taken from pomFile if given
- artifactId Maven artifactId for the bundle, taken from pomFile if given
- version Maven version for the bundle, taken from pomFile if given
- packaging Maven packaging type for the bundle, taken from pomFile if given
- classifier Maven classifier type, defaults to none
- pomFile optional Pom file describing the bundle
- file bundle file, defaults to the bundle from the local Maven repository
- obrXml optional additional properties for the bundle
- obrRepository path to local OBR, defaults to
- deploy - adds the current bundle project to a remote OBR
configuration options:- remoteOBR name of remote OBR, defaults to NONE (which means no remote OBR deployment)
- obrRepository used when the remoteOBR name is blank, defaults to repository.xml
- prefixUrl optional public URL prefix for the remote repository
- bundleUrl optional public URL where the bundle has been deployed
- altDeploymentRepository alternative remote repository, id::layout::url
- obrDeploymentRepository optional OBR specific deployment repository.
- ignoreLock ignore remote locking when updating the OBR
- supportedProjectTypes defaults to "jar","bundle"
- deploy-file - adds a local bundle file to a remote OBR
configuration options:- remoteOBR name of remote OBR, defaults to an empty string
- obrRepository used when the remoteOBR name is blank, defaults to repository.xml
- repositoryId optional repository id, used to lookup authentication settings
- url remote repository transport URL, like
scpexe://host/path/to/obr
- bundleUrl public URL of deployed bundle, like
http://www.foo.org/bundles/foo.jar
- groupId Maven groupId for the bundle, taken from pomFile if given
- artifactId Maven artifactId for the bundle, taken from pomFile if given
- version Maven version for the bundle, taken from pomFile if given
- packaging Maven packaging type for the bundle, taken from pomFile if given
- classifier Maven classifier type, defaults to none
- pomFile optional Pom file describing the bundle
- file bundle file, defaults to the bundle from the local Maven repository
- obrXml optional additional properties for the bundle
- ignoreLock ignore remote locking when updating the OBR
- clean - cleans the local OBR, removing missing bundles
configuration options:- obrRepository path to local OBR, defaults to
/repository.xml
- obrRepository path to local OBR, defaults to
- remote-clean - cleans a remote OBR, removing missing bundles
configuration options:- remoteOBR name of remote OBR, defaults to NONE (which means no remote cleaning)
- obrRepository used when the remoteOBR name is blank, defaults to repository.xml
- prefixUrl optional public URL prefix for the remote repository
- altDeploymentRepository alternative remote repository, id::layout::url
- obrDeploymentRepository optional OBR specific deployment repository.
- ignoreLock ignore remote locking when updating the OBR
There are also new instructions available from the underlying BND tool, which continues to be improved independently; for the latest see BND documentation.
The default goal bundle will be initialized by setting the
The following features are only available from version 1.2.0 onwards
Embedding dependencies
The Maven Bundle Plugin supports embedding of selected project dependencies inside the bundle by using the
dependencies
where:
dependencies ::= clause ( ',' clause ) * clause ::= MATCH ( ';' attr '=' MATCH | ';inline=' inline ) attr ::= 'groupId' | 'artifactId' | 'version' | 'scope' | 'type' | 'classifier' | 'optional' inline ::= 'true' | 'false' | PATH ( '|' PATH ) * MATCH ::=PATH ::=
The plugin uses the
The MATCH section accepts alternatives, separated by |, and can be negated by using ! at the beginning of the MATCH. Use * to represent zero or more unknown characters and ? to represent zero or one character. You can also use standard Java regexp constructs. There is no need to escape the . character inside MATCH. The first MATCH in a clause will filter against the artifactId.
some examples:
*;scope=compile|runtime junit;scope=runtime *;scope=!runtime;type=!pom;inline=true *;scope=compile|runtime;inline=false;artifactId=!cli|lang|runtime|tidy|jsch *;inline=images/**|icons/**
examples of using {maven-dependencies}:
{maven-resources}, {maven-dependencies}, org/foo/Example.class=target/classes/org/foo/Example.class .,{maven-dependencies},some.jar
By default matched dependencies are embedded in the bundle as artifactId-version.jar. This behaviour can be modified using the following instructions:
-
true - removes the version from the file (ie. artifactId.jar) -
false - adds the groupId as a subdirectory (ie. groupId/artifactId-version.jar) -
directory - adds a subdirectory (ie. directory/artifactId-version.jar)
Normally the plugin only checks direct dependencies, but this can be changed to include the complete set of transitive dependencies with the following option:
true
If you want a dependency inlined instead of embedded add the inline=true. For example to inline all compile and runtime scoped dependencies use:
*;scope=compile|runtime;inline=true
Embed-Dependency and Export-Package
If you embed a dependency with
<_exportcontents> behaves just like Export-Package, except it doesn't change the content of the bundle, just what content should be exported.
OBR integration
The latest Maven Bundle Plugin automatically updates the local OBR repository.xml file during the install phase, using a default location of:
/repository.xml
You can configure the location of the OBR repository by using the command line:
mvn clean install -DobrRepository=
or in the configuration section for the maven-bundle-plugin in your Maven POM:
org.apache.felix maven-bundle-plugin true PATH_TO_OBR
to disable OBR installation set the obrRepository to NONE, for example:
org.apache.felix maven-bundle-plugin true NONE
Eclipse/PDE integration
It is possible to configure the Maven Bundle Plugin to put the bundle manifest where Eclipse/PDE expects it, and use the Maven Dependency Plugin to arrange for any embedded dependencies to appear in a local directory that matches the Bundle-ClassPath entries. Here is an example POM that does this:
org.example org.example 4.0.0 examples org.example 1.0-SNAPSHOT ${bundle.symbolicName} [${bundle.namespace}] bundle src/main/resources . plugin.xml org.apache.felix maven-bundle-plugin 1.2.1 true META-INF ${bundle.symbolicName} ${pom.version} !${bundle.namespace}.internal.*,${bundle.namespace}.*;version="${pom.version}" ${bundle.namespace}.internal.* ${bundle.namespace}.internal.ExampleActivator *;scope=compile|runtime;inline=false target/dependency true maven-dependency-plugin copy-dependencies package copy-dependencies org.osgi osgi_R4_core 1.0 provided true org.osgi osgi_R4_compendium 1.0 provided true junit junit 3.8.1 compile true
To generate the Eclipse metadata use:
mvn clean package eclipse:eclipse -Declipse.pde install
and you should now be able to import this as an existing Eclipse project.
FYI: the above POM was generated using the pax-create-bundle command from Pax-Construct and then tweaked to demonstrate using the Maven Dependency Plugin to handle embedded jars in Eclipse.
With the original Pax-Construct generated POM you would simply use:
mvn clean package pax:eclipse
to create the appropriate Eclipse files and manifest, and also handle any embedded entries. The pax:eclipse goal extends eclipse:eclipse, and supports the same parameters.
Unpacking bundle contents to 'target/classes'
Once in a while you may create a bundle which contains additional classes to the ones compiled from src/main/java, for example when you embed the classes from another jar. This can sometimes cause unforeseen problems in Maven, as it will use the output directory (target/classes) rather than the final bundle, when compiling against projects in the same reactor (ie. the same build).
The easiest way to get around this Maven 'feature' is to unpack the contents of the bundle to the output directory after the packaging step, so the additional classes will be found where Maven expects them. Thankfully there is now an easy option to do this in the bundle-plugin:
org.apache.felix maven-bundle-plugin true true
Using an existing MANIFEST.MF file
If you have an existing manifest, you can add this to the Bnd instructions, like so:
<_include>src/main/resources/META-INF/MANIFEST.MForg.example.*
Bnd will use it when calculating the bundle contents, and will also copy across all manifest attributes starting with a capital letter.
As shown in the above example, you could use this to include a non-OSGi manifest which you then customize with extra OSGi attributes.
The following features are only available from version 1.4.0 onwards
bundle:ant
The ant goal creates a customized build.xml Ant script along with a collection of BND instructions and properties, taken from the current project and stored in maven-build.bnd. You also need to runant:ant to create the standard Ant support tasks to download Maven dependencies and perform compilation, etc.
The customized Ant script uses the BND tool to rebuild the bundle, so any source changes should be reflected in the (re)generated manifest.
Example:
mvn ant:ant bundle:ant ant clean package
bundle:install-file
The install-file goal updates the local OBR with the details of a bundle from the local filesystem.
configuration:
- obrRepository path to local OBR, defaults to
/repository.xml - groupId Maven groupId for the bundle, taken from pomFile if given
- artifactId Maven artifactId for the bundle, taken from pomFile if given
- version Maven version for the bundle, taken from pomFile if given
- packaging Maven packaging type for the bundle, taken from pomFile if given
- classifier Maven classifier type, defaults to none
- pomFile optional Pom file describing the bundle
- file bundle file, defaults to the bundle from the local Maven repository
- obrXml optional additional properties for the bundle
Example:
mvn org.apache.felix:maven-bundle-plugin:1.4.0:install-file \ -DpomFile=myPom.xml -Dfile=foo-1.0.jar
bundle:deploy
The deploy goal updates the remote OBR with the details of the deployed bundle from the local Maven repository. The remote OBR is found by querying the
(If the project has an obr.xml file somewhere in its resources, then it will be automatically detected and applied.)
configuration:
- remoteOBR name of remote OBR, defaults to NONE (which means no remote OBR deployment)
- obrRepository used when the remoteOBR name is blank, defaults to repository.xml
- altDeploymentRepository alternative remote repository, id::layout::url
- ignoreLock ignore remote locking when updating the OBR
This goal is part of the "bundle" packaging lifecycle, but is disabled by default - to enable just set the remoteOBR parameter.
bundle:deploy-file
The deploy-file goal updates the remote OBR with the details of a deployed bundle from the local filesystem. The remote OBR is found using the -DrepositoryId and -Durl parameters. Seehttp://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html for more details about these particular settings.
You can use the -DbundleUrl parameter to give the public location of the deployed bundle, which may differ from the remote OBR location.
configuration:
- remoteOBR name of remote OBR, defaults to an empty string
- obrRepository used when the remoteOBR name is blank, defaults to repository.xml
- repositoryId optional repository id, used to lookup authentication settings
- url remote repository transport URL, like
scpexe://host/path/to/obr
- bundleUrl public URL of deployed bundle, like
http://www.foo.org/bundles/foo.jar
- groupId Maven groupId for the bundle, taken from pomFile if given
- artifactId Maven artifactId for the bundle, taken from pomFile if given
- version Maven version for the bundle, taken from pomFile if given
- packaging Maven packaging type for the bundle, taken from pomFile if given
- classifier Maven classifier type, defaults to none
- pomFile optional Pom file describing the bundle
- file bundle file, defaults to the bundle from the local Maven repository
- obrXml optional additional properties for the bundle
- ignoreLock ignore remote locking when updating the OBR
Example:
mvn org.apache.felix:maven-bundle-plugin:1.4.0:deploy-file \ -DpomFile=myPom.xml -Dfile=foo-1.0.jar -Durl=file:/tmp/example/OBR \ -DbundleUrl=http://www.foo.org/bundles/foo.jar
bundle:clean
Sometimes you would like to clean your local OBR because it contains bundles that are no longer in your local Maven repository. This case often occurs when artifacts were deleted manually. The maven-bundle-plugin provides a simple goal to check for missing bundles, and remove them from the local OBR.
configuration:
- obrRepository path to local OBR, defaults to
/repository.xml
Example:
mvn bundle:clean
bundle:index
The index goal allows the creation of an OBR repository based on a set of jars in a maven repository.
Configuration:
- obrRepository path to local OBR, defaults to
/repository.xml - urlTemplate template for generating urls for OBR resources
- mavenRepository path to the maven repository, defaults to
Possible values for the urlTemplate are:
- maven this will create a maven based url such as mvn:groupid/artifactid/version
- pattern with the following placeholders:
- %v bundle version
- %s bundle symbolic name
- %f file name
- %p file path
Concurrent updates
With a remote OBR, several uploads may occur at the same time. However, the remote OBR is centralized in one file, so concurrent modification must be avoided. To achieve this, the plug-in implements a locking system. Each time the plug-in tries to modify the file it sets a file based lock. If it can't take the lock, it will wait and retry. After 3 attempts the upload process fails. To bypass this lock add -DignoreLock to the command-line (or add
FTP protocol
Not all protocols are supported by Maven out of the box. For example the ftp protocol requires the wagon-ftp component. To enable the ftp protocol add this to your Pom:
org.apache.maven.wagon wagon-ftp 1.0-alpha-6
How the plug-in computes the description of the bundle
The description of the bundle comes from three different sources:
- Bindex : Bindex is a tool that analyzes a bundle manifest to generate OBR description
- pom.xml : by analyzing the pom file, various information is collected (symbolic name ...)
- obr.xml : this file contains customized description and capabilities for the bundle
These sources are merged together using the following precedence:
Bindex | (overrides) pom.xml | (overrides) obr.xml
A warning message is displayed when existing information is overridden.