Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

maven

maven-3

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.


I might be late here to discuss on this. I got a simple solution for this WARNING.

First of all, if you want that all child modules will take same version as parent, then you just remove <version> tag from child POM and as you include <parent> in child POM, that should be there.

In absence of <version> in child POM, it will automatically take Parent POM version.

Now if you want to use property in parent POM version and want to get the same in all child-modules, you can go through as follow.

There is no limitation on using property in <version> part of parent or child POM. But if you use your own xml tag for specifying that or you use your own property, then WARNING comes, (although this is just warning, everything works as expected).

But if you want to get rid of this WARNING, you can follow these steps:

  1. Create <properties> inside POM.xml as below

    <properties>
        <revision>1.0.0</revision>  <!-- Put your version -->
    </properties>
    
  2. In <version> of the POM.xml, put as follow

    <version>${revision}</version>
    

Sample code snippet (for multi-module project):

<groupId>abc.xyz</groupId>
<artifactId>pqr</artifactId>
<!-- <version>1.0.0</version> -->
<version>${revision}</version>
<packaging>pom</packaging>
<description>Parent POM</description>

<properties>
    <revision>1.0.0</revision>
</properties>

Note: Instead of <revision>, if you use any other name (for example, <my.version>), you will face that WARNING

Now if you want to pass version during mvn deploy, you can use mvn deploy "-Drevision=1.0.0-SNAPSHOT" and similarly for mvn install also.

Now if above configuration, you want to use as Parent POM, and you want to use same version in all child module, that can also be done. In each child module POM, use below

<parent>
    <groupId>abc.xyz</groupId>
    <artifactId>Parent</artifactId>
    <!-- <version>1.0.0</version> -->
    <version>${revision}</version>
</parent>

<groupId>abc.xyz</groupId>
<artifactId>Child</artifactId>
<!-- <version>1.0.0</version> -->     <!-- Automatically inherit parent POM version -->
<name>Demo</name>

For reference, you can go through maven multi module setup