添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

What happens when spring-boot plugin is added to Gradle project? Why do we need to explicitly include spring.dependency-management plugin also.?

plugins {
    id "org.springframework.boot" version "2.1.5.RELEASE"
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
                For future reference, stating a target or end goal improves the quality of a question. For example, this would be better if you'd clarified whether you wish to have dependency management. Or in fact it was just Spring Boot support that you were after. See my answer for further clarification.
– Nikhil Silveira
                May 21, 2020 at 9:11

Since Gradle 5+ supports BOM files, you don't need the dependency-management plugin anymore. The spring boot plugin is still needed to provide tasks such as bootJar and bootRun. Here's a minimal build.gradle that should work:

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'
                To add two points in favor of using BOM instead of the gradle plugin, based on my experience working with a larger multi-project build: (1) The dependency resolution with BOM is much faster, in some cases over 90%. (2) The dependency management plugin creates "detached Configurations" for its managed dependencies. This makes it much more difficult to follow how the dependency was resolved.
– ZeitPolizei
                Sep 23, 2021 at 13:20
  

The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by 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. But...

Why Spring Dependency Management Plugin

The spring-boot-dependencies BOM enables dependency management.

Which means:

  • IF you omit the version for a dependency you declare:
  • AND it is a managed dependency (ie listed in the BOM with version):
  • THEN (provided you apply the spring-boot-dependencies plugin) you get to enjoy dependency management like in 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. 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.

    Why one or the other?

    If you need Spring Boot support, you want that plugin.

    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).

    Another way (supported by Gradle 5 and up) is to import recommended dependency versions from a BOM as dependency constraints in Gradle. The "platform" (dependency handler method) is used to import the 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'
      

    Spring Boot’s Gradle plugin no longer automatically applies the dependency management plugin. 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. 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.

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

    In addition to the Migration Guide, this is documented as part of the Spring Boot Plugin itself. – jaco0646 Aug 9, 2021 at 20:57

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.