Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 479
George Hawkins Avatar asked Sep 10 '10 12:09

George Hawkins


People also ask

What happens if you don't specify version in POM XML?

of your question: If you don't specify a version there are various different outcomes: a) you will get an error... b) if you have defined the version in the dependency management of the project's parent's pom, then that version is taken. The parent of the project doesn't have to be an enclosing superproject.

How do I change the version of POM XML?

mvn scm:checkin -Dincludes=pom. xml -Dmessage="Setting version, preping for release." Then you can perform your release (I recommend the maven-release-plugin), after which you can set your new version and commit it as above. The versions plugin is your friend.

Where does property tag go in POM XML?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.


1 Answers

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

  • MNG-624
like image 101
Pascal Thivent Avatar answered Oct 21 '22 16:10

Pascal Thivent