几年前,我写了一篇关于如何基于 Maven 工件生成 P2 存储库的博文。所描述的方法现在已经过时了,我想展示一种基于 p2-maven-plugin 的新方法,它的创建正是为了解决这个问题。
首先,我们将 p2-maven-plugin 的目标 site 绑定到 Maven 的生命周期阶段 package。这个目标负责生成 P2 存储库。
<plugin> <groupId>org.reficio</groupId> <artifactId>p2-maven-plugin</artifactId> <version>1.3.0</version> <executions> <execution> <id>default-cli</id> <phase>package</phase> <goals> <goal>site</goal> </goals> <!--... --> </execution> </executions> </plugin>
现在,我们可以定义哪些 Maven 工件应该成为新 P2 存储库的一部分。定义的工件是否已经有 OSGi 清单与 p2-maven-pluging 无关。如果不存在 OSGi 清单,插件将生成一个。
<execution> <!-- ... --> <configuration> <artifacts> <!-- specify your dependencies here --> <!-- groupId:artifactId:version --> <artifact> <id>com.google.guava:guava:jar:23.0</id> <!-- Artifact with existing OSGi-Manifest--> </artifact> <artifact> <id>commons-io:commons-io:1.3</id> <!-- Artifact without existing OSGi-Manifest--> </artifact> </artifacts> </configuration> </execution>
工件由模式 groupId:artifactId:version 指定。如果您想节省一些输入,请使用 MVN 存储库网站 上的Buildr 选项卡复制正确的依赖项声明格式。
此示例配置创建一个 P2 存储库,如下所示:
target/repository ├── artifacts.jar ├── category.xml ├── content.jar └── plugins ├── com.google.code.findbugs.jsr305_1.3.9.jar ├── com.google.errorprone.error_prone_annotations_2.0.18.jar ├── com.google.guava_23.0.0.jar ├── com.google.j2objc.annotations_1.1.0.jar ├── commons-io_1.3.0.jar └── org.codehaus.mojo.animal-sniffer-annotations_1.14.0.jar 1 directory, 9 files
插件的默认行为是,定义的工件的所有传递依赖项也被下载并打包到 P2 存储库中。如果您不想要它,则必须在相应的工件声明中将选项 transitive 设置为 false 。如果您需要 P2 存储库中定义的工件的源代码(如果它们存在于 Maven 存储库中),则必须在相应的目录中将选项 source 设置为 true工件声明。
<!-- ... --> <artifact> <id>com.google.guava:guava:jar:23.0</id> <transitive>false</transitive> <source>true</source> </artifact> <!-- ... -->
然后生成的 P2 存储库如下所示:
target/repository ├── artifacts.jar ├── category.xml ├── content.jar └── plugins ├── com.google.guava.source_23.0.0.jar ├── com.google.guava_23.0.0.jar └── commons-io_1.3.0.jar 1 directory, 6 files
在某些情况下,您希望在所谓的功能 中对工件进行分组。 p2-maven-plugin 提供了一个选项,允许将 Maven 工件直接分组到功能中。工件的定义与上面相同。不同之处在于它必须在相应的功能内部。然后,功能定义需要一些元数据信息,如功能 ID、功能版本、描述等。
<!-- ...--> <configuration> <featureDefinitions> <feature> <!-- Generate a feature including artifacts that are listed below inside the feature element--> <id>spring.feature</id> <version>4.3.11</version> <label>Spring Framework 4.3.11 Feature</label> <providerName>A provider</providerName> <description>${project.description}</description> <copyright>A copyright</copyright> <license>A licence</license> <artifacts> <artifact> <id>org.springframework:spring-core:jar:4.3.11.RELEASE</id>id> </artifact> <artifact> <id>org.springframework:spring-context:jar:4.3.11.RELEASE</id>id> <source>true</source> </artifact> </artifacts> </feature> <!--...--> </featureDefinitions> <!-- ... --> <configuration>
然后生成的 P2 存储库如下所示:
target/repository ├── artifacts.jar ├── category.xml ├── content.jar ├── features │ └── spring.feature_4.3.11.jar └── plugins ├── org.apache.commons.logging_1.2.0.jar ├── org.springframework.spring-aop.source_4.3.11.RELEASE.jar ├── org.springframework.spring-aop_4.3.11.RELEASE.jar ├── org.springframework.spring-beans.source_4.3.11.RELEASE.jar ├── org.springframework.spring-beans_4.3.11.RELEASE.jar ├── org.springframework.spring-context.source_4.3.11.RELEASE.jar ├── org.springframework.spring-context_4.3.11.RELEASE.jar ├── org.springframework.spring-core_4.3.11.RELEASE.jar ├── org.springframework.spring-expression.source_4.3.11.RELEASE.jar └── org.springframework.spring-expression_4.3.11.RELEASE.jar 2 directories, 14 files
当然,这两个选项(生成具有功能和仅具有插件的 p2 存储库)可以混合使用。
p2-maven-plugin 提供了更多选项,如排除特定的传递依赖、引用其他 eclipse 功能等。有关更多信息,请查看 p2-maven-plugin 主页。
现在,我们可以从 Maven 工件生成 P2 存储库。我们缺少如何将此 P2 存储库部署到像 Artifactory 或 Sonatype Nexus 这样的存储库管理器。两个存储库管理器都支持 P2 存储库、专业变体中的 Artifactory(收费)和 OSS 变体中的 Sonatype Nexus(免费)。对于 Nexus,使用 2.x 版本很重要。最新版本 3.x 尚不支持 P2 存储库。
首先,我们希望将生成的 P2 存储库打包到一个 zip 文件中。因此,我们将 tycho-p2-repository-plugin 添加到 Maven 构建生命周期中:
<plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-p2-repository-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>archive-repository</goal> </goals> </execution> </executions> </plugin>
然后,我们必须标记这个 zip 文件,以便 Maven 认识到它必须在部署阶段将其部署到存储库管理器。为此,我们将 build-helper-maven-plugin 添加到 Maven 构建生命周期中。
<!-- Attach zipped P2 repository to be installed and deployed in the Maven repository during the deploy phase. --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>target/${project.artifactId}-${project.version}.zip</file> <type>zip</type> </artifact> </artifacts> </configuration> </execution> </executions> </plugin>
现在,生成的 P2 存储库可以被其他项目访问。有关如何访问 P2 存储库的更多信息,请查看存储库管理器的文档。
可以在 Github 上找到完整的 pom.xml 示例。
标签2: Java教程地址:https://www.cundage.com/article/jcg-generate-p2-repository-maven-artifacts-2017.html