concept Maven in category osgi

appears as: Maven, Maven, The Maven
Enterprise OSGi in Action

This is an excerpt from Manning's book Enterprise OSGi in Action.

Maven considerably simplifies the dependency management required when building with Ant. If Maven is your build tool of choice, you’ll find that the decision about whether to control your manifests directly or generate manifests automatically has been mostly made for you. Although it’s technically possible to use Maven to build bundles while using existing manifests—and the sample code for the earlier chapters of this book did that—it’s not a natural way of using Maven. (If you need convincing, you need only to look at the build scripts packaged with the sample code!)

In many ways, Maven is a natural fit with OSGi, because Maven’s modules and dependencies map relatively neatly to OSGi bundles. Both modules and bundles are uniquely identified by their symbolic names and versions. Maven’s bundle plug-in combines module-level (or bundle-level) dependencies declared in the pom.xml with bnd bytecode analysis to produce OSGi manifests that declare package-level dependencies. The convenient thing about this process is that it involves almost no extra work compared to normal Maven JAR packaging.

If you look at the manifest generated for you by the bundle plug-in, you’ll see an extra imported package, org.osgi.service.blueprint. Where did this import come from? When the Maven bundle plug-in detects that your bundle uses Blueprint, it will automatically add that package dependency. Although this package doesn’t include any code, the Blueprint specification encourages Blueprint bundles to import it to ensure that Blueprint is available in their runtime environment. (The specification also insists that Blueprint implementations must export this package.) If you need your bundle to run in non-Blueprint environments too, you can make the package optional by specifying it explicitly in your pom:

<Import-Package>
  org.osgi.service.blueprint;resolution:=optional
</Import-Package>

If you’re using code-first development, you’ve got a lot of build tools open to you. You can use bnd on its own, you can use it in combination with Ant, or you can use the powerful Maven build plug-in. If you’re doing manifest-first development, you have a similar choice between Ant and Maven. Several tools allow manifest-first building using Ant, although most involve some degree of duplication of information with the IDE. There’s also a nice set of Maven plug-ins that neatly share information with the Eclipse PDE IDE.

Using Maven

Pax Exam is well integrated with Maven, so one of the most convenient ways of specifying bundles to install is using their Maven coordinates and the CoreOptions.maven-Bundle() method. Versions can be explicitly specified, pulled from a pom.xml using versionAsInProject(), or left implicit to default to the latest version.

OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

Or if Maven (http://maven.apache.org/) project metadata is available, you can use the Maven groupId + artifactId to identify the JAR file:

A.2. Building with Maven

You saw in chapter 6 how easy it is to take an existing Ant-based project and migrate it to OSGi with bnd. But what if you use Maven? Maven (http://maven.apache.org) is another popular build tool from Apache that also uses XML to describe builds, but this time the description is declarative rather than procedural. Instead of listing the steps required to build a JAR (also known as an artifact), you list the packaging as jar in the project XML.

Maven favors convention over configuration; follow the Maven way, and you can keep your XML relatively lean and uncomplicated. The problem is when you need to do something special outside of the normal Maven build process. You can end up with pages of XML detailing each step of your customized build.

How well does OSGi fit with Maven? Will you need pages of configuration to assemble your bundle?