The Maven Cookbook

1.2. Generating an OSGi Project with Maven

1.2. Generating an OSGi Project with Maven

1.2.1. Task

You need to create a Maven multi-module project that allows you to develop a modular, OSGi-based web application.

1.2.2. Action

Use the Maven Pax Plugin from OPS4J, and call the create-project goal. The following command-line will create a multi-module project with a groupId of org.sonatype.mcookbook, an artifactId of osgi-project, and a version of 1.0-SNAPSHOT:

~/examples/osgi $ mvn org.ops4j:maven-pax-plugin:create-project \
  -DgroupId=org.sonatype.mcookbook \
  -DartifactId=osgi-project \
  -Dversion=1.0-SNAPSHOT
[INFO] Scanning for projects...
[INFO] artifact org.ops4j:maven-pax-plugin: checking for updates from central
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [org.ops4j:maven-pax-plugin:create-project] (aggregator-style)
[INFO] ------------------------------------------------------------------------
...
[INFO] [pax:create-project]
[INFO] Selecting latest archetype release within version range [1,2)
[INFO] artifact org.ops4j.pax.construct:maven-archetype-osgi-project: checking for updates from central
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating Archetype: maven-archetype-osgi-project:1.0.3
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: packageName, Value: org.sonatype.mcookbook.osgi-project
[INFO] Parameter: archetypeVersion, Value: 1.0.3
[INFO] Parameter: groupId, Value: org.sonatype.mcookbook
[INFO] Parameter: archetypeArtifactId, Value: maven-archetype-osgi-project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: archetypeGroupId, Value: org.ops4j.pax.construct
[INFO] Parameter: basedir, Value: ~/examples/osgi
[INFO] Parameter: package, Value: org.sonatype.mcookbook.osgi-project
[INFO] Parameter: artifactId, Value: osgi-project
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] Archetype created in dir: ~/examples/osgi/osgi-project

Once you've generated an OSGi project using the Pax Plugin, you will have the following directory structure:

Project Structure Created by the OPS4J Pax Plugin

Figure 1.1. Project Structure Created by the OPS4J Pax Plugin


If you want to verify that you can build the project successfully, run mvn clean install:

~/examples/osgi/osgi-project $ mvn clean install
[INFO] Reactor build order: 
[INFO]   org.sonatype.mcookbook.osgi-project (OSGi project)
[INFO]   osgi-project - plugin configuration
[INFO]   osgi-project - wrapper instructions
[INFO]   osgi-project - bundle instructions
[INFO]   osgi-project - imported bundles
[INFO] ------------------------------------------------------------------------
[INFO] Building org.sonatype.mcookbook.osgi-project (OSGi project)
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [site:attach-descriptor]
[INFO] [install:install]
[INFO] Installing ~/examples/osgi/osgi-project/target/pom-transformed.
xml to ~/.m2/repository/org/sonatype/mcookbook/osgi-project/1.0-SNAPS
HOT/osgi-project-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Building osgi-project - plugin configuration
[INFO]    task-segment: [clean, install]
...

Note

Instead of executing these goals manually, you can also download and install the Pax-Construct scripts which can be used to automate the process of creating a new OSGi project with the Pax plugin. For more information, see the Pax Construct Quickstart page.

1.2.3. Detail

The generated multi-module project structure contains a parent project with the supplied groupId, artifactId, and version, and a few submodules:

osgi-project/pom.xml

This is the parent POM

osgi-project/poms/compiled/pom.xml

This POM serves as the parent POM to all of the compiled OSGi components you will add to this osgi-project Maven project.

osgi-project/poms/wrappers/pom.xml

If you can't find a particular library as an OSGi component, the Pax Construct tools allow you to wrap an existing dependency artifact into an OSGi bundle. The configuration for these wrapped bundles is stored in this POM.

osgi-project/provision/pom.xml

This POM configures the Apache Felix runtime environment that you can start by running the pax:provision goal. If you need to import a bundle into your runtime environment, this is the POM that contains a reference to the corresponding Maven dependency.

Note

The osgi-project/runner directory is not created until you run the pax:provision goal as shown in Section 1.3, “Starting an OSGi Container”.