Skip to content

Commit 2d3291f

Browse files
committed
Merge branch '2.2.x' into 2.3.x
Closes gh-23598
2 parents a74c538 + 7ac1420 commit 2d3291f

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,34 @@ See {gradle-api}/org/gradle/api/tasks/JavaExec.html#setArgsString-java.lang.Stri
8989
[[running-your-application-passing-system-properties]]
9090
=== Passing System properties to your application
9191
Since `bootRun` is a standard `JavaExec` task, system properties can be passed to the application's JVM by specifying them in the build script.
92-
The values can be parameterized and passed as properties on the command line using the `-P` flag.
92+
To make that value of a system property to be configurable set its value using a {gradle-dsl}/org.gradle.api.Project.html#N14FE1[project property].
93+
To allow a project property to be optional, reference it using `findProperty`.
94+
Doing so also allows a default value to be provided using the `?:` Elvis operator, as shown in the following example:
9395

94-
See {gradle-api}/org/gradle/api/tasks/JavaExec.html#systemProperty-java.lang.String-java.lang.Object-[the javadoc for `JavaExec.systemProperty`] for further details.
96+
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
97+
.Groovy
98+
----
99+
include::../gradle/running/boot-run-system-property.gradle[tags=system-property]
100+
----
101+
102+
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
103+
.Kotlin
104+
----
105+
include::../gradle/running/boot-run-system-property.gradle.kts[tags=system-property]
106+
----
107+
108+
The preceding example sets that `com.example.property` system property to the value of the `example` project property.
109+
If the `example` project property has not been set, the value of the system property will be `default`.
110+
111+
Gradle allows project properties to be set in a variety of ways, including on the command line using the `-P` flag, as shown in the following example:
112+
113+
[source,bash,indent=0,subs="verbatim,attributes"]
114+
----
115+
$ ./gradlew -Pexample=custom
116+
----
117+
118+
The preceding example sets the value of the `example` project property to `custom`.
119+
`bootRun` will then use this as the value of the `com.example.property` system property.
95120

96121

97122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '{version}'
4+
}
5+
6+
// tag::system-property[]
7+
bootRun {
8+
systemProperty 'com.example.property', findProperty('example') ?: 'default'
9+
}
10+
// end::system-property[]
11+
12+
task configuredSystemProperties {
13+
doLast {
14+
bootRun.systemProperties.each { k, v ->
15+
println "$k = $v"
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.springframework.boot.gradle.tasks.run.BootRun
2+
3+
plugins {
4+
java
5+
id("org.springframework.boot") version "{version}"
6+
}
7+
8+
// tag::system-property[]
9+
tasks.getByName<BootRun>("bootRun") {
10+
systemProperty("com.example.property", findProperty("example") ?: "default")
11+
}
12+
// end::system-property[]
13+
14+
task("configuredSystemProperties") {
15+
doLast {
16+
tasks.getByName<BootRun>("bootRun").systemProperties.forEach { k, v ->
17+
println("$k = $v")
18+
}
19+
}
20+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ void bootRunDisableOptimizedLaunch() throws IOException {
7171
.build("optimizedLaunch").getOutput()).contains("false");
7272
}
7373

74+
@TestTemplate
75+
void bootRunSystemPropertyDefaultValue() throws IOException {
76+
assertThat(this.gradleBuild.script("src/main/gradle/running/boot-run-system-property")
77+
.build("configuredSystemProperties").getOutput()).contains("com.example.property = default");
78+
}
79+
80+
@TestTemplate
81+
void bootRunSystemPropetry() throws IOException {
82+
assertThat(this.gradleBuild.script("src/main/gradle/running/boot-run-system-property")
83+
.build("-Pexample=custom", "configuredSystemProperties").getOutput())
84+
.contains("com.example.property = custom");
85+
}
86+
7487
}

0 commit comments

Comments
 (0)