Add system properties support using explicit configuration tag

See gh-10741
This commit is contained in:
Vadeg
2017-10-21 13:18:52 +02:00
committed by Stephane Nicoll
parent fe497d70b8
commit ba34bf6470
7 changed files with 205 additions and 5 deletions

View File

@@ -25,7 +25,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
@@ -169,6 +171,15 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
@Parameter(property = "spring-boot.run.skip", defaultValue = "false")
private boolean skip;
/**
* List of JVM system properties. System property consists of key and value
* and it will be transformed to <code>-Dkey=value</code> format.
* In case if value is not specified or empty only key will be provided.
* @since 2.0
*/
@Parameter(property = "spring-boot.run.systemPropertyVariabled")
private Map<String, String> systemPropertyVariables;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.skip) {
@@ -201,7 +212,8 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
private boolean hasJvmArgs() {
return (this.jvmArguments != null && !this.jvmArguments.isEmpty());
return (this.jvmArguments != null && !this.jvmArguments.isEmpty()) ||
(this.systemPropertyVariables != null && !this.systemPropertyVariables.isEmpty());
}
private boolean hasWorkingDirectorySet() {
@@ -232,8 +244,22 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
getLog().warn("Fork mode disabled, ignoring agent");
}
if (hasJvmArgs()) {
getLog().warn("Fork mode disabled, ignoring JVM argument(s) ["
+ this.jvmArguments + "]");
String messageTemplate = "Fork mode disabled, ignoring JVM argument(s) [%s%s]";
String sysPropsStr = "";
if (this.systemPropertyVariables != null && !this.systemPropertyVariables.isEmpty()) {
sysPropsStr = this.systemPropertyVariables
.entrySet()
.stream()
.map(e -> SystemPropertyFormatter.format(e.getKey(), e.getValue()))
.collect(Collectors.joining(" "));
}
String message = String.format(
messageTemplate,
this.jvmArguments,
sysPropsStr.isEmpty() ? sysPropsStr : " " + sysPropsStr
);
getLog().warn(message);
}
if (hasWorkingDirectorySet()) {
getLog().warn("Fork mode disabled, ignoring working directory configuration");
@@ -292,7 +318,22 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
* @return a {@link RunArguments} defining the JVM arguments
*/
protected RunArguments resolveJvmArguments() {
return new RunArguments(this.jvmArguments);
final StringBuilder stringBuilder = new StringBuilder();
if (this.jvmArguments != null) {
stringBuilder.append(this.jvmArguments);
}
if (this.systemPropertyVariables != null) {
String result = this.systemPropertyVariables
.entrySet()
.stream()
.map(e -> SystemPropertyFormatter.format(e.getKey(), e.getValue()))
.collect(Collectors.joining(" "));
stringBuilder
.append(" ")
.append(result);
}
return new RunArguments(stringBuilder.toString());
}
private void addJvmArgs(List<String> args) {
@@ -509,4 +550,23 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
}
/**
* System properties formatter.
*/
static class SystemPropertyFormatter {
private static final String NO_VALUE_FORMAT = "-D%s";
private static final String KEY_VALUE_FORMAT = NO_VALUE_FORMAT + "=%s";
public static String format(Object key, Object value) {
if (key == null) {
return "";
}
if (value == null || String.valueOf(value).trim().isEmpty()) {
return String.format(NO_VALUE_FORMAT, key);
}
return String.format(KEY_VALUE_FORMAT, key, value);
}
}
}