如何通过父pom中的dependencyManagement强制Maven依赖版本?

问题描述

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

很容易发现<dependencyManagement>中的依存顺序会影响版本覆盖,因此在A:1.0.2之前的子pom中添加B:1.0.1甚至会强制使用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>

解决方法

您不正确地使用了dependencyManagement。

如果A和B是jar文物,则不应使用标签

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

这些仅用于BOM表。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...