Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

properties - Maven: property substitution not done for /project/version tag of pom?

http://maven.apache.org/pom.html#Properties says property "values are accessible anywhere within a 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>
    ...

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>

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.

However if I hardcode version in the parent to 8 as well, rather than using ${myversion}, then everything works fine.

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?

Regards,

/George

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Property substitution is not allowed in /project/parent/(groupId|artifactId|version) or in /project/(groupId|artifactId|version) by design in Maven 2.x.

So the rules are:

  • 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
  • use ${project.groupId} and ${project.version} for inter module dependencies.

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.

Version less parent will be allowed in Maven 3.1.

See also


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...