From 7ac14203ab63b90ea06231c97592630f38fe4081 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 6 Oct 2020 14:34:39 +0100 Subject: [PATCH] Expand Gradle plugin's docs on setting bootRun's system properties Closes gh-23578 --- .../src/main/asciidoc/running.adoc | 29 +++++++++++++++++-- .../running/boot-run-system-property.gradle | 18 ++++++++++++ .../boot-run-system-property.gradle.kts | 20 +++++++++++++ .../docs/RunningDocumentationTests.java | 15 +++++++++- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle.kts diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/running.adoc b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/running.adoc index cc616b50c99..c4839dd0df8 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/running.adoc +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/asciidoc/running.adoc @@ -92,9 +92,34 @@ See {gradle-api}/org/gradle/api/tasks/JavaExec.html#setArgsString-java.lang.Stri [[running-your-application-passing-system-properties]] === Passing System properties to your application Since `bootRun` is a standard `JavaExec` task, system properties can be passed to the application's JVM by specifying them in the build script. -The values can be parameterized and passed as properties on the command line using the `-P` flag. +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]. +To allow a project property to be optional, reference it using `findProperty`. +Doing so also allows a default value to be provided using the `?:` Elvis operator, as shown in the following example: -See {gradle-api}/org/gradle/api/tasks/JavaExec.html#systemProperty-java.lang.String-java.lang.Object-[the javadoc for `JavaExec.systemProperty`] for further details. +[source,groovy,indent=0,subs="verbatim,attributes",role="primary"] +.Groovy +---- +include::../gradle/running/boot-run-system-property.gradle[tags=system-property] +---- + +[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"] +.Kotlin +---- +include::../gradle/running/boot-run-system-property.gradle.kts[tags=system-property] +---- + +The preceding example sets that `com.example.property` system property to the value of the `example` project property. +If the `example` project property has not been set, the value of the system property will be `default`. + +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: + +[source,bash,indent=0,subs="verbatim,attributes"] +---- +$ ./gradlew -Pexample=custom +---- + +The preceding example sets the value of the `example` project property to `custom`. +`bootRun` will then use this as the value of the `com.example.property` system property. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle new file mode 100644 index 00000000000..ec02477d9b3 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle @@ -0,0 +1,18 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '{version}' +} + +// tag::system-property[] +bootRun { + systemProperty 'com.example.property', findProperty('example') ?: 'default' +} +// end::system-property[] + +task configuredSystemProperties { + doLast { + bootRun.systemProperties.each { k, v -> + println "$k = $v" + } + } +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle.kts b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle.kts new file mode 100644 index 00000000000..a15e436ef4c --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/running/boot-run-system-property.gradle.kts @@ -0,0 +1,20 @@ +import org.springframework.boot.gradle.tasks.run.BootRun + +plugins { + java + id("org.springframework.boot") version "{version}" +} + +// tag::system-property[] +tasks.getByName("bootRun") { + systemProperty("com.example.property", findProperty("example") ?: "default") +} +// end::system-property[] + +task("configuredSystemProperties") { + doLast { + tasks.getByName("bootRun").systemProperties.forEach { k, v -> + println("$k = $v") + } + } +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/RunningDocumentationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/RunningDocumentationTests.java index cad05fca05f..92b6f0ec74b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/RunningDocumentationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/docs/RunningDocumentationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,4 +68,17 @@ class RunningDocumentationTests { .build("optimizedLaunch").getOutput()).contains("false"); } + @TestTemplate + void bootRunSystemPropertyDefaultValue() throws IOException { + assertThat(this.gradleBuild.script("src/main/gradle/running/boot-run-system-property") + .build("configuredSystemProperties").getOutput()).contains("com.example.property = default"); + } + + @TestTemplate + void bootRunSystemPropetry() throws IOException { + assertThat(this.gradleBuild.script("src/main/gradle/running/boot-run-system-property") + .build("-Pexample=custom", "configuredSystemProperties").getOutput()) + .contains("com.example.property = custom"); + } + }