【问题标题】:Check dependency version in a multi-module Maven project检查多模块 Maven 项目中的依赖版本
【发布时间】:2016-01-10 21:43:03
【问题描述】:

我的多模块项目的父 POM 中有这个 dependencyManagement:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>A</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>B</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>C</artifactId>
            <version>1.1</version>
        </dependency>
</dependencyManagement>

在每个子模块 POM 中:

模块 A:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>ParentId</artifactId>
    <version>1.0</version>
</parent>
<artifactId>A</artifactId>
<version>3.5</version>
<packaging>nbm</packaging>

模块 B:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>ParentId</artifactId>
    <version>1.0</version>
</parent>
<artifactId>B</artifactId>
<version>1.0</version>
<packaging>nbm</packaging>

模块 C:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>ParentId</artifactId>
    <version>1.0</version>
</parent>
<artifactId>C</artifactId>
<version>1.1</version>
<packaging>nbm</packaging>

每次我们要发布时,我们都必须检查每个子模块中的版本是否与dependencyManagement中的版本相同(开发人员每次更改时都被要求更改两个地方的版本)。

有什么方法可以自动检查版本是否相同?如果版本不一样,如何自动更改?

【问题讨论】:

  • 模块 B 和 C 的 artifactId 不应该是 BC 吗??
  • 您应该考虑在父 pom 的 maven 属性中定义版本,并在子模块中引用该属性。这样他们就会同步。
  • 如果你有一个多模块构建,每个模块/子模块都应该与父模块具有相同的版本,这样你就违反了多模块构建的想法,这会导致你遇到问题。
  • @vikingsteve 你是对的。我编辑了:)
  • @SanjeevGour 如果我按照你说的做,我会收到一条警告:“版本包含一个表达式,但应该是一个常量。强烈建议修复这些问题,因为它们会威胁到你的稳定性构建。因此,未来的 Maven 版本可能不再支持构建此类格式错误的项目。”

标签: maven maven-release-plugin multi-module


【解决方案1】:
parent  pom discovery-ui :

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>discovery-ui</artifactId>
    <version>1.0</version>
</parent>

<properties>
        <A.version>3.5</A.version>
        <B.version>1.0</B.version>
        <C.version>1.1</C.version>
<properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>A</artifactId>
            <version>${A.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>B</artifactId>
            <version>${B.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>C</artifactId>
            <version>${C.version}</version>
        </dependency>
</dependencyManagement>

Module A:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>discovery-ui</artifactId>
    <version>1.0</version>
</parent>
<artifactId>A</artifactId>
<version>${A.version}</version>
<packaging>nbm</packaging>

【讨论】:

  • 这样我得到以下信息:'version' 包含一个表达式,但应该是一个常量。强烈建议修复这些问题,因为它们会威胁您构建的稳定性。因此,未来的 Maven 版本可能不再支持构建此类格式错误的项目。
【解决方案2】:

你试过maven versions plugin吗?看起来processParent 参数应该可以解决问题。不过,我还没有测试过。

mvn versions:set -DgenerateBackupPoms=false -f A/pom.xml -DnewVersion=NEW_VERSION -DartifactId=A -DprocessParent=true

【讨论】:

    【解决方案3】:

    您已经在父 POM 的 &lt;dependencyManagement&gt; 部分下声明了依赖项及其版本。 这样你只需要在你的子 POM 中声明 GroupID:ArtifactID(没有版本,它将从父 POM 继承)

    【讨论】: