gradle如何解决冲突的依赖版本 [英] how does gradle resolve conflicting dependency versions

查看:472
本文介绍了gradle如何解决冲突的依赖版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有3个模块,其中包含3个不同的build.gradle属性文件。

Say I have 3 modules with 3 different build.gradle property files.

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

Module A v1 has the following entries in build.gradle

ATLAS_VERSION = 1

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

Module B v1 has the following entries in build.gradle

ATLAS_VERSION = 2
MODULE_A_VERSION = 1

模块C v1的build.gradle中具有以下条目

Module C v1 has the following entries in its build.gradle

ATLAS_VERSION = 3
MODULE_B_VERSION = 1

所以我的问题是:运行时将解决什么ATLAS版本?

So my question is: what ATLAS version will be resolved during runtime?

推荐答案

根据此Gradle文档管理传递依赖项,如果您没有为传递依赖项解析指定任何特定的约束,最高版本的ATLAS mo应该选择dules:

According to this Gradle documentation Managing Transitive Dependencies, in case you don't specify any specific constraints for transitive dependencies resolution, the highest version of ATLAS modules should be selected:


当Gradle尝试将对模块版本的依赖项解析为所有依赖项声明以及版本,所有可传递依赖项以及所有该模块的依赖关系约束已考虑在内。选择满足所有条件的最高版本。

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.

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

You can quickly test this behavior with simple multi-project build below:

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

You can then check which version of commons-io has been selected, which is 2.6 :

./ gradlew C:依赖项

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如何解决冲突的依赖版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆