gpt4 book ai didi

gradle - gradle如何解决冲突的依赖版本

转载 作者:行者123 更新时间:2023-12-03 03:46:27 25 4
gpt4 key购买 nike

假设我有 3 个模块和 3 个不同的 build.gradle 属性文件。

模块 A v1 在 build.gradle 中有以下条目

ATLAS_VERSION = 1

模块 B v1 在 build.gradle 中有以下条目

ATLAS_VERSION = 2
MODULE_A_VERSION = 1

Module C v1 在其 build.gradle 中有以下条目

ATLAS_VERSION = 3
MODULE_B_VERSION = 1

所以我的问题是:运行时将解析哪个 ATLAS 版本?

最佳答案

根据这个 Gradle 文档 Managing Transitive Dependencies ,如果您没有为传递依赖项解析指定任何特定约束,则应选择 ATLAS 模块的最高版本:

When Gradle attempts to resolve a dependency to a module version, all dependency declarations with version, all transitive dependencies and all dependency constraints for that module are taken into consideration. The highest version that matches all conditions is selected.

您可以使用下面的简单多项目构建快速测试此行为:

settings.gradle

rootProject.name = 'demo'
include "A", "B", "C"

build.gradle

subprojects{
apply plugin: "java"
repositories{
mavenCentral()
}
}
project(':A') {
dependencies{
implementation 'commons-io:commons-io:1.2'
}
}
project(':B') {
dependencies{
implementation project(":A")
implementation 'commons-io:commons-io:2.0'
}
}
project(':C') {
dependencies{
implementation project(":B")
implementation 'commons-io:commons-io:2.6'
}
}

然后您可以检查选择了哪个版本的commons-io,即2.6:

./gradlew C:dependencies

runtimeClasspath - Runtime classpath of source set 'main'.
+--- project :B
| +--- project :A
| | \--- commons-io:commons-io:1.2 -> 2.6
| \--- commons-io:commons-io:2.0 -> 2.6
\--- commons-io:commons-io:2.6

关于gradle - gradle如何解决冲突的依赖版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56135182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com