Skip to content

Commit d2926e0

Browse files
committed
Merge branch '2.3.x'
Closes gh-22678
2 parents 95f76d6 + 00cd894 commit d2926e0

File tree

7 files changed

+204
-8
lines changed

7 files changed

+204
-8
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/getting-started.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ endif::[]
8282
Applied in isolation the plugin makes few changes to a project.
8383
Instead, the plugin detects when certain other plugins are applied and reacts accordingly.
8484
For example, when the `java` plugin is applied a task for building an executable jar is automatically configured.
85-
A typical Spring Boot project will apply the {groovy-plugin}[`groovy`], {java-plugin}[`java`], or {kotlin-plugin}[`org.jetbrains.kotlin.jvm`] plugin and the {dependency-management-plugin}[`io.spring.dependency-management`] plugin as a minimum.
85+
A typical Spring Boot project will apply the {groovy-plugin}[`groovy`], {java-plugin}[`java`], or {kotlin-plugin}[`org.jetbrains.kotlin.jvm`] plugin as a minimum and also use the {dependency-management-plugin}[`io.spring.dependency-management`] plugin or Gradle's native bom support for dependency management.
8686
For example:
8787

8888

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/docs/asciidoc/managing-dependencies.adoc

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
[[managing-dependencies]]
22
== Managing Dependencies
3+
To manage dependencies in your Spring Boot application, you can either apply the {dependency-management-plugin}[`io.spring.dependency-management`] plugin or, if you are using Gradle 6 or later, use Gradle's native bom support.
4+
The primary benefit of the former is that it offers property-based customization of managed versions, while using the latter will likely result in faster builds.
5+
6+
7+
8+
[[managing-dependencies-dependency-management-plugin]]
9+
=== Managing Dependencies with the Dependency Management Plugin
310
When you apply the {dependency-management-plugin}[`io.spring.dependency-management`] plugin, Spring Boot's plugin will automatically <<reacting-to-other-plugins-dependency-management,import the `spring-boot-dependencies` bom>> from the version of Spring Boot that you are using.
411
This provides a similar dependency management experience to the one that's enjoyed by Maven users.
512
For example, it allows you to omit version numbers when declaring dependencies that are managed in the bom.
@@ -18,8 +25,9 @@ include::../gradle/managing-dependencies/dependencies.gradle.kts[tags=dependenci
1825
----
1926

2027

21-
[[managing-dependencies-customizing]]
22-
=== Customizing Managed Versions
28+
29+
[[managing-dependencies-dependency-management-plugin-customizing]]
30+
==== Customizing Managed Versions
2331
The `spring-boot-dependencies` bom that is automatically imported when the dependency management plugin is applied uses properties to control the versions of the dependencies that it manages.
2432
Browse the {version-properties-appendix}[`Dependency versions Appendix`] in the Spring Boot reference for a complete list of these properties.
2533

@@ -38,14 +46,13 @@ include::../gradle/managing-dependencies/custom-version.gradle[tags=custom-versi
3846
include::../gradle/managing-dependencies/custom-version.gradle.kts[tags=custom-version]
3947
----
4048

41-
4249
WARNING: Each Spring Boot release is designed and tested against a specific set of third-party dependencies.
4350
Overriding versions may cause compatibility issues and should be done with care.
4451

4552

4653

47-
[[managing-dependencies-using-in-isolation]]
48-
=== Using Spring Boot's Dependency Management in Isolation
54+
[[managing-dependencies-dependency-management-plugin-using-in-isolation]]
55+
==== Using Spring Boot's Dependency Management in Isolation
4956

5057
Spring Boot's dependency management can be used in a project without applying Spring Boot's plugin to that project.
5158
The `SpringBootPlugin` class provides a `BOM_COORDINATES` constant that can be used to import the bom without having to know its group ID, artifact ID, or version.
@@ -120,6 +127,56 @@ include::../gradle/managing-dependencies/configure-bom-with-plugins.gradle.kts[t
120127
----
121128

122129

123-
[[managing-dependencies-learning-more]]
124-
=== Learning More
130+
131+
[[managing-dependencies-dependency-management-plugin-learning-more]]
132+
==== Learning More
125133
To learn more about the capabilities of the dependency management plugin, please refer to its {dependency-management-plugin-documentation}[documentation].
134+
135+
136+
137+
[[managing-dependencies-gradle-bom-support]]
138+
=== Managing Dependencies with Gradle's Bom Support
139+
Gradle allows a bom to be used to manage a project's versions by declaring it as a `platform` or `enforcedPlatform` dependency.
140+
A `platform` dependency treats the versions in the bom as recommendations and other versions and constraints in the dependency graph may cause a version of a dependency other than that declared in the bom to be used.
141+
An `enforcedPlatform` dependency treats the versions in the bom as requirements and they will override any other version found in the dependency graph.
142+
143+
The `SpringBootPlugin` class provides a `BOM_COORDINATES` constant that can be used to declare a dependency upon Spring Boot's bom without having to know its group ID, artifact ID, or version, as shown in the following example:
144+
145+
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
146+
.Groovy
147+
----
148+
include::../gradle/managing-dependencies/configure-platform.gradle[tags=configure-platform]
149+
----
150+
151+
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
152+
.Kotlin
153+
----
154+
include::../gradle/managing-dependencies/configure-platform.gradle.kts[tags=configure-platform]
155+
----
156+
157+
A platform or enforced platform will only constrain the versions of the configuration in which it has been declared or that extend from the configuration in which it has been declared.
158+
As a result, in may be necessary to declare the same dependency in more than one configuration.
159+
160+
161+
162+
[[managing-dependencies-gradle-bom-support-customizing]]
163+
==== Customizing Managed Versions
164+
When using Gradle's bom support, you cannot use the properties from `spring-boot-dependencies` to control the versions of the dependencies that it manages.
165+
Instead, you must use one of the mechanisms that Gradle provides.
166+
One such mechanism is a resolution strategy.
167+
SLF4J's modules are all in the `org.slf4j` group so their version can be controlled by configuring every dependency in that group to use a particular version, as shown in the following example:
168+
169+
[source,groovy,indent=0,subs="verbatim",role="primary"]
170+
.Groovy
171+
----
172+
include::../gradle/managing-dependencies/custom-version-with-platform.gradle[tags=custom-version]
173+
----
174+
175+
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
176+
.Kotlin
177+
----
178+
include::../gradle/managing-dependencies/custom-version-with-platform.gradle.kts[tags=custom-version]
179+
----
180+
181+
WARNING: Each Spring Boot release is designed and tested against a specific set of third-party dependencies.
182+
Overriding versions may cause compatibility issues and should be done with care.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '{gradle-project-version}'
4+
}
5+
6+
// tag::configure-platform[]
7+
dependencies {
8+
implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
9+
}
10+
// end::configure-platform[]
11+
12+
dependencies {
13+
implementation "org.springframework.boot:spring-boot-starter"
14+
}
15+
16+
repositories {
17+
maven { url 'file:repository' }
18+
}
19+
20+
configurations.all {
21+
resolutionStrategy {
22+
eachDependency {
23+
if (it.requested.group == 'org.springframework.boot') {
24+
it.useVersion 'TEST-SNAPSHOT'
25+
}
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
plugins {
2+
java
3+
id("org.springframework.boot") version "{gradle-project-version}"
4+
}
5+
6+
// tag::configure-platform[]
7+
dependencies {
8+
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
9+
}
10+
// end::configure-platform[]
11+
12+
dependencies {
13+
implementation("org.springframework.boot:spring-boot-starter")
14+
}
15+
16+
repositories {
17+
maven {
18+
url = uri("file:repository")
19+
}
20+
}
21+
22+
configurations.all {
23+
resolutionStrategy {
24+
eachDependency {
25+
if (requested.group == "org.springframework.boot") {
26+
useVersion("TEST-SNAPSHOT")
27+
}
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '{gradle-project-version}'
4+
}
5+
6+
dependencies {
7+
implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
8+
implementation "org.slf4j:slf4j-api"
9+
}
10+
11+
repositories {
12+
maven { url 'file:repository' }
13+
}
14+
15+
configurations.all {
16+
resolutionStrategy {
17+
eachDependency {
18+
if (it.requested.group == 'org.springframework.boot') {
19+
it.useVersion 'TEST-SNAPSHOT'
20+
}
21+
}
22+
}
23+
}
24+
25+
// tag::custom-version[]
26+
configurations.all {
27+
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
28+
if (details.requested.group == 'org.slf4j') {
29+
details.useVersion '1.7.20'
30+
}
31+
}
32+
}
33+
// end::custom-version[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
plugins {
2+
java
3+
id("org.springframework.boot") version "{gradle-project-version}"
4+
}
5+
6+
dependencies {
7+
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
8+
implementation("org.slf4j:slf4j-api")
9+
}
10+
11+
repositories {
12+
maven {
13+
url = uri("file:repository")
14+
}
15+
}
16+
17+
configurations.all {
18+
resolutionStrategy {
19+
eachDependency {
20+
if (requested.group == "org.springframework.boot") {
21+
useVersion("TEST-SNAPSHOT")
22+
}
23+
}
24+
}
25+
}
26+
27+
// tag::custom-version[]
28+
configurations.all {
29+
resolutionStrategy.eachDependency {
30+
if (requested.group == "org.slf4j") {
31+
useVersion("1.7.20")
32+
}
33+
}
34+
}
35+
// end::custom-version[]

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/ManagingDependenciesDocumentationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,17 @@ void dependencyManagementInIsolationWithPluginsBlock() {
6363
.contains("org.springframework.boot:spring-boot-starter TEST-SNAPSHOT"));
6464
}
6565

66+
@TestTemplate
67+
void configurePlatform() {
68+
assertThat(this.gradleBuild.script("src/docs/gradle/managing-dependencies/configure-platform")
69+
.build("dependencies", "--configuration", "compileClasspath").getOutput())
70+
.contains("org.springframework.boot:spring-boot-starter ");
71+
}
72+
73+
@TestTemplate
74+
void customManagedVersionsWithPlatform() {
75+
assertThat(this.gradleBuild.script("src/docs/gradle/managing-dependencies/custom-version-with-platform")
76+
.build("dependencies", "--configuration", "compileClasspath").getOutput()).contains("1.7.20");
77+
}
78+
6679
}

0 commit comments

Comments
 (0)