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

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

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

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

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

  1. <properties>
  2. <guava.version>18.0</guava.version>
  3. </properties>
  4. <dependencyManagement>
  5. <dependencies>
  6. <dependency>
  7. <groupId>com.google.guava</groupId>
  8. <artifactId>guava</artifactId>
  9. <version>${guava.version}</version>
  10. </dependency>
  11. </dependencies>
  12. </dependencyManagement>

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

  1. <properties>
  2. <gwizard.version>0.5</gwizard.version>
  3. </properties>
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.gwizard</groupId>
  7. <artifactId>gwizard-config</artifactId>
  8. <version>${gwizard.version}</version>
  9. </dependency>
  10. </dependencies>

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

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

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

  1. [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ gwizard-example ---
  2. [INFO] org.gwizard:gwizard-example:jar:1.0-SNAPSHOT
  3. [INFO] +- org.gwizard:gwizard-config:jar:0.5:compile
  4. [INFO] | +- com.google.inject:guice:jar:4.0-beta5:compile
  5. [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并没有声明一个真正被使用的依赖项,它只是定义了版本等等。*************
如果你这样定义,它不会导致变化。

  1. <properties>
  2. <guava.version>18.0</guava.version>
  3. </properties>
  4. <dependencyManagement>
  5. <dependencies>
  6. <dependency>
  7. <groupId>com.google.guava</groupId>
  8. <artifactId>guava</artifactId>
  9. <version>${guava.version}</version>
  10. </dependency>
  11. </dependencies>
  12. </dependencyManagement>

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

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.google.guava</groupId>
  4. <artifactId>guava</artifactId>
  5. </dependency>
  6. </dependencies>

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

展开查看全部
zysjyyx4

zysjyyx42#

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

  1. <dependency>
  2. <groupId>org.gwizard</groupId>
  3. <artifactId>gwizard-parent</artifactId>
  4. <version>${gwizard.version}</version>
  5. <type>pom</type>
  6. <scope>import</scope>
  7. </dependency>

相关问题