Maven,如何继承属性到模块
问题描述:
在maven中,是否可以从POM继承配置到它的模块,而不必将其声明为子模块POM中的父级?Maven,如何继承属性到模块
更具体地说,我想从包含模块的POM继承版本号。如果我从声明父文件中得到它,我必须将父版本号硬编码到每个模块。如果我更新父POM,我也必须更新所有模块POM。这是由于维护原因我想避免的重复信息。我希望所有模块的父代版本号相同,并且在父代中只定义版本。
答
我想有所有的模块相同的版本号为父母,只有在父定义版本。
您应定义在父pom.xml的版本
<groupId>stackoverflow</groupId>
<artifactId>stackoverflow</artifactId>
<packaging>pom</packaging>
<version>2.0.2</version>
....
<modules>
<module>sample</module>
<module>inherit</module>
</modules>
,并在你的子模块进一步只需使用
<parent>
<artifactId>stackoverflow</artifactId>
<groupId>stackoverflow</groupId>
<version>2.0.2</version>
</parent>
<artifactId>sample</artifactId>
和
<parent>
<artifactId>stackoverflow</artifactId>
<groupId>stackoverflow</groupId>
<version>2.0.2</version>
</parent>
<artifactId>inherit</artifactId>
这意味着子模块sample
和inherit
与其父母stackoverflow
具有相同的版本。
+0
你没有明白我的观点。在这两种情况下,我仍然必须在子模块中指定版本。作为父项的工件或版本的版本。我仍然直接或间接地在每个子模块中指定版本。理想情况下,我希望将版本从父级传播到模块,而无需在模块中指定父级版本。为什么maven不能仅仅从POM提供给它的模块呢? –
*如果我更新父POM,我必须更新所有模块POM的*,只有当您希望该更改传播到所有继承**的子模块**时才会更新。 – nullpointer
通常您不应该手动更改版本号。使用maven-release-plugin或versions-maven-plugin来更改版本......这使得它更容易... – khmarbaise