Maven:未对 pom 的/project/version 标签进行属性替换? [英] Maven: property substitution not done for /project/version tag of pom?

查看:62
本文介绍了Maven:未对 pom 的/project/version 标签进行属性替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

问候,

/乔治

推荐答案

/project/parent/(groupId|artifactId|version)/project/中不允许进行属性替换(groupId|artifactId|version) 由 Maven 2.x 设计.

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 用户列表上找到关于此主题的无数线程(参见例如 Pom 父版本属性),我只想说,任何解决上述规则的尝试都是错误的,并且不起作用.

      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.

      在 Maven 3.1 中将允许版本更少的 parent.

      Version less parent will be allowed in Maven 3.1.

      这篇关于Maven:未对 pom 的/project/version 标签进行属性替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆