【问题标题】:How to force maven dependency version via dependencyManagement in parent pom?如何通过父pom中的dependencyManagement强制maven依赖版本?
【发布时间】:2020-08-22 09:00:23
【问题描述】:

假设B:1.0.1 具有传递依赖关系A:1.0.1,但子项目应该依赖于A:1.0.2(有意覆盖传递依赖关系)。

很容易发现<dependencyManagement> 中的依赖顺序会影响版本覆盖,因此在B:1.0.1 之前的子pom 中添加A:1.0.2 将强制使用A:1.0.2,即使作为B:1.0.1 的依赖。

在这种情况下,我正在寻找一种在父 pom 中声明 A:1.0.2 并从其所有子代中删除样板的方法。不幸的是,以下设置导致在最终工件中使用这两个版本:A:1.0.1(作为 B:1.0.1 的依赖项出现)和 A:1.0.2(来自父 pom 中的显式声明)。

如何在所有子项目中强制使用版本A:1.0.2,将声明保留在父项目中?

父 pom:

<groupId>my-group</groupId>
<artifactId>my-parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<dependencyManagement>
  <dependencies>   
    <dependency>
      <groupId>g</groupId>
      <artifactId>A</artifactId>
      <version>1.0.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

儿童 pom:

<parent>
  <groupId>my-group</groupId>
  <artifactId>my-parent</artifactId>
  <version>0.0.1</version>
</parent>

<artifactId>my-child</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<dependencies>   
  <dependency>
    <groupId>g</groupId>
    <artifactId>A</artifactId>
    <!-- version 1.0.2 comes from the parent pom -->
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>   
    <dependency>
      <groupId>g</groupId>
      <artifactId>B</artifactId>
      <version>1.0.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

【问题讨论】:

    标签: maven pom.xml dependency-management transitive-dependency


    【解决方案1】:

    您错误地使用了dependencyManagement。

    如果 A 和 B 是 jar 工件,则不应有标签

    <type>pom</type>
    <scope>import</scope>
    

    这些仅用于 BOM。

    【讨论】:

    • 让我们假设类型是适合回答这个问题的任何类型。那么我们如何解决这个版本冲突的问题呢?
    • 在父 POM 依赖管理中为 A 设置版本也会为所有子项目设置版本(当然,除非他们明确设置自己的版本)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2020-07-12
    • 2017-02-22
    • 2014-06-22
    • 1970-01-01
    • 2014-10-13
    相关资源
    最近更新 更多