Spring Boot Gradle Plugin
To paraphrase Spring Boot Gradle Plugin Reference Guide:
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…
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.
So, a typical build file might have:
plugins { id 'java' id 'org.springframework.boot' version '2.2.6.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' }
Why one or the other?
If you need Spring Boot support, you need the Spring Boot plugin.
In addition, if you want managed dependencies (as explained above), you need the dependency-management plugin.
One big win with Spring dependency-management is the starter modules that Spring provides. For example, without a test-starter, you’d manually add Test libraries:
dependencies { testImplementation('org.junit.jupiter:junit-jupiter-api') testImpementation('org.junit.jupiter:junit-jupiter-engine') testImpementation('org.assertj:assertj-core') //:3.15.0 testImpementation('org.mockito:mockito-core') //:3.3.3 }
With Spring boot Starter Test you replace these with a single dependency:
dependencies { testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } }
You could even use the dependency-management plugin without the Spring Boot plugin, if you only care for the dependency management aspect.
The dependency-mangement plugin provides Maven-like dependency management. It allows you to import an existing Maven BOM to utilise its dependency management:
https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/
dependencyManagement {
imports {
mavenBom ‘io.spring.platform:platform-bom:1.0.1.RELEASE’
}
}
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' }