记录一次Maven依赖传递,模块之间依赖版本不一致问题
模块A
<groupId>com.bfxy.netty</groupId>
<artifactId>netty-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
<dependency>
<groupId>com.study</groupId>
<artifactId>demo</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
模块B:依赖模块A
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.bfxy.netty</groupId>
<artifactId>netty-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
对比发现两个模块的 netty-all 版本不一致。
原因
一开始以为是缓存的问题,但是清了缓存还是没有变化。后来发现,原来是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默认配置的版本号。
2、所以解释为什么 模块A 的 demo 版本 是1.2 ?
因为 springboot 2.1.5 中没有默认配置 demo这个依赖。依赖传递过来时,会沿用传递过来的版本。
<dependency>
<groupId>com.study</groupId>
<artifactId>demo</artifactId>
<version>1.2</version>
</dependency>
3、怎么正确依赖
可以使用 exclusion 标签排除依赖传递,再新增一个dependency 并指定版本
<dependencies>
<dependency>
<groupId>com.bfxy.netty</groupId>
<artifactId>netty-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
</dependencies>
参考:
记录一次Maven依赖传递,模块之间依赖版本不一致问题_mvn间接依赖固定版本_i进击的攻城狮的博客-CSDN博客
还没有评论,来说两句吧...