Skip to content

Commit

Permalink
Merge pull request #18711 from nosan
Browse files Browse the repository at this point in the history
* pr/18711:
  Polish contribution
  Support commas embedded in command line arguments from Maven plugin

Closes gh-18711
  • Loading branch information
snicoll committed Dec 26, 2019
2 parents b268b57 + 6c01666 commit 83e594a
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.systemPropertiesFile=test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-arguments-commandline</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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 + "'");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring-boot.run.arguments=--management.endpoints.web.exposure.include=prometheus,info,health,metrics --spring.profiles.active=foo,bar
Original file line number Diff line number Diff line change
@@ -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'")
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>run-arguments</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<arguments>
<argument>--management.endpoints.web.exposure.include=prometheus,info</argument>
<argument>--spring.profiles.active=foo,bar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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 + "'");
}

}
Original file line number Diff line number Diff line change
@@ -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'")
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,21 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private Map<String, String> 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.
* @since 1.0.0
*/
@Parameter(property = "spring-boot.run.arguments")
@Parameter
private String[] arguments;

/**
* 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 commandlineArguments;

/**
* The spring profiles to activate. Convenience shortcut of specifying the
* 'spring.profiles.active' argument. On command line use commas to separate multiple
Expand Down Expand Up @@ -311,7 +319,8 @@ protected abstract void runWithMavenJvm(String startClassName, String... argumen
* @return a {@link RunArguments} defining the application arguments
*/
protected RunArguments resolveApplicationArguments() {
RunArguments runArguments = new RunArguments(this.arguments);
RunArguments runArguments = (this.commandlineArguments != null) ? new RunArguments(this.commandlineArguments)
: new RunArguments(this.arguments);
addActiveProfileArgument(runArguments);
return runArguments;
}
Expand All @@ -327,7 +336,7 @@ protected EnvVariables resolveEnvVariables() {
private void addArgs(List<String> args) {
RunArguments applicationArguments = resolveApplicationArguments();
Collections.addAll(args, applicationArguments.asArray());
logArguments("Application argument(s): ", this.arguments);
logArguments("Application argument(s): ", applicationArguments.asArray());
}

private Map<String, String> determineEnvironmentVariables() {
Expand Down

0 comments on commit 83e594a

Please sign in to comment.