Maven依赖项在传递依赖项中忽略管理版本

u3r8eeie  于 2023-05-16  发布在  Maven
关注(0)|答案(2)|浏览(174)

Maven正在过渡性地引入guava的版本16,尽管我有一个指定版本18的部分。
快速总结:

  • gwizard-example依赖于gwizard-config
  • gwizard-config具有父pom gwizard-parent
  • gwizard-parent具有指定版本18的guava

值得庆幸的是,这是一个开源项目,所以你可以直接看到pom:gwizard-parentgwizard-configgwizard-example。但是,gwizard-parent中有一点很重要:

<properties>
    <guava.version>18.0</guava.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

...以及在gwizard-example中声明的无装饰依赖项:

<properties>
    <gwizard.version>0.5</gwizard.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.gwizard</groupId>
        <artifactId>gwizard-config</artifactId>
        <version>${gwizard.version}</version>
    </dependency>
</dependencies>

gwizard-config的依赖关系树正确显示guava 18:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gwizard-config ---
[INFO] org.gwizard:gwizard-config:jar:0.5
[INFO] +- com.google.inject:guice:jar:4.0-beta5:compile
[INFO] |  \- com.google.guava:guava:jar:18.0:compile

但是,gwizard-example的依赖关系树显示guava 16(这会导致问题):

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gwizard-example ---
[INFO] org.gwizard:gwizard-example:jar:1.0-SNAPSHOT
[INFO] +- org.gwizard:gwizard-config:jar:0.5:compile
[INFO] |  +- com.google.inject:guice:jar:4.0-beta5:compile
[INFO] |  |  \- com.google.guava:guava:jar:16.0.1:compile

这是使用Maven v3.2.5。我很困惑。帮忙?
可能相关:dependencyManagement in parent ignored

UPDATE:github上链接的pom正在发生变化;在gwizard-example中向gwizard-services添加一个依赖项(它直接声明了一个guavadep)“修复”了这个问题。还是有一些不好的潜在行为在这里。
UPDATE:已创建this JIRA issue

jhiyze9q

jhiyze9q1#

有一个简单的事情。一个dependencyManagement并没有声明一个真正被使用的依赖项,它只是定义了版本等等。*************
如果你这样定义,它不会导致变化。

<properties>
    <guava.version>18.0</guava.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

如果你真的想覆盖你树中使用的版本,你需要定义一个真正的依赖:因此,基于上述定义,您还需要添加以下内容:

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </dependency>
</dependencies>

如果您已添加此内容,请稍后通过mvn dependency:tree进行检查。

zysjyyx4

zysjyyx42#

在这种情况下,Maven不能解决版本传递依赖问题。
这个问题可以通过使用maven bom概念来解决。
查看bom的Maven文档。
Here是另一个解释bom用法的博客。
在您的案例中,要解决此问题,您需要在项目gwizard-exampledependencyManagement部分中添加以下依赖项。

<dependency>
  <groupId>org.gwizard</groupId>
  <artifactId>gwizard-parent</artifactId>
  <version>${gwizard.version}</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

相关问题