Java 使 Maven 子 poms 与父 poms 具有相同的版本号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24416002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:05:09  来源:igfitidea点击:

Make Maven child poms have same version number as parent?

javamaven

提问by user3120173

I have a multi-module (aggregator) Maven project that also inherits properties from the parent pom. So in my parent pom.xml I have this:

我有一个多模块(聚合器)Maven 项目,它也从父 pom 继承属性。所以在我的父 pom.xml 我有这个:

<groupId>com.something.project</groupId>
<artifactId>MyProject</artifactId>
<packaging>pom</packaging>
<version>1.0.123</version>

<modules>
    <module>ModuleA</module>
    <module>ModuleB</module>
    <module>ModuleC</module>
</modules>

I would like to have all the child modules inherit the parent version number 1.0.123, but the child pom.xmlrequires me to go ahead and hardcode the version number of the parent anyway (ModuleA pom.xml example):

我想让所有子模块继承父版本号1.0.123,但是子模块pom.xml要求我继续硬编码父模块的版本号(ModuleA pom.xml 示例):

<artifactId>ModuleA</artifactId>
<name>Some Amazing Thing</name>

<parent>
    <relativePath>../</relativePath>
    <artifactId>MyProject</artifactId>
    <version>1.0.123</version>
    <groupId>com.something.project</groupId>
</parent>

How can I just put the version number in the parent pom, and have the modules inherit it without having to update it in multiple places? So far I've tried ${project.version} and ${project.parent.version} and Maven seems very unhappy with that. Nor can I remove the version reference entirely.

我怎样才能把版本号放在父 pom 中,让模块继承它而不必在多个地方更新它?到目前为止,我已经尝试过 ${project.version} 和 ${project.parent.version} 并且 Maven 似乎对此非常不满意。我也不能完全删除版本参考。

Does anyone have a good answer for this?

有没有人对此有一个很好的答案?

采纳答案by jtahlborn

In general, if you are using maven to manage the versions, it isn't a problem to put the parent version in the sub-module pom. when you run the maven release plugin, it will update all the versions in all the sub-modules for you.

一般情况下,如果你使用maven来管理版本,把父版本放在子模块pom中是没有问题的。当您运行 maven 发布插件时,它会为您更新所有子模块中的所有版本。

UPDATE:

更新:

if you want to manage versions on your own, then you will probably be in for a bit of pain. i'm pretty sure you will have to specify the version number in each pom at least once. you can probably simplify things for yourself by writing a few scripts which do the recursive search and replace for you (instead of doing it manually).

如果您想自己管理版本,那么您可能会有点痛苦。我很确定您必须至少在每个 pom 中指定版本号一次。您可以通过编写一些脚本来为自己简化事情,这些脚本为您执行递归搜索和替换(而不是手动执行)。

As a bit of side context: maven is built with a "best(maven) way of doing things" mentality. if you follow that way, things generally tend to be really simple and work reallywell. doing things differently is often possible, but the further you get from the "best way", the more painful (or impossible) things will be.

作为一个侧面的背景:maven 是用“最好的(maven)做事方式”的心态构建的。如果您遵循这种方式,事情通常会变得非常简单并且工作很好。以不同的方式做事通常是可能的,但是您离“最佳方式”越远,事情就越痛苦(或不可能)。

回答by Sridivakar

I think this is already been answered @ Can you inherit the version from the parent POM in Maven?

我认为这已经得到了回答@ 你能从 Maven 的父 POM 继承版本吗?

I tried the solution, it worked for me (using Apache Maven 3.0.5). But, it has limitation, that if we go directly to child pom and build, it won't resolve the version-variable.

我尝试了解决方案,它对我有用(使用 Apache Maven 3.0.5)。但是,它有局限性,如果我们直接进入子 pom 并构建,它不会解析版本变量。