在多模块Maven项目中更新模块的版本号

本文介绍了如何在多模块Maven项目中统一管理版本号,避免在每个模块的pom.xml中硬编码。建议在父模块中指定<version>,并在子模块中引用。此外,可以使用Versions Maven插件的update-properties目标更新所有模块的版本和依赖。对于自动化版本更新,可以考虑使用Maven的release插件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Updating version numbers of modules in a multi-module Maven project

I have a multi-module maven project. 我有一个多模块的Maven项目。 We intend to version all these modules together. 我们打算将所有这些模块一起版本化。 But as of now I am ending up hard-coding version in each of the module pom.xml as below 但是到目前为止,我将在每个模块pom.xml中结束硬编码版本,如下所示

<parent>
    <artifactId>xyz-application</artifactId>
    <groupId>com.xyz</groupId>
    <version>2.50.0.g</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-Library</artifactId>
<version>2.50.0.g</version>

and the main parent module has the below configuration 并且主父模块具有以下配置

<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-application</artifactId>
<version>2.50.0.g</version>
<packaging>pom</packaging>

#1楼

参考:https://stackoom.com/question/O1fX/在多模块Maven项目中更新模块的版本号


#2楼

The best way is, since you intend to bundle your modules together, you can specify <dependencyManagement> tag in outer most pom.xml (parent module) direct under <project> tag. 最好的方法是,由于您打算将模块捆绑在一起,因此可以直接在<project>标记下的最外部pom.xml (父模块)中指定<dependencyManagement> <project>标记。 It controls the version and group name. 它控制版本和组名。 In your individual module, you just need to specify the <artifactId> tag in your pom.xml . 在您自己的模块中,只需在pom.xml指定<artifactId>标记。 It will take the version from parent file. 它将从父文件中获取版本。


#3楼

The given answer assumes that the project in question use project inheritance in addition to module aggregation. 给定的答案假设所讨论的项目除模块聚合外还使用项目继承。 In fact those are distinct concepts: 实际上,这些是不同的概念:

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation

Some projects may be an aggregation of modules, yet not have a parent-child relationship between aggregator POM and the aggregated modules. 一些项目可能是模块的聚合,但在聚合器POM和聚合的模块之间没有父子关系。 (There may be no parent-child relationship at all, or the child modules may use a separate POM altogether as the "parent".) In these situations the given answer will not work. (可能根本没有父子关系,或者子模块可以完全使用单独的POM作为“父”。)在这些情况下,给定的答案将不起作用。

After much reading and experimentation, it turns out there is a way to use the Versions Maven Plugin to update not only the aggregator POM but also all aggregated modules as well; 经过大量阅读和试验,事实证明,有一种方法可以使用Versions Maven插件不仅更新聚合器POM,而且还更新所有聚合的模块。 it is the processAllModules option. 它是processAllModules选项。 The following command must be done in the directory of the aggregator project: 必须在聚合器项目的目录中执行以下命令:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT -DprocessAllModules

The Versions Maven Plugin will not only update the versions of all contained modules, it will also update inter-module dependencies!!!! Maven版本插件不仅将更新所有包含的模块的版本,还将更新模块间的依赖关系! This is a huge win and will save a lot of time and prevent all sorts of problems. 这是一个巨大的胜利,将节省大量时间并避免各种问题。

Of course don't forget to commit the changes in all modules, which you can also do with the same switch: 当然,不要忘记在所有模块中提交更改,也可以使用同一开关进行更改:

mvn versions:commit -DprocessAllModules

You may decide to dispense with the backup POMS altogether and do everything in one command: 您可能决定完全省去备份POMS,并在一个命令中完成所有操作:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT -DprocessAllModules -DgenerateBackupPoms=false

#4楼

如果要完全自动化该过程(即,您想增加版本号而不必知道当前版本号是什么),则可以执行以下操作:

mvn build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion} versions:commit

#5楼

You may want to look into Maven release plugin's release:update-versions goal. 您可能需要研究Maven版本插件的release:update-versions目标。 It will update the parent's version as well as all the modules under it. 它将更新父级的版本及其下的所有模块。


Update: Please note that the above is the release plugin. 更新:请注意,以上是发布插件。 If you are not releasing, you may want to use versions:set 如果不发布,则可能要使用versions:set

mvn versions:set -DnewVersion=1.2.3-SNAPSHOT

#6楼

Use versions:set from the versions-maven plugin : 使用来自versions-maven插件的 versions:set

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT

It will adjust all pom versions, parent versions and dependency versions in a multi-module project. 它将调整多模块项目中的所有pom版本,父版本和依赖版本。

If you made a mistake, do 如果您输入有误,请执行

mvn versions:revert

afterwards, or 之后,或

mvn versions:commit

if you're happy with the results. 如果您对结果感到满意。


Note: this solution assumes that all modules use the aggregate pom as parent pom also, a scenario that was considered standard at the time of this answer. 注意:此解决方案假定所有模块也将聚合pom用作父pom,在此答案被认为是标准的情况下。 If that is not the case, go for Garret Wilson's answer . 如果不是这种情况,请寻求Garret Wilson的回答

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值