If you already have a Maven project and you want to modify it so that it generates an OSGi bundle, perform the following steps:
Configure Maven to generate an OSGi bundle by changing the package type to
bundle
in your project's pom.xml
file. Change the
contents of the packaging
element to bundle
, as shown in
the following example:
<project ... >
...
<packaging>bundle</packaging>
...
</project>
The effect of this setting is to select the Maven bundle plug-in,
maven-bundle-plugin
, to perform packaging for this project. This
setting on its own, however, has no effect until you explicitly add the bundle
plug-in to your POM.
To add the Maven bundle plug-in, copy and paste the following sample
plugin
element into the project/build/plugins
section
of your project's pom.xml
file:
<project ... >
...
<build>
<defaultGoal>install</defaultGoal>
<plugins>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
Where the bundle plug-in is configured by the settings in the
instructions
element. For a general guide to customizing bundle
instructions, see Configuring the Bundle Plug-In, and for specific
recommendations, see Packaging a Web Service in a Bundle and Packaging Routes in a Bundle.
It is almost always necessary to specify the JDK version in your POM file. If your
code uses any modern features of the Java language—such as generics, static
imports, and so on—and you have not customized the JDK version in the POM,
Maven will fail to compile your source code. It is not
sufficient to set the JAVA_HOME
and the PATH
environment
variables to the correct values for your JDK, you must also modify the POM
file.
To configure your POM file, so that it accepts the Java language features
introduced in JDK 1.5, add the following maven-compiler-plugin
plug-in
settings to your POM (if they are not already present):
<project ... >
...
<build>
<defaultGoal>install</defaultGoal>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
If you are using JDK 1.6, you can change the contents of the source
element and the target
element to 1.6
. But this has the
drawback that you would be unable to deploy the artifact on JDK 1.5. Typically, it
is more convenient to build on JDK 1.5, which produces an artifact that can be
deployed either on JDK 1.5 or on JDK 1.6.