Maven:未对pom的/project/version标记进行属性替换? [英] Maven: property substitution not done for /project/version tag of pom?
问题描述
http://maven.apache.org/pom.html#Properties 说属性可以在POM中的任何位置访问值".
http://maven.apache.org/pom.html#Properties says property "values are accessible anywhere within a POM".
这应该是在POM中的最个地方都可以访问"吗?
Should this read "are accessible in most places within a POM"?
我可以这样指定依赖项的版本:
I can specify the version of a dependency no problem like so:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
但是项目本身的版本是这样的:
But what about the version of the project itself like so:
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>${myversion}</version>
<properties>
<myversion>8</myversion>
</properties>
<modules>
<module>alpha</module>
<module>beta</module>
</modules>
...
如果我尝试使用此< version>,则不会采用值8.在这里,我在pom中定义了$ {myversion},但是如果在命令行上指定-Dmyversion = 8,情况似乎也是如此.
If I try this <version> will not take the value 8. Here I've defined ${myversion} in the pom but the same seems to be the case if I specify -Dmyversion=8 on the command line.
如果其中一个模块使用硬编码版本号指定其父级,如下所示:
If one of the modules specifies its parent with a hardcoded version number like so:
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>8</version>
</parent>
当我尝试构建时,当maven来查看模块的pom时,它将说无法找到版本8的给定父pom.
When I try to build then when maven comes to look at the module's pom it will say it cannot find the given parent pom with version 8.
但是,如果我也将父级中的版本也硬编码为8,而不是使用$ {myversion},则一切正常.
However if I hardcode version in the parent to 8 as well, rather than using ${myversion}, then everything works fine.
因此对我来说似乎不会对父pom的/project/version标记进行属性替换.
So it seems to me that property substitution does not happen for the /project/version tag of the parent pom.
是这种情况还是对我似乎看到的内容有其他解释?
Is this the case or is there some other explanation for what I seem to be seeing?
此致
/乔治
推荐答案
在Maven 2.x中,按设计不允许在/project/parent/(groupId|artifactId|version)
或/project/(groupId|artifactId|version)
中进行属性替换.
Property substitution is not allowed in /project/parent/(groupId|artifactId|version)
or in /project/(groupId|artifactId|version)
by design in Maven 2.x.
所以规则是:
- 硬编码顶部
project/version
元素中的版本. - 硬编码子级
project/parent/version
元素中的版本. - 子级继承该版本,除非他们想覆盖它
- 因此不需要
${myversion}
属性
- hard code the version in the top
project/version
element. - hard code the version in the
project/parent/version
element of children. - children inherit the version unless they want to override it
- there is thus not need for a
${myversion}
property
您将在maven用户列表中找到无数个与此主题相关的线程(例如,参见
You'll find an infinite number of threads on this topic on the maven user list (see for example Pom Parent Version Properties) and I'll just say that any attempt to workaround the above rules is wrong and doesn't work.
版本小于
parent
的版本将在Maven 3.1中允许使用.Version less
parent
will be allowed in Maven 3.1.这篇关于Maven:未对pom的/project/version标记进行属性替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- there is thus not need for a
- 因此不需要