Use dependencies from Eclipse p2 repository in a regular Maven build?

otto.poellath picture otto.poellath · Jul 13, 2011 · Viewed 11k times · Source

I'd like to use dependencies from a remote Eclipse p2 repository in a "regular" Maven 3 build (e.g. JAR or WAR packaging) - all without converting the p2 repo to a local Maven repo (which is what osgi-to-maven2 and m4e seem to do).

Ideally, I'd just use http://maven.eclipse.org/nexus/, but that doesn't (yet?) contain many bundles.

Using Maven's systemPath doesn't count!

Answer

otto.poellath picture otto.poellath · Jul 15, 2011

Looks like I'm answering another of my own questions..

First of all, you can use Tycho to add a dependency on a bundle from a P2 repository:

  • Configure the build to use tycho-maven-plugin.
  • Specify a P2 repository.
  • Set packaging to "eclipse-plugin".
  • Create a manifest for your build and use Require-Bundle to state the dependency (e.g. org.eclipse.xsd). Also set Bundle-Version to the same version we used for our build in pom.xml.

Let's give that a try:

$ mvn dependency:tree
[INFO] com.example:org.eclipse.xsd:eclipse-plugin:0.0.1
[INFO] +- p2.eclipse-plugin:org.eclipse.xsd:jar:2.6.0.v20100914-1218:system
[INFO] ...
[INFO] \- p2.eclipse-plugin:org.eclipse.core.filesystem:jar:1.3.1.R36x_v20100727-0745:system

Our dependency has sucessfully been resolved from the P2 repository. Unfortunately, we're not done yet. The dependency has been added with system scope, which means that the artifacts won't be included if we create a webapp that depends on our build. To work around that, we'll first unpack all classes contained in the dependency to some directory, and then re-package that directory as a jar and use it as the final artifact of our build.

For the first part (unpacking), we add the maven-dependency-plugin to our build and configure it to run its unpack-dependencies goal during the package phase. For the second part (re-packaging), we add the maven-assembly-plugin to our build and configure it to run its single goal during the package phase. We also need to create and configure a custom assembly descriptor.

Our build now consists of 3 files: The build file in pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>org.eclipse.xsd</artifactId>
    <version>0.0.1</version>
    <packaging>eclipse-plugin</packaging>
    <repositories>
        <repository>
            <id>helios</id>
            <layout>p2</layout>
            <url>http://download.eclipse.org/releases/helios</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>0.12.0</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/dependency</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>true</overWriteSnapshots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/repackaged.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The manifest in META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ignored
Bundle-SymbolicName: Ignored
Bundle-Version: 0.0.1
Require-Bundle: org.eclipse.xsd
Bundle-Vendor: Ignored

The assembly descriptor in src/main/assembly/repackaged.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>repackaged</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
      <fileSet>
        <directory>${project.build.directory}/dependency/</directory>
        <outputDirectory>/</outputDirectory>
        <useDefaultExcludes>true</useDefaultExcludes>
      </fileSet>
    </fileSets>
</assembly>

Now mvn package will create a jar file that contains all the code from our P2 dependency, re-packaged as a proper Maven artifact, ready to be used as a dependency in another project.

$ jar tf target/org.eclipse.xsd-0.0.1-repackaged.jar 
org/eclipse/xsd/ecore/XSDEcoreBuilder$1.class
org/eclipse/xsd/ecore/XSDEcoreBuilder$2.class
org/eclipse/xsd/ecore/XSDEcoreBuilder$Comparator.class
org/eclipse/xsd/ecore/XSDEcoreBuilder$EffectiveOccurrence.class
org/eclipse/xsd/ecore/XSDEcoreBuilder.class