From 1fb904acee77697bb6c07e3bebec32a89aba7dc5 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Wed, 23 Oct 2019 18:23:27 +0300 Subject: [PATCH 1/2] Support commas embedded in command line arguments from Maven plugin See gh-18711 --- .../invoker.properties | 1 + .../src/it/run-arguments-commandline/pom.xml | 30 +++++++++++++++ .../main/java/org/test/SampleApplication.java | 38 +++++++++++++++++++ .../run-arguments-commandline/test.properties | 1 + .../run-arguments-commandline/verify.groovy | 2 + .../src/it/run-arguments/pom.xml | 36 ++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 38 +++++++++++++++++++ .../src/it/run-arguments/verify.groovy | 2 + .../boot/maven/AbstractRunMojo.java | 18 ++++++--- 9 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/invoker.properties create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/test.properties create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/verify.groovy create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/pom.xml create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/verify.groovy diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/invoker.properties b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/invoker.properties new file mode 100644 index 0000000000..9225c36f9e --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/invoker.properties @@ -0,0 +1 @@ +invoker.systemPropertiesFile=test.properties diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/pom.xml new file mode 100644 index 0000000000..af5471f4de --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + org.springframework.boot.maven.it + run-arguments-commandline + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + @java.version@ + @java.version@ + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + package + + run + + + + + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..966c3b52bb --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.test; + +import java.util.Arrays; + +public class SampleApplication { + + public static void main(String[] args) { + if (args.length < 2) { + throw new IllegalArgumentException("Missing arguments " + Arrays.toString(args) + ""); + } + if (!args[0].startsWith("--management.endpoints.web.exposure.include=")) { + throw new IllegalArgumentException("Invalid argument " + args[0]); + } + if (!args[1].startsWith("--spring.profiles.active=")) { + throw new IllegalArgumentException("Invalid argument " + args[1]); + } + String endpoints = args[0].split("=")[1]; + String profile = args[1].split("=")[1]; + System.out.println("I haz been run with profile(s) '" + profile + "' and endpoint(s) '" + endpoints + "'"); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/test.properties b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/test.properties new file mode 100644 index 0000000000..4de2d29f21 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/test.properties @@ -0,0 +1 @@ +spring-boot.run.arguments=--management.endpoints.web.exposure.include=prometheus,info,health,metrics --spring.profiles.active=foo,bar diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/verify.groovy new file mode 100644 index 0000000000..9963e64c2e --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments-commandline/verify.groovy @@ -0,0 +1,2 @@ +def file = new File(basedir, "build.log") +return file.text.contains("I haz been run with profile(s) 'foo,bar' and endpoint(s) 'prometheus,info,health,metrics'") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/pom.xml b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/pom.xml new file mode 100644 index 0000000000..34ca2c0386 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + org.springframework.boot.maven.it + run-arguments + 0.0.1.BUILD-SNAPSHOT + + UTF-8 + @java.version@ + @java.version@ + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + package + + run + + + + --management.endpoints.web.exposure.include=prometheus,info + --spring.profiles.active=foo,bar + + + + + + + + diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/src/main/java/org/test/SampleApplication.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..966c3b52bb --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.test; + +import java.util.Arrays; + +public class SampleApplication { + + public static void main(String[] args) { + if (args.length < 2) { + throw new IllegalArgumentException("Missing arguments " + Arrays.toString(args) + ""); + } + if (!args[0].startsWith("--management.endpoints.web.exposure.include=")) { + throw new IllegalArgumentException("Invalid argument " + args[0]); + } + if (!args[1].startsWith("--spring.profiles.active=")) { + throw new IllegalArgumentException("Invalid argument " + args[1]); + } + String endpoints = args[0].split("=")[1]; + String profile = args[1].split("=")[1]; + System.out.println("I haz been run with profile(s) '" + profile + "' and endpoint(s) '" + endpoints + "'"); + } + +} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/verify.groovy b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/verify.groovy new file mode 100644 index 0000000000..2a7cc17ab5 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/it/run-arguments/verify.groovy @@ -0,0 +1,2 @@ +def file = new File(basedir, "build.log") +return file.text.contains("I haz been run with profile(s) 'foo,bar' and endpoint(s) 'prometheus,info'") diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java index 013e4accc5..ca6929cd61 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java @@ -133,13 +133,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { private Map environmentVariables; /** - * Arguments that should be passed to the application. On command line use commas to - * separate multiple arguments. + * Arguments that should be passed to the application. On command line use spaces to + * separate multiple arguments and make sure to wrap multiple values between quotes. * @since 1.0.0 */ - @Parameter(property = "spring-boot.run.arguments") + @Parameter private String[] arguments; + /** + * Arguments from command line that should be passed to the application. + * @since 2.2.1 + */ + @Parameter(property = "spring-boot.run.arguments", readonly = true) + private String args; + /** * The spring profiles to activate. Convenience shortcut of specifying the * 'spring.profiles.active' argument. On command line use commas to separate multiple @@ -311,7 +318,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { * @return a {@link RunArguments} defining the application arguments */ protected RunArguments resolveApplicationArguments() { - RunArguments runArguments = new RunArguments(this.arguments); + RunArguments runArguments = (this.args != null) ? new RunArguments(this.args) + : new RunArguments(this.arguments); addActiveProfileArgument(runArguments); return runArguments; } @@ -327,7 +335,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { private void addArgs(List args) { RunArguments applicationArguments = resolveApplicationArguments(); Collections.addAll(args, applicationArguments.asArray()); - logArguments("Application argument(s): ", this.arguments); + logArguments("Application argument(s): ", applicationArguments.asArray()); } private Map determineEnvironmentVariables() { From 6c016663fdd78308f17dd9895ede593d85890f8d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 26 Dec 2019 11:17:51 +0100 Subject: [PATCH 2/2] Polish contribution See gh-18711 --- .../springframework/boot/maven/AbstractRunMojo.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java index ca6929cd61..b76a6e9588 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java @@ -133,19 +133,20 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { private Map environmentVariables; /** - * Arguments that should be passed to the application. On command line use spaces to - * separate multiple arguments and make sure to wrap multiple values between quotes. + * Arguments that should be passed to the application. * @since 1.0.0 */ @Parameter private String[] arguments; /** - * Arguments from command line that should be passed to the application. - * @since 2.2.1 + * Arguments from the command line that should be passed to the application. Use + * spaces to separate multiple arguments and make sure to wrap multiple values between + * quotes. When specified, takes precedence over {@link #arguments}. + * @since 2.2.3 */ @Parameter(property = "spring-boot.run.arguments", readonly = true) - private String args; + private String commandlineArguments; /** * The spring profiles to activate. Convenience shortcut of specifying the @@ -318,7 +319,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { * @return a {@link RunArguments} defining the application arguments */ protected RunArguments resolveApplicationArguments() { - RunArguments runArguments = (this.args != null) ? new RunArguments(this.args) + RunArguments runArguments = (this.commandlineArguments != null) ? new RunArguments(this.commandlineArguments) : new RunArguments(this.arguments); addActiveProfileArgument(runArguments); return runArguments;