Generate P2 Repository From Maven Artifacts In 2017

Share on:

Some years ago, I wrote a blog post about how to generate a P2 repository based on Maven artifacts. That described approach is obsolete nowadays and I'd like to show a new approach that is based on the p2-maven-plugin that was created to solve exactly this problem.

P2-Maven-Plugin Integration in Maven Build Life Cycle

First at all, we bind the p2-maven-plugin's goal site to the Maven's life cycle phase package. This goal is responsible for the generation of the P2 repository.

 2  <groupId>org.reficio</groupId>
 3  <artifactId>p2-maven-plugin</artifactId>
 4  <version>1.3.0</version>
 5  <executions>
 6    <execution>
 7      <id>default-cli</id>
 8      <phase>package</phase>
 9      <goals>
10        <goal>site</goal>
11      </goals>
12      <!--... -->
13    </execution>
14  </executions>

Generating P2 Repository

Now, we can define which Maven artifacts should be a part of the new P2 repository. It is irrelevant for the p2-maven-pluging if the defined artifacts have already a OSGi manifest or not. If no OSGi manifest exists, the plugin will generate one.

 2<!-- ... -->
 4  <artifacts>
 5    <!-- specify your dependencies here -->
 6    <!-- groupId:artifactId:version -->
 7    <artifact>
 8      <id></id>
 9      <!-- Artifact with existing OSGi-Manifest-->
10    </artifact>
11    <artifact>
12      <id>commons-io:commons-io:1.3</id>
13      <!-- Artifact without existing OSGi-Manifest-->
14    </artifact>
15  </artifacts>

The artifacts are specified by the pattern groupId:artifactId:version. If you want to save some typing, use the Buildr tab on MVN repository website for copying the right dependency declaration format. MVN repository website This sample configuration creates a P2 repository that look like the following one:

 2├── artifacts.jar
 3├── category.xml
 4├── content.jar
 5└── plugins
 6    ├──
 7    ├──
 8    ├──
 9    ├──
10    ├── commons-io_1.3.0.jar
11    └── org.codehaus.mojo.animal-sniffer-annotations_1.14.0.jar
131 directory, 9 files

The default behavior of the plugin is, that all transitive dependencies of the defined artifact are also downloaded and packed into the P2 repository. If you don't want it, then you have to set the option transitive to false in the corresponded artifact declaration. If you need the sources (if they exist in the Maven repository) of the defined artifact in the P2 repository, then you have to set the option source to true in the corresponded artifact declaration.

1<!-- ... -->
3  <id></id>
4  <transitive>false</transitive>
5  <source>true</source>
7<!-- ... -->

Then the generated P2 repository looks like the following one:

 2├── artifacts.jar
 3├── category.xml
 4├── content.jar
 5└── plugins
 6    ├──
 7    ├──
 8    └── commons-io_1.3.0.jar
101 directory, 6 files

Generating P2 Repository With Grouped Artifacts

In some situations, you want to group artifacts in so-called feature. p2-maven-plugin provides an option that allows to group the Maven artifact directly into features. The definition of the artifacts is the same like above. The difference is that it has to be inside the corresponded feature. Then, the feature definition needs some meta data information like feature ID, feature version, description etc.

 1<!-- ...-->
 3  <featureDefinitions>
 4    <feature>
 5      <!-- Generate a feature including artifacts that are listed below inside the feature element-->
 6      <id>spring.feature</id>
 7      <version>4.3.11</version>
 8      <label>Spring Framework 4.3.11 Feature</label>
 9      <providerName>A provider</providerName>
10      <description>${project.description}</description>
11      <copyright>A copyright</copyright>
12      <license>A licence</license>
13      <artifacts>
14        <artifact>
15          <id>org.springframework:spring-core:jar:4.3.11.RELEASE</id>id>
16        </artifact>
17        <artifact>
18          <id>org.springframework:spring-context:jar:4.3.11.RELEASE</id>id>
19          <source>true</source>
20        </artifact>
21      </artifacts>
22    </feature>
23    <!--...-->
24  </featureDefinitions>
25  <!-- ... -->

Then the generated P2 repository looks like the following one:

 2├── artifacts.jar
 3├── category.xml
 4├── content.jar
 5├── features
 6│   └── spring.feature_4.3.11.jar
 7└── plugins
 8    ├── org.apache.commons.logging_1.2.0.jar
 9    ├── org.springframework.spring-aop.source_4.3.11.RELEASE.jar
10    ├── org.springframework.spring-aop_4.3.11.RELEASE.jar
11    ├── org.springframework.spring-beans.source_4.3.11.RELEASE.jar
12    ├── org.springframework.spring-beans_4.3.11.RELEASE.jar
13    ├── org.springframework.spring-context.source_4.3.11.RELEASE.jar
14    ├── org.springframework.spring-context_4.3.11.RELEASE.jar
15    ├── org.springframework.spring-core_4.3.11.RELEASE.jar
16    ├── org.springframework.spring-expression.source_4.3.11.RELEASE.jar
17    └── org.springframework.spring-expression_4.3.11.RELEASE.jar
192 directories, 14 files

Of course both options (generating p2 repository with feature and only with plugins) can be mixed. p2-maven-plugin provides more options like excluding specific transitive dependencies, referencing to other eclipse features and so on. For more information, please look at the p2-maven-plugin homepage. Now, we can generate P2 repositories from Maven artifacts. We lacks of how to deploy this P2 repository to a Repository manager like Artifactory or Sonatype Nexus. Both repository manager supports P2 repositories, Artifactory in the Professional variant (cost money) and Sonatype Nexus in OSS variant (free). For Nexus, it's important that you use the version 2.x. The newest version, 3.x, doesn't yet support P2 repositories.

Deploying P2 Repository to a Repository Manager

First at all, we want that our generated P2 repository is packed into a zip file. Therefore, we add the tycho-p2-repository-plugin to the Maven build life cycle:

 2  <groupId>org.eclipse.tycho</groupId>
 3  <artifactId>tycho-p2-repository-plugin</artifactId>
 4  <version>1.0.0</version>
 5  <executions>
 6    <execution>
 7      <phase>package</phase>
 8      <goals>
 9        <goal>archive-repository</goal>
10      </goals>
11    </execution>
12  </executions>

Then, we have to mark this zip file, so that Maven recognize that it has to deploy it during the deploy phase to a repository manager. For this, we add the build-helper-maven-plugin to the Maven build life cycle.

 1<!-- Attach zipped P2 repository to be installed and deployed in the Maven repository during the deploy phase. -->
 3  <groupId>org.codehaus.mojo</groupId>
 4  <artifactId>build-helper-maven-plugin</artifactId>
 5  <version>3.0.0</version>
 6  <executions>
 7    <execution>
 8      <goals>
 9        <goal>attach-artifact</goal>
10      </goals>
11      <configuration>
12        <artifacts>
13          <artifact>
14            <file>target/${project.artifactId}-${project.version}.zip</file>
15            <type>zip</type>
16          </artifact>
17        </artifacts>
18      </configuration>
19    </execution>
20  </executions>

Now, the generated P2 repository can be addressed by other projects. For more information about how to address the P2 repository, please have a look on the documentation of your repository manager. A whole pom.xml sample can be found on Github.