Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 823
otto.poellath Avatar asked Jul 13 '11 16:07

otto.poellath


People also ask

What is P2 repository in Eclipse?

P2 repositories are very common in the eclipse ecosystem. They combine the artifacts (bundles), the meta data and can also contain eclipse features. P2 repositories are generally completely independent of maven repositories and nexus.

How do I add Maven dependencies to an existing project in Eclipse?

Via the Maven index, you can search for dependencies, select them and add them to your pom file. To download the index, select Windows > Preferences > Maven and enable the Download repository index updates on startup option. After changing this setting, restart Eclipse. This triggers the download of the Maven index.

How do I run a dependency in Maven?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

What is P2 Maven plugin?

Welcome to the p2-maven-plugin! This is an easy-to-use Maven3 plugin responsible for the automation of the third-party dependency management in the Eclipse RCP environment.


1 Answers

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 
like image 69
otto.poellath Avatar answered Sep 22 '22 22:09

otto.poellath