Kotlin标准库Maven传递依赖异常:指定1.8.20-RC2却引入1.3.72
Kotlin Maven依赖冲突:为何1.8.20-RC2版本却引入1.3.72标准库?
问题原因
你遇到的是典型的Maven依赖冲突:
- 尽管你直接声明了
kotlin-stdlib-jdk8:1.8.20-RC2,但项目中某个未展示的依赖(从你截断的依赖树来看)间接引入了kotlin-stdlib:1.3.72。 - Maven默认的依赖调解规则是「路径最短优先」,若多个依赖路径长度相同,则「先声明的依赖优先」。这导致旧版本标准库被优先选中,覆盖了你期望的1.8.20-RC2版本。
- 补充说明:从Kotlin 1.4+开始,
kotlin-stdlib-jdk8本身应该依赖同版本的kotlin-stdlib和kotlin-stdlib-jdk7,出现1.3.72版本完全是第三方依赖的传递依赖干扰。
解决方案
1. 用DependencyManagement锁定所有Kotlin核心库版本(推荐)
在pom.xml中添加dependencyManagement节点,强制所有Kotlin相关依赖使用统一版本,彻底避免版本冲突:
<dependencyManagement> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk7</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </dependencyManagement>
这个配置会让所有直接/传递依赖的Kotlin库都遵循你指定的1.8.20-RC2版本。
2. 显式声明核心标准库依赖
直接在dependencies节点中添加kotlin-stdlib和kotlin-stdlib-jdk7的依赖,覆盖传递依赖的旧版本:
<dependencies> <!-- 原有的依赖 --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> <!-- 添加以下两个显式依赖 --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk7</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies>
3. 定位并排除引入旧版本的依赖
如果你想找到问题根源,可以执行以下命令,精准定位是哪个依赖引入了kotlin-stdlib:1.3.72:
mvn dependency:tree -Dverbose -Dincludes=org.jetbrains.kotlin:kotlin-stdlib
输出结果会显示完整的依赖路径,找到对应的第三方依赖后,在dependencies中添加exclusions排除其传递的旧版本Kotlin库:
<dependency> <!-- 引入旧版本的第三方依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <exclusions> <exclusion> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> </exclusion> <exclusion> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk7</artifactId> </exclusion> </exclusions> </dependency>
内容的提问来源于stack exchange,提问作者J-bob
相关产品推荐
相关产品推荐

