管理依赖项

要在你的 Spring Boot 应用程序中管理依赖,你可以应用 io.spring.dependency-management 插件,或者使用 Gradle 的原生 BOM 支持。前者的主要优点是它提供了基于属性的管理版本自定义功能,而后者可能会带来更快的构建速度。

使用依赖管理插件管理依赖

当你应用 io.spring.dependency-management 插件时,Spring Boot 的插件会自动 导入你正在使用的 Spring Boot 版本的 spring-boot-dependencies BOM。这提供了类似于 Maven 用户所享有的依赖管理体验。例如,它允许你在声明 BOM 中管理的依赖时省略版本号。要使用此功能,请像往常一样声明依赖,但省略版本号

  • Groovy

  • Kotlin

dependencies {
	implementation('org.springframework.boot:spring-boot-starter-web')
	implementation('org.springframework.boot:spring-boot-starter-data-jpa')
}
dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("org.springframework.boot:spring-boot-starter-data-jpa")
}

自定义管理版本

在应用依赖管理插件时自动导入的 spring-boot-dependencies BOM 使用属性来控制其管理的依赖版本。请查阅 Spring Boot 参考文档中的依赖版本属性部分,以获取这些属性的完整列表。

要自定义管理版本,请设置其对应的属性。例如,要自定义由 slf4j.version 属性控制的 SLF4J 版本

  • Groovy

  • Kotlin

ext['slf4j.version'] = '1.7.20'
extra["slf4j.version"] = "1.7.20"
每个 Spring Boot 版本都是针对一套特定的第三方依赖进行设计和测试的。覆盖版本可能会导致兼容性问题,应谨慎操作。

独立使用 Spring Boot 的依赖管理

Spring Boot 的依赖管理可以在项目中独立使用,而无需将 Spring Boot 插件应用于该项目。SpringBootPlugin 类提供了一个 BOM_COORDINATES 常量,可用于导入 BOM,而无需知道其 groupId、artifactId 或版本。

首先,将项目配置为依赖 Spring Boot 插件,但不要应用它

  • Groovy

  • Kotlin

/*
 * Copyright 2012-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://apache.ac.cn/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

plugins {
	id 'org.springframework.boot' version '4.0.0' apply false
}
plugins {
	id("org.springframework.boot") version "4.0.0" apply false
}

Spring Boot 插件对依赖管理插件的依赖意味着你可以使用依赖管理插件,而无需声明对其的依赖。这也意味着你将自动使用与 Spring Boot 相同的依赖管理插件版本。

应用依赖管理插件,然后配置它以导入 Spring Boot 的 BOM

  • Groovy

  • Kotlin

apply plugin: 'io.spring.dependency-management'

dependencyManagement {
	imports {
		mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
	}
}
apply(plugin = "io.spring.dependency-management")

the<DependencyManagementExtension>().apply {
	imports {
		mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
	}
}

上面的 Kotlin 代码有点笨拙。那是因为我们正在使用命令式的方式应用依赖管理插件。

我们可以通过从根父项目应用插件,或者像我们对 Spring Boot 插件那样使用 plugins 块来使代码不那么笨拙。这种方法的缺点是它强制我们指定依赖管理插件的版本

plugins {
	java
	id("org.springframework.boot") version "4.0.0" apply false
	id("io.spring.dependency-management") version "1.1.7"
}

dependencyManagement {
	imports {
		mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
	}
}

了解更多

要了解更多关于依赖管理插件的功能,请参阅其文档

使用 Gradle 的 BOM 支持管理依赖

Gradle 允许通过将其声明为 platformenforcedPlatform 依赖项来使用 BOM 管理项目的版本。platform 依赖项将 BOM 中的版本视为建议,并且依赖关系图中的其他版本和约束可能会导致使用与 BOM 中声明的版本不同的依赖项版本。enforcedPlatform 依赖项将 BOM 中的版本视为要求,它们将覆盖依赖关系图中找到的任何其他版本。

SpringBootPlugin 类提供了一个 BOM_COORDINATES 常量,可用于声明对 Spring Boot BOM 的依赖,而无需知道其 group ID、artifact ID 或版本,如以下示例所示

  • Groovy

  • Kotlin

dependencies {
	implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
dependencies {
	implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
}

平台或强制平台只会约束已声明的配置或从已声明的配置扩展的配置的版本。因此,可能需要在多个配置中声明相同的依赖项。

自定义管理版本

在使用 Gradle 的 BOM 支持时,你不能使用 spring-boot-dependencies 中的属性来控制其管理的依赖版本。相反,你必须使用 Gradle 提供的机制之一。其中一种机制是解析策略。SLF4J 的所有模块都在 org.slf4j 组中,因此可以通过将该组中的每个依赖项配置为使用特定版本来控制它们的版本,如以下示例所示

  • Groovy

  • Kotlin

configurations.all {
	resolutionStrategy.eachDependency { DependencyResolveDetails details ->
		if (details.requested.group == 'org.slf4j') {
			details.useVersion '1.7.20'
		}
	}
}
configurations.all {
    resolutionStrategy.eachDependency {
        if (requested.group == "org.slf4j") {
            useVersion("1.7.20")
        }
    }
}
每个 Spring Boot 版本都是针对一套特定的第三方依赖进行设计和测试的。覆盖版本可能会导致兼容性问题,应谨慎操作。
© . This site is unofficial and not affiliated with VMware.