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

wha*_*ley 76 maven-3 maven

在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>
Run Code Online (Sandbox Code Playgroud)

自从我开始使用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]
Run Code Online (Sandbox Code Playgroud)

我很想知道将模块的版本绑定到父版本的真正问题是,如果有的话?或者这是一个一般警告的情况,当任何表达式,无论是否是project.parent.version,用于版本元素.

Pas*_*ent 90

我很想知道将模块的版本绑定到父版本的真正问题是,如果有的话?或者这是一个一般警告的情况,当任何表达式,无论是否是project.parent.version,用于版本元素.

那么,这很容易测试.因为我好奇,我只是使用下面的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>
Run Code Online (Sandbox Code Playgroud)

而且maven确实在抱怨:

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

说实话,我认为maven就在这里,对<version>元素使用属性没有多大意义(至少不适用于project.version),让maven抱怨它很好.

如果您想在子模块中使用父pom版本,只需<version>从子poms中删除标记,它们将从父级继承该版本.你目前正在做的事情是没用的.

  • http://jira.codehaus.org/browse/MNG-4715中的示例似乎有一些正确的理由为<version>元素使用属性,所以我不相信它没有用,但+1提醒我们,如果您希望子模块使用父pom版本,只需删除标记并让继承工作. (12认同)
  • 这是愚蠢的:-(.如何在版本中使用buildnumber-maven-plugin中的$ {buildNumber}? (7认同)
  • 没有!在Maven版本3.2.1中从子poms中删除version标签,导致'project build error:parent.version is missing' (6认同)
  • @shanyangqu.在这里要清楚.你说你不能从子pom的*parent*部分删除版本是正确的.这需要指定您继承的父级.但你*可以*遗漏孩子本身的版本.它将默认为parent.version(父节中的版本). (4认同)
  • 只需在此处记录您的粗体答案:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance.在Maven:The Definitive指南中提到了这种方式,但在我阅读它的时候,我忽略了它.谢谢你的纠正. (2认同)

S.M*_*DAL 5

我可能会迟到在这里讨论这个。我得到了一个简单的解决方案WARNING

首先,如果您希望所有子模块都采用与父模块相同的版本,那么您只需<version>从子 POM 中删除标记,并且当您包含<parent>在子 POM 中时,它应该在那里。

如果没有<version>in child POM,它将自动采用 Parent POM version

现在,如果您想property在父 POM 版本中使用并希望在所有子模块中使用相同的版本,您可以按照以下步骤进行。

<version>父或子 POM 的一部分中使用属性没有限制。但是,如果您使用自己的 xml 标记来指定它,或者使用自己的属性,那么WARNING就会出现(尽管这只是警告,但一切都按预期进行)。

但是如果你想摆脱这个WARNING,你可以按照以下步骤操作:

  1. <properties>在 POM.xml 中创建如下

    <properties>
        <revision>1.0.0</revision>  <!-- Put your version -->
    </properties>
    
    Run Code Online (Sandbox Code Playgroud)
  2. <version>POM.xml 中,放置如下

    <version>${revision}</version>
    
    Run Code Online (Sandbox Code Playgroud)

示例代码片段(用于多模块项目):

<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>
Run Code Online (Sandbox Code Playgroud)

注意:代替<revision>,如果您使用任何其他名称(例如,<my.version>),您将面临WARNING

现在,如果您想在 期间传递版本mvn deploy,您也可以使用mvn deploy "-Drevision=1.0.0-SNAPSHOT"和 类似地mvn install

现在如果上面的配置,你想用作父POM,并且你想version在所有子模块中使用相同的,这也可以做到。在每个中child module POM,在下面使用

<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>
Run Code Online (Sandbox Code Playgroud)

作为参考,您可以通过maven 多模块设置