对于最新的稳定版本,请使用 Spring Boot 3.5.5! |
管理依赖关系
要管理 Spring Boot 应用程序中的依赖项,您可以应用io.spring.dependency-management
插件或使用 Gradle 的原生 bom 支持。
前者的主要好处是它提供了基于属性的托管版本自定义,而使用后者可能会加快生成速度。
使用依赖项管理插件管理依赖项
当您应用io.spring.dependency-management
插件,Spring Boot 的插件会自动import 的spring-boot-dependencies
BOM从您正在使用的 Spring Boot 版本。
这提供了与 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
应用依赖项管理插件时自动导入的物料清单使用属性来控制它所管理的依赖项的版本。
浏览 Spring Boot 参考中的 Dependency Versions Properties 部分,获取这些属性的完整列表。
若要自定义托管版本,请设置其相应的属性。
例如,要自定义由slf4j.version
财产:
-
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,而无需知道其组 ID、工件 ID 或版本。
首先,将项目配置为依赖于 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://www.apache.org/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 '3.4.9' apply false
}
plugins {
id("org.springframework.boot") version "3.4.9" 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 代码有点尴尬。 这是因为我们使用的是应用依赖管理插件的命令式方式。
我们可以通过应用根父项目中的插件或使用plugins
块,就像我们对 Spring Boot 插件所做的那样。这种方法的一个缺点是它迫使我们指定依赖管理插件的版本:
plugins {
java
id("org.springframework.boot") version "3.4.9" 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 允许使用 bom 来管理项目的版本,方法是将 bom 声明为platform
或enforcedPlatform
Dependency。 一个platform
依赖项将 BOM 中的版本视为建议,依赖关系图中的其他版本和约束可能会导致使用 BOM 中声明的版本以外的依赖项版本。 一enforcedPlatform
依赖项将 BOM 中的版本视为需求,它们将覆盖依赖项图中找到的任何其他版本。
这SpringBootPlugin
类提供了一个BOM_COORDINATES
常量,可用于声明对 Spring Boot bom 的依赖关系,而无需知道其组 ID、工件 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 版本都是针对一组特定的第三方依赖项进行设计和测试的。 覆盖版本可能会导致兼容性问题,应谨慎作。 |