90

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.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
whaley
  • 16,075
  • 10
  • 57
  • 68

4 Answers4

96

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.

Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 2
    Just seeing your bolded answer documented here: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance . It was mentioned that way in Maven: The Definitive guide, but I glossed over that at the time I read it. Thanks for the correction. – whaley Dec 30 '09 at 21:03
  • 14
    The examples in http://jira.codehaus.org/browse/MNG-4715 seem to have some valid reasons for using a property for the element, so I'm not convinced there's no use for that, but +1 for reminding us that in the case you want submodules to use the parent pom version, just remove the tag and let inheritance work. – metamatt Apr 08 '11 at 23:56
  • 1
    As of Spring Roo 1.3.0.BUILD-SNAPSHOT if you remove the ``-tag from a child pom, it would start complaining that `Version is required for ...`. But that's Spring Roo's fault, not Maven's. Maven is working just fine without the version-tag. – Vladimir Tsvetkov Apr 17 '12 at 18:41
  • 8
    That's stupid :-(. How do I use ${buildNumber} from buildnumber-maven-plugin in version? – Nux Aug 22 '14 at 10:55
  • 6
    No! In Maven version 3.2.1 remove the version tag from the child poms, result in 'project build error: parent.version is missing' – Junchen Liu Jun 22 '15 at 13:12
  • 1
    @shanyangqu Is right, the answer is wrong. You cannot remove the tag that lies in the tag. – toni07 Oct 14 '15 at 13:19
  • 6
    @shanyangqu. Just to be clear here. You are correct to say that you can't remove the version from the *parent* section of the child pom. That's needed to specify the parent that you're inheriting from. But you *can* leave out the version of the child itself. It will default to the parent.version (the version in the parent section). – drrob Dec 07 '15 at 12:04
  • @drrob so how to get rid of those nasty Maven warnings in such a case? While I'm releasing new versions I want to have my child modules always use the latest parent version. That's why I'm using a property for the version in the parent tag. And Maven is complaining about it. How can one get rid of those warnings? – omni Jul 02 '16 at 10:22
  • @masi hardcode the version number in the parent section, don't use a property. Then use the release and versions plugins to update all modules – drrob Jul 03 '16 at 11:39
  • Also, parent is a relative concept : who is the parent in your tag when is extended once again ? it is the parent of the n+2 child, or the parent of the n+1 child (where it was declared) ? Depending on how maven handle inheritance, you could get unexpected result. – Thierry Jul 28 '17 at 11:56
  • I don't agree with this answer. For each child project we need to set parent pom's version. Fortunately this is only a warning and maven is compiling. – Davut Gürbüz Jan 30 '19 at 14:44
  • You can also visit this maven page,even maven documentation offer this option but gives warning :) https://maven.apache.org/maven-ci-friendly.html#Multi_Module_Setup – Davut Gürbüz Jan 30 '19 at 14:58
20

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

S.Mandal
  • 172
  • 1
  • 10
1

It seems that the warning is correct. See MNG-4717: "the pom that gets deployed will not have the property value resolved, so anyone depending on that pom will pick up the dependency as being the string uninterpolated with the ${ } and much hilarity will ensue in your build process." "However, if one uses flatten-maven-plugin the deployed pom gets a resolved value."

Jonathan Rosenne
  • 2,159
  • 17
  • 27
0

I have a multi-module app. In the sub-module, I changed

    <parent>
        <groupId>com.microservices</groupId>
        <artifactId>microservices</artifactId>
        <version>1.1</version>
        <packaging>pom</packaging>
   </parent>

to

    <parent>
        <groupId>com.microservices</groupId>
        <artifactId>microservices</artifactId>
        <version>{parent.version}</version>
        <packaging>pom</packaging>
   </parent>

That fixed it for me.

brohjoe
  • 854
  • 4
  • 17
  • 39