本文翻译自: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楼
#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的回答 。