简体   繁体   English

当我们已经使用 id “org.springframework.boot” 插件时,我们是否需要 “io.spring.dependency-management” gradle 插件

[英]Do we need "io.spring.dependency-management" gradle plugin when already using id "org.springframework.boot" plugin

What happens when spring-boot plugin is added to Gradle project?将 spring-boot 插件添加到 Gradle 项目时会发生什么? Why do we need to explicitly include spring.dependency-management plugin also.?为什么我们还需要明确包含 spring.dependency-management 插件。?

plugins {
    id "org.springframework.boot" version "2.1.5.RELEASE"
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}

Since Gradle 5+ supports BOM files, you don't need the dependency-management plugin anymore.由于 Gradle 5+ 支持 BOM文件,您不再需要依赖管理插件。 The spring boot plugin is still needed to provide tasks such as bootJar and bootRun .仍然需要 spring boot 插件来提供bootJarbootRun等任务。 Here's a minimal build.gradle that should work:这是一个应该可以工作的最小 build.gradle:

buildscript {
    ext {
        springBootVersion = '2.2.4.RELEASE'
    }
}

repositories {
    mavenCentral()
}

plugins {
    id 'java'
    id 'org.springframework.boot' version "${springBootVersion}"
}

dependencies {
    implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

Why Spring Boot Gradle Plugin为什么使用 Spring Boot Gradle 插件

To paraphrase Spring Boot Gradle Plugin Reference Guide :转述Spring Boot Gradle 插件参考指南

The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. Spring Boot Gradle 插件在 Gradle 中提供 Spring Boot 支持。 It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies.它允许你打包可执行的 jar 或 war 档案,运行 Spring Boot 应用程序,并使用 spring-boot-dependencies 提供的依赖管理。

The key point to note is, apart from providing Spring Boot support, it enables the use of dependency management through spring-boot-dependencies.需要注意的关键点是,除了提供 Spring Boot 支持外,它还通过 spring-boot-dependencies 启用了依赖管理的使用。 But...但...

Why Spring Dependency Management Plugin为什么使用 Spring 依赖管理插件

The spring-boot-dependencies BOM enables dependency management. spring-boot-dependencies BOM 支持依赖管理。

Which means:意思是:

  1. IF you omit the version for a dependency you declare:如果您省略了您声明的依赖项的版本:
  2. AND it is a managed dependency (ie listed in the BOM with version):并且它是一个托管依赖项(即在 BOM 中列出的版本):
  3. THEN (provided you apply the spring-boot-dependencies plugin) you get to enjoy dependency management like in Maven那么(如果你应用了 spring-boot-dependencies 插件)你就可以像在 Maven 中一样享受依赖管理

To get the dependencies bom in your project you have to apply the dependency-management plugin in concert with the Spring boot gradle plugin.要在您的项目中获取依赖项 bom,您必须将依赖项管理插件与 Spring boot gradle 插件配合使用。 Again to paraphrase再次转述

When you apply the io.spring.dependency-management plugin, Spring Boot's plugin will automatically import the spring-boot-dependencies bom from the version of Spring Boot that you are using.当您应用 io.spring.dependency-management 插件时,Spring Boot 的插件会自动从您使用的 Spring Boot 版本中导入 spring-boot-dependencies bom。

Why one or the other?为什么是其中之一?

If you need Spring Boot support, you want that plugin.如果您需要 Spring Boot 支持,则需要该插件。

In addition, if you want managed dependencies (as explained above), you want the dependency-management plugin.此外,如果您需要托管依赖项(如上所述),则需要依赖项管理插件。

Another way to have dependency constraints具有依赖性约束的另一种方法

The dependency-management plugin is one way to import the Spring Boot bill of materials (BOM).依赖管理插件是导入 Spring Boot 材料清单 (BOM) 的一种方式。

Another way (supported by Gradle 5 and up) is to import recommended dependency versions from a BOM as dependency constraints in Gradle.另一种方法(由 Gradle 5 及更高版本支持)是从 BOM 中导入推荐的依赖版本作为 Gradle 中的依赖约束。 The "platform" (dependency handler method) is used to import the BOM. “平台”(依赖处理程序方法)用于导入 BOM。

dependencies {
    // Load BOM for Spring Boot.
    implementation(platform("org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE"))
}

Using either way of dependency management, you can specify your dependencies without an explicit version, as below:使用任何一种依赖管理方式,您都可以在没有显式版本的情况下指定您的依赖项,如下所示:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}

The migration guide gives some hints about this:迁移指南对此给出了一些提示:

Spring Boot's Gradle plugin no longer automatically applies the dependency management plugin. Spring Boot 的 Gradle 插件不再自动应用依赖管理插件。 Instead, Spring Boot's plugin now reacts to the dependency management plugin being applied by importing the correct version of the spring-boot-dependencies BOM.相反,Spring Boot 的插件现在通过导入正确版本的 spring-boot-dependencies BOM 来对应用的依赖管理插件做出反应。 This gives you more control over how and when dependency management is configured.这使您可以更好地控制如何以及何时配置依赖项管理。

For most applications applying the dependency management plugin will be sufficient:对于大多数应用程序应用依赖管理插件就足够了:

 apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' // <-- add this to your build.gradle

Note: The dependency management plugin remains a transitive dependency of spring-boot-gradle-plugin so there's no need for it to be listed as a classpath dependency in your buildscript configuration.注意:依赖管理插件仍然是 spring-boot-gradle-plugin 的传递依赖,因此不需要在 buildscript 配置中将其列为类路径依赖。

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#dependency-management https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#dependency-management

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gradle 无法应用插件 [id &#39;org.springframework.boot&#39;] - gradle Failed to apply plugin [id 'org.springframework.boot'] 找不到ID为&#39;org.springframework.boot&#39;的插件 - plugin with id 'org.springframework.boot' not found 插件org.springframework.boot:spring-boot-maven-plugin? - Plugin org.springframework.boot:spring-boot-maven-plugin? 使用 Gradle 导入项目 spring boot 得到错误:无法应用插件 &#39;org.springframework.boot&#39; - Import project spring boot with Gradle get error: Failed to apply plugin 'org.springframework.boot' 在以下任何来源中均未找到插件 [id: 'org.springframework.boot']: - Plugin [id: 'org.springframework.boot'] was not found in any of the following sources: 找不到插件“org.springframework.boot:spring-boot-maven-plugin:” - Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found Gradle 无法解析 org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE (repo1.maven.org: Nome o servizio sconosciuto) - Gradle Could not resolve org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE (repo1.maven.org: Nome o servizio sconosciuto) 无法执行目标 org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE - Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.0.RELEASE 无法执行目标 org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE - Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE 无法执行目标 org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run - Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM