获取 maven 版本:准备使用版本号更新属性
Posted
技术标签:
【中文标题】获取 maven 版本:准备使用版本号更新属性【英文标题】:Get maven release:prepare to update a property with the version number 【发布时间】:2020-04-17 09:53:10 【问题描述】:由于各种原因,我的 pom 文件中有一个属性必须与版本号匹配:
<project>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<!-- can't use $project.version here, as that is calculated lazily based on the final jar project! -->
<artifact-version>1.2.3-SNAPSHOT</artifact-version>
</properties>
</project>
当我运行release:prepare
时,发布插件会更新项目版本,但不会更新artifact-version
属性。我需要将此属性更新到发布版本,然后更新到下一个快照版本以及项目属性。
我也不想在树中添加任何额外的提交来手动更新属性,我希望这一切都作为发布准备和执行步骤的一部分完成。
如何获取发布插件来更新此属性以及版本号?
版本属性
我们需要属性和版本字段的原因是因为在我们到达构建 jar 的 pom 之前,我们有几个嵌套的聚合器 pom 层。我们使用<dependencyManagement>
来保持依赖关系相同,但通常我们需要在父pom 中覆盖一个<dependencyManagement>
依赖关系,并在中间pom 中覆盖一个(例如添加排除项)。
Maven 不允许 <dependencyManagement>
中的工件引用从父 <dependencyManagement>
继承版本,因此我们需要重新指定版本号。我们不能使用$project.version
,因为它解析为子项目的版本号。因此使用一个属性来存储版本号,这需要与这里的发布版本相匹配。
【问题讨论】:
1.我非常怀疑您能否说服发布插件执行此操作。 2、能否解释一下“各种原因”?也许我们会找到一种更好的方法来使这些版本保持同步。 此外,$project.version
是在非常接近构建开始时计算出来的,无论如何在解决任何手写属性之前。
我在关于版本属性的问题中添加了更多信息
所以你的一些 jar-building-pom 的版本号与父版本不同?
是的 - 这是在一个超级 pom 中,它有一个特定的版本号,以及一些与之关联的共享该版本号的共享库。超级 pom 然后被几个不同的项目用作父级,具有自己的版本号。这些项目可以有自己的聚合器 pom,可能需要从 super-pom 重新指定 <dependencyManagement>
依赖项
【参考方案1】:
你可以让maven-release-plugin
运行versions:set-property
:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<artifact.version>1.2.3-SNAPSHOT</artifact.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<preparationGoals>clean versions:set-property verify</preparationGoals>
<completionGoals>versions:set-property</completionGoals>
<arguments>-Prelease-super</arguments>
</configuration>
<inherited>false</inherited>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release-super</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version><!-- at least 2.5 -->
<configuration>
<property>artifact.version</property>
<newVersion>$project.version</newVersion>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
必须指定属性名称和值(具有动态值);这些是在versions-maven-plugin
的<configuration>
中设置的。
因为我不想扩展项目来继承它,所以我使用inherited>false
和一个在发布期间激活的<profile>
。这在 artifact
也是 aggregator 项目时有效;子模块还需要版本配置不会阻塞set-property
(尽管它对它们没有影响)。 (如果artifact
是一个单模块项目,它可以在两个插件上使用<inherited>false
而不是配置文件。)
【讨论】:
以上是关于获取 maven 版本:准备使用版本号更新属性的主要内容,如果未能解决你的问题,请参考以下文章