views:

28

answers:

1

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

+2  A: 

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

Pascal Thivent