记录一次Maven依赖传递,模块之间依赖版本不一致问题

我就是我 2023-10-10 22:53 65阅读 0赞

模块A

  1. <groupId>com.bfxy.netty</groupId>
  2. <artifactId>netty-common</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.5.RELEASE</version>
  8. <relativePath/> <!-- lookup parent from repository -->
  9. </parent>
  10. <dependencies>
  11. <dependency>
  12. <groupId>io.netty</groupId>
  13. <artifactId>netty-all</artifactId>
  14. <version>4.1.12.Final</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.study</groupId>
  18. <artifactId>demo</artifactId>
  19. <version>1.2</version>
  20. </dependency>
  21. </dependencies>

模块B:依赖模块A

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.5.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>com.bfxy.netty</groupId>
  10. <artifactId>netty-common</artifactId>
  11. <version>0.0.1-SNAPSHOT</version>
  12. </dependency>
  13. </dependencies>

对比发现两个模块的 netty-all 版本不一致。

275fa7631ff746819665ef1791ce5750.png

原因

一开始以为是缓存的问题,但是清了缓存还是没有变化。后来发现,原来是SpringBoot的依赖问题。
SpringBoot中,默认配置了一些开源包的版本号,如果项目A里面依赖B,B依赖C,B使用了C的版本1。
但是A依赖了B后,A中的SpringBoot默认C的版本是0,所以会导致依赖进来的B使用A中SpringBoot默认配置的版本。

1、所以解释为什么 模块B 的 netty-all 版本 是 4.1.36?
因为springboot 2.1.5 已经默认配置了 netty-all 的版本是 4.1.36,所以当从模块B依赖传递过来时,版本号会用springboot默认配置的版本号。

0082784948494917b8c440a643922b08.png

2、所以解释为什么 模块A 的 demo 版本 是1.2 ?

因为 springboot 2.1.5 中没有默认配置 demo这个依赖。依赖传递过来时,会沿用传递过来的版本。

  1. <dependency>
  2. <groupId>com.study</groupId>
  3. <artifactId>demo</artifactId>
  4. <version>1.2</version>
  5. </dependency>

3、怎么正确依赖

可以使用 exclusion 标签排除依赖传递,再新增一个dependency 并指定版本

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.bfxy.netty</groupId>
  4. <artifactId>netty-common</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>io.netty</groupId>
  9. <artifactId>netty-all</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.netty</groupId>
  15. <artifactId>netty-all</artifactId>
  16. <version>4.1.12.Final</version>
  17. </dependency>
  18. </dependencies>

参考:

记录一次Maven依赖传递,模块之间依赖版本不一致问题_mvn间接依赖固定版本_i进击的攻城狮的博客-CSDN博客

发表评论

表情:
评论列表 (有 0 条评论,65人围观)

还没有评论,来说两句吧...

相关阅读

    相关 maven依赖传递依赖

    目录 依赖范围 传递性依赖 依赖调节 > 本文主要是针对《maven实战》书中关键知识点的学习记录,未免有纰漏或描述不到之处,建议购买阅读原书 首先贴出