【问题标题】:What's the logic of Gradle dependency resolvingGradle依赖解析的逻辑是什么
【发布时间】:2020-11-13 13:44:23
【问题描述】:

在 Gradle 6.7 中,我们有一个 dependencyManagement.dependencies 来设置项目的默认值。

最近,有人用 dependencySet 替换了 Spring 的 dependency 行。

dependencySet(group: 'org.springframework.boot', version: "2.2.11.RELEASE") {
            entry 'spring-boot-devtools'
            entry 'spring-boot-dependencies'
            entry 'spring-boot-devtools'
            entry 'spring-boot-starter-aop'
            entry 'spring-boot-starter-cache'
            entry 'spring-boot-starter-webflux'
            ...

现在在发现一些 CVE 警报后,我发现 Gradle 将 spring-boot-starter-cache 解析为 2.2.8。我不确定它从哪里获得该版本:我们的项目中没有它,并且 deps 树看起来好像是我们自己要求的(它处于 0 级)。

+--- org.springframework.boot:spring-boot-starter-cache -> 2.2.8.RELEASE

当我像以前一样明确添加项目时,

dependency 'org.springframework.boot:spring-boot-starter-cache:2.2.11.RELEASE'

然后它最终被解析为 2.2.11。

+--- org.springframework.boot:spring-boot-starter-cache -> 2.2.11.RELEASE

在 Maven 中,依赖管理非常简单,与此相比:您可以使用依赖管理和 BOM 来控制它,并且一切正常,没有这样的惊喜。

所以也许我在 Gradle 的逻辑中遗漏了一些东西,即使在阅读了依赖管理指南之后。

如何使用类似 BOM 的 dependencySet 来一次控制所有 entry-es?还是我有错误的假设?

【问题讨论】:

    标签: gradle dependency-management gradle-dependencies


    【解决方案1】:

    在 Gradle 6.7 中,我们有一个 dependencyManagement.dependencies 来设置项目的默认值。

    不要将 Spring 的 dependency management Gradle plugin 与 Gradle 的原生 dependency mangaement 功能混淆。尽管他们实现了相同的目标,但他们以非常不同的方式实现目标。

    我不确定它从哪里获得该版本:我们的项目中没有它,并且 deps 树看起来好像是我们自己要求的(它在级别 0)。

    您可以使用dependencyInsight 任务来获取有关特定依赖项以及选择特定版本的原因的更多信息。

    ./gradlew dependencyInsight --dependency org.springframework.boot:spring-boot-starter-cache
    

    更多详情请见Viewing and debugging dependencies

    如何使用类似 BOM 的 dependencySet 来一次控制所有 entry-es?还是我有错误的假设?

    Spring 依赖管理插件的文档很清楚您需要做什么才能实现这一目标:https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#dependency-management-configuration-dsl-dependency-sets

    如果它没有像你期望的那样工作,那么你需要调试你的依赖,就像我上面链接的那样。

    同样从你的例子中,我猜你有一个典型的 Spring Boot 应用程序,应用了 Spring Boot Gradle 插件。如果是,那么 Spring Boot Gradle 插件会检测是否应用了 Spring 依赖管理插件和automatically imports the Spring Boot BOM。所以应该不需要像你一样管理 Spring 特定的依赖项。

    【讨论】:

    • 谢谢弗朗西斯科!听起来有很多事情需要检查——我们有一个内部框架来完成你提到的一些事情,但它只是我们项目的一个依赖项,不像 Maven 的父级。所以我希望这只是过渡依赖的另一个来源。我更愿意管理构建脚本中的 deps 以使其与其他依赖项保持一致。如果每个框架都有自己的插件来管理依赖关系,那将是一团糟,不是吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 2019-04-24
    • 2023-03-29
    • 2016-03-17
    • 2014-02-10
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多