gradle 如何解决冲突的依赖版本
how does gradle resolve conflicting dependency versions
假设我有 3 个模块,其中包含 3 个不同的 build.gradle 属性 文件。
模块 A v1 在 build.gradle
中有以下条目
ATLAS_VERSION = 1
模块 B v1 在 build.gradle
中有以下条目
ATLAS_VERSION = 2
MODULE_A_VERSION = 1
模块 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.
您可以使用下面的简单 multi-project 构建快速测试此行为:
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
假设我有 3 个模块,其中包含 3 个不同的 build.gradle 属性 文件。
模块 A v1 在 build.gradle
中有以下条目ATLAS_VERSION = 1
模块 B v1 在 build.gradle
中有以下条目ATLAS_VERSION = 2
MODULE_A_VERSION = 1
模块 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.
您可以使用下面的简单 multi-project 构建快速测试此行为:
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