关于在 Maven 3中使用 project.Parent.version 作为模块版本的警告

在 maven 多模块项目中,我希望每个模块始终保持与父模块相同的版本,我通常在模块的 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>

由于我开始使用 maven 3.0-alpha-5,因此会得到以下警告。

[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]

我很想知道将模块版本与父版本绑定的真正问题是什么(如果有的话) ?或者,当任何表达式(不管它是否是 project.Parent.version)用于 version 元素时,这是一个通用警告。

112851 次浏览

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