为什么引入兄弟模块会改变 gradle 5 中该模块的依赖性 rules/order?

Why does bringing in a sibling module change the dependency rules/order of that module in gradle 5?

我有两个模块:

base-lib 有一些 Spring Boot/Security 依赖项以及一些 Azure 依赖项。 Azure 需要特定版本的 nimbusds,因此我将该依赖项设置为特定版本 (5.64.4)。当我自己构建第一个模块时,gradle 只下载 5.64.4。但是当我将它作为另一个模块的项目依赖项(它有 no 其他依赖项)包含时,它会下载 two 版本:5.64.4和 6.0。为什么会有所不同?

基础库:build.gradle

buildscript {

    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: "java"
apply plugin: "java-library"
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"

group "${group}"
version "${version}"

sourceCompatibility = 11.0

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {

    api( [ "com.nimbusds:oauth2-oidc-sdk:5.64.4" ] )

    /* These are what pulls in 6.0 */
    api( [ "org.springframework.boot:spring-boot-starter-security:${springBootVersion}" ] )
    api( [ "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:${springBootVersion}" ] )
    api( [ "org.springframework.security:spring-security-oauth2-client:${springOAuthClientVersion}" ] )

    //Microsoft Azure AD
    api( [ "com.microsoft.azure:adal4j:${adal4jVersion}" ] )

    /* elided, lot's of other dependencies here */
}

服务build.gradle

dependencies {
    implementation project(":base-lib")
}

如果我删除第二个模块 (service) 并构建第一个模块,那么它只会下载 5.64.4。但是当我同时拥有并构建它们时,它也降低了 6.0。

这修复了它,但是为什么 在作为项目依赖项引入时 而不是通常需要?为什么依赖规则不同?

api( [ "com.nimbusds:oauth2-oidc-sdk:5.64.4" ] ) {
    force = true
}

解决此类问题的最佳方法是对有问题的依赖项使用 the dependencyInsight task

对于您的情况,最可能的解释是您的项目 base-lib 使用了 Spring 引导和 Spring 依赖管理插件。这些插件将根据 Spring 引导 BOM 强制使用多个版本,但也有一个 feature 使得任何使用版本声明的依赖项覆盖来自 BOM 的内容。并且由于您指定了 oauth2-oidc-sdk 的版本,它确实获得了该版本。

现在,当您在 service 中传递所有这些依赖项时,不会应用依赖项管理插件。因此默认 Gradle 解析规则适用,这意味着在 5.64.46.0 版本之间,Gradle 将选择最高的。

可以通过强制使用您试验过的版本或应用相同的插件并再次声明来完成修复。