DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
Bundle Plugin for Maven
This plugin for Maven 2 is based on the BND tool from Peter Kriens. The way BND works is by treating your project as a big collection of classes (e.g., project code, dependencies, and the class path). The way you create a bundle with BND is to tell it the content of the bundle's JAR file as a subset of the available classes. This plugin wraps BND to make it work specifically with the Maven 2 project structure and to provide it with reasonable default behavior for Maven 2 projects.
...
| Anchor | ||||
|---|---|---|---|---|
|
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:
...
The <Export-Package> and <Private-Package> instructions tell the plugin about the contents of the resulting bundle JAR file. The <Export-Package> instruction tells the plugin which of the available packages to copy into the bundle and export, while the <Private-Package> instruction indicates which of the available packages to copy into the bundle but not export. If the two sets overlap, as they do in the case, then the export takes precedence. Since we did not specify any values for any other bundle manifest headers, they will assume default values which are described below. One specific behavior to highlight is that the plugin generates the Import-Package bundle manifest header based on the contents of the bundle, which means that you generally do not ever need to explicitly specify it yourself. That's it.
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:
...
| Anchor | ||||
|---|---|---|---|---|
|
Instructions
<Export-Package>
The <Export-Package> instruction is a list of packages for the bundle to export. These packages are copied into the resulting bundle JAR file from the available classes (i.e., project classes, dependencies, and class path); thus, it is possible to include classes into your bundle that are not associated with source files in your project. <Export-Package> can be specified with package patterns using the '*' wildcard. Also, it is possible to exclude packages using negation by starting the package pattern with '!'. Thus, non-negated patterns indicate which of the available packages to include in the bundle, whereas negated patterns indicate which should not be included in the bundle.
...
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.
<Private-Package>
The <Private-Package> instruction is similar in every way to the <Export-Package> instruction, except for the fact that these packages will not be exported by the bundle. If a package is selected by both the export and private package headers, then the export takes precedence.
<Include-Resource>
The <Include-Resource> instruction is a list of arbitrary resources that should be copied into the bundle JAR file. The specified resources are declared as clauses that can have the following forms:
...
By default the bundle plugin converts the project's Maven resource directories into a single <Include-Resource> instruction. If you specify your own <Include-Resource> instruction, this will replace the generated one. To include the generated list of Maven resources in your own <Include-Resource> instruction just add {maven-resources} to the list and it will be expanded automatically.
<Import-Package>
The <Import-Package> instruction is a list of packages that are required by the bundle's contained packages. The default for this header is "*", resulting in importing all referred packages. This header rarely has to be explicitly specified. However, in certain cases when there is an unwanted import, such an import can be removed by using a negation package pattern. The package patterns work in the same way as for <Export-Package>, which means they are ordered. For example, if you wanted to import all packages except org.foo.impl you would specify "!org.foo.impl,*"
| Anchor | ||||
|---|---|---|---|---|
|
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:
...
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.
| Anchor | ||||
|---|---|---|---|---|
|
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:
...
There are two main things to note: (1) the <packaging> specifier must be "bundle" and (2) the plugin and configuration must be specified (the configuration section is where you will issue instructions to the plugin).
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:
...
| No Format |
|---|
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:
| Code Block | ||||
|---|---|---|---|---|
| ||||
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
|
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 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.
| No Format |
|---|
$ 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:
...
The default goal bundle will be initialized by setting the <packaging> entry to "bundle".
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 <Embed-Dependency> instruction:
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
<Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency> |
Embed-Dependency and Export-Package
If you embed a dependency with <Embed-Dependency>, and your <Export-Package> or <Private-Package> instructions match packages inside the embedded jar, you will see some duplication inside the bundle. This is because the <Export-Package> and <Private-Package> instructions will result in classes being inlined in the bundle, even though they also exist inside the embedded jar. If you want to export packages from an embedded dependency without such duplication then you can either inline the dependency, or use a new BND instruction called <_exportcontents>.
<_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:
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<obrRepository>NONE</obrRepository>
<instructions>
<!-- bnd instructions -->
</instructions>
</configuration>
|
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:
...
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).
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<unpackBundle>true</unpackBundle>
<instructions>
<!-- bnd instructions -->
</instructions>
</configuration>
|
Using an existing MANIFEST.MF file
If you have an existing manifest, you can add this to the Bnd instructions, like so:
...
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 run ant:ant to create the standard Ant support tasks to download Maven dependencies and perform compilation, etc.
...
| No Format |
|---|
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.
...
| No Format |
|---|
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 <distributionManagement> section of the project, unless -DaltDeploymentRepository is set. See http://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html for more details about these particular settings.
...
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. See http://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html for more details about these particular settings.
...
| No Format |
|---|
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.
...
| No Format |
|---|
mvn bundle:clean |
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 <ignoreLock>true<ignoreLock> to the configuration section of your Pom).
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:
| Code Block | ||||
|---|---|---|---|---|
| ||||
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>
|
How the plug-in computes the description of the bundle
The description of the bundle comes from three different sources:
...
A warning message is displayed when existing information is overridden.
Known issues & limitations
- obr.xml (file given by the user to add properties not found by Bindex) must be correct, because the plug-in does not check its syntax.
Feedback
Subscribe to the Felix users mailing list by sending a message to users-subscribe@felix.apache.org; after subscribing, email questions or feedback to users@felix.apache.org.