Warning on using project.parent.version as the version of a module in Maven 3

whaley picture whaley · Dec 30, 2009 · Viewed 96.9k times · Source

In maven multi-module projects where I want each of the modules to always keep the same version as the parent, I've typically done something like the following in the module's pom.xml:

  <parent>
    <groupId>com.groupId</groupId>
    <artifactId>parentArtifactId</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <groupId>com.groupId</groupId>
  <artifactId>artifactId</artifactId>
  <packaging>jar</packaging>
  <version>${project.parent.version}</version>
  <name>name</name>

Since I started using maven 3.0-alpha-5, I get the following warning for doing so.

[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.groupid.artifactId:name:jar:1.1-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.groupid.artifactId:name::${project.parent.version}, /Users/whaley/path/to/project/child/pom.xml
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

I'm curious to know what the real problem with tying a module's version to the parent version is, if any? Or is this a case of a general warning when any expression, regardless of whether it's project.parent.version, is used for the version element.

Answer

Pascal Thivent picture Pascal Thivent · Dec 30, 2009

I'm curious to know what the real problem with tying a module's version to the parent version is, if any? Or is this a case of a general warning when any expression, regardless of whether it's project.parent.version, is used for the version element.

Well, that would be easy to test. Because I was curious, I just did it for you using the following pom:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>parent</artifactId>
    <groupId>com.mycompany</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.mycompany</groupId>
  <artifactId>module</artifactId>
  <version>${myversion}</version>
  <name>module</name>
  <url>http://maven.apache.org</url>
  <properties>
    <myversion>1.0-SNAPSHOT</myversion>
  </properties>
  ...
</project>

And maven is indeed complaining:

[WARNING] 'version' contains an expression but should be a constant. @ com.mycompany:module:${myversion}, /home/pascal/Projects/maven-maven3-testcase/module/pom.xml

To be honest, I think that maven is right here, it doesn't make much sense to use a property for the <version> element (at least not for project.version) and it's nice to have maven complaining about it.

And if you want to use the parent pom version in sub-modules, just remove the <version> tag from the child poms, they will inherit the version from the parent. What you are currently doing is unnecessary.