Skip to content

Commit 7ac1420

Browse files
committed
Expand Gradle plugin's docs on setting bootRun's system properties
Closes gh-23578
1 parent 4dad8c2 commit 7ac1420

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,34 @@ See {gradle-api}/org/gradle/api/tasks/JavaExec.html#setArgsString-java.lang.Stri
9292
[[running-your-application-passing-system-properties]]
9393
=== Passing System properties to your application
9494
Since `bootRun` is a standard `JavaExec` task, system properties can be passed to the application's JVM by specifying them in the build script.
95-
The values can be parameterized and passed as properties on the command line using the `-P` flag.
95+
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].
96+
To allow a project property to be optional, reference it using `findProperty`.
97+
Doing so also allows a default value to be provided using the `?:` Elvis operator, as shown in the following example:
9698

97-
See {gradle-api}/org/gradle/api/tasks/JavaExec.html#systemProperty-java.lang.String-java.lang.Object-[the javadoc for `JavaExec.systemProperty`] for further details.
99+
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
100+
.Groovy
101+
----
102+
include::../gradle/running/boot-run-system-property.gradle[tags=system-property]
103+
----
104+
105+
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
106+
.Kotlin
107+
----
108+
include::../gradle/running/boot-run-system-property.gradle.kts[tags=system-property]
109+
----
110+
111+
The preceding example sets that `com.example.property` system property to the value of the `example` project property.
112+
If the `example` project property has not been set, the value of the system property will be `default`.
113+
114+
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:
115+
116+
[source,bash,indent=0,subs="verbatim,attributes"]
117+
----
118+
$ ./gradlew -Pexample=custom
119+
----
120+
121+
The preceding example sets the value of the `example` project property to `custom`.
122+
`bootRun` will then use this as the value of the `com.example.property` system property.
98123

99124

100125

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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,4 +68,17 @@ void bootRunDisableOptimizedLaunch() throws IOException {
6868
.build("optimizedLaunch").getOutput()).contains("false");
6969
}
7070

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

0 commit comments

Comments
 (0)