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

文章讲述了在SpringBoot项目中,由于依赖传递和SpringBoot默认配置的版本冲突,可能导致模块间相同依赖的不同版本问题。例如,模块A和模块B对netty-all版本有不同需求。解决方法是使用exclusion标签排除不需要的版本,然后明确添加所需版本的dependency。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模块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博客

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值