首页 > 解决方案 > Gradle依赖解析的逻辑是什么

问题描述

在 Gradle 6.7 中,我们需要dependencyManagement.dependencies为项目设置默认值。

最近,有人dependencydependencySet.

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?还是我有错误的假设?

标签: gradledependency-managementgradle-dependencies

解决方案


在 Gradle 6.7 中,我们需要dependencyManagement.dependencies为项目设置默认值。

不要将 Spring 的依赖管理 Gradle 插件与 Gradle 的原生依赖管理功能混淆。尽管他们实现了相同的目标,但他们以非常不同的方式实现目标。

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

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

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

有关更多详细信息,请参阅查看和调试依赖 项。

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

Spring 依赖管理插件的文档很清楚你需要做什么来实现这一点:https ://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#dependency-management-configuration- dsl 依赖集

如果它没有按您的预期工作,那么您需要调试您的依赖项,正如我上面链接的那样。

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


推荐阅读