【问题标题】:Why does bringing in a sibling module change the dependency rules/order of that module in gradle 5?为什么引入同级模块会改变 gradle 5 中该模块的依赖规则/顺序?
【发布时间】:2018-12-19 17:50:26
【问题描述】:

我有两个模块:

  • 基础库
  • 服务

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

base-lib: 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 */
}

service build.gradle

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

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

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

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

【问题讨论】:

    标签: java maven gradle gradle-dependencies


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 2019-02-11
      • 2013-09-10
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多