Add support for environment variables
See gh-12800
This commit is contained in:
committed by
Stephane Nicoll
parent
5dd4a7e91e
commit
95f7e3ca37
@@ -24,6 +24,7 @@ import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -48,6 +49,7 @@ import org.springframework.boot.loader.tools.MainClassFinder;
|
||||
* @author Stephane Nicoll
|
||||
* @author David Liu
|
||||
* @author Daniel Young
|
||||
* @author Dmytro Nosan
|
||||
* @see RunMojo
|
||||
* @see StartMojo
|
||||
*/
|
||||
@@ -115,6 +117,15 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
@Parameter
|
||||
private Map<String, String> systemPropertyVariables;
|
||||
|
||||
/**
|
||||
* List of Environment variables that should be associated with the forked process used to run the
|
||||
* application.
|
||||
* <p>NOTE: the use of Environment variables means that processes will be started by forking a
|
||||
* new JVM.
|
||||
*/
|
||||
@Parameter(property = "spring-boot.run.environmentVariables")
|
||||
private Map<String, String> environmentVariables;
|
||||
|
||||
/**
|
||||
* Arguments that should be passed to the application. On command line use commas to
|
||||
* separate multiple arguments.
|
||||
@@ -203,7 +214,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
* @see #logDisabledFork()
|
||||
*/
|
||||
protected boolean enableForkByDefault() {
|
||||
return hasAgent() || hasJvmArgs() || hasWorkingDirectorySet();
|
||||
return hasAgent() || hasJvmArgs() || hasEnvVariables() || hasWorkingDirectorySet();
|
||||
}
|
||||
|
||||
private boolean hasAgent() {
|
||||
@@ -216,6 +227,11 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
&& !this.systemPropertyVariables.isEmpty());
|
||||
}
|
||||
|
||||
private boolean hasEnvVariables() {
|
||||
return (this.environmentVariables != null && !this.environmentVariables.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
private boolean hasWorkingDirectorySet() {
|
||||
return this.workingDirectory != null;
|
||||
}
|
||||
@@ -257,22 +273,27 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
private void doRunWithForkedJvm(String startClassName)
|
||||
throws MojoExecutionException, MojoFailureException {
|
||||
List<String> args = new ArrayList<>();
|
||||
Map<String, String> envVariables = new LinkedHashMap<>();
|
||||
addAgents(args);
|
||||
addJvmArgs(args);
|
||||
addClasspath(args);
|
||||
args.add(startClassName);
|
||||
addArgs(args);
|
||||
runWithForkedJvm(this.workingDirectory, args);
|
||||
addEnvironmentVariables(envVariables);
|
||||
runWithForkedJvm(this.workingDirectory, args, envVariables);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run with a forked VM, using the specified command line arguments.
|
||||
* @param workingDirectory the working directory of the forked JVM
|
||||
* @param args the arguments (JVM arguments and application arguments)
|
||||
* @param environmentVariables the environment variables;
|
||||
* @throws MojoExecutionException in case of MOJO execution errors
|
||||
* @throws MojoFailureException in case of MOJO failures
|
||||
*/
|
||||
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args)
|
||||
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args,
|
||||
Map<String, String> environmentVariables)
|
||||
throws MojoExecutionException, MojoFailureException;
|
||||
|
||||
/**
|
||||
@@ -295,12 +316,29 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
return runArguments;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resolve the environment variables to use.
|
||||
*
|
||||
* @return a {@link EnvVariables} defining the environment variables
|
||||
*/
|
||||
protected EnvVariables resolveEnvVariables() {
|
||||
return new EnvVariables(this.environmentVariables);
|
||||
}
|
||||
|
||||
|
||||
private void addArgs(List<String> args) {
|
||||
RunArguments applicationArguments = resolveApplicationArguments();
|
||||
Collections.addAll(args, applicationArguments.asArray());
|
||||
logArguments("Application argument(s): ", this.arguments);
|
||||
}
|
||||
|
||||
private void addEnvironmentVariables(Map<String, String> environmentVariables) {
|
||||
EnvVariables envVariables = resolveEnvVariables();
|
||||
environmentVariables.putAll(envVariables.asMap());
|
||||
logArguments("Environment variable(s): ", envVariables.asArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the JVM arguments to use.
|
||||
* @return a {@link RunArguments} defining the JVM arguments
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.springframework.boot.maven;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Utility class for working with Env variables.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
class EnvVariables {
|
||||
|
||||
private static final String SPACE = "=";
|
||||
private static final String NO_VALUE = "";
|
||||
|
||||
private final Map<String, String> args = new LinkedHashMap<>();
|
||||
|
||||
EnvVariables(Map<String, String> args) {
|
||||
this.args.putAll(getArgs(args));
|
||||
}
|
||||
|
||||
Map<String, String> asMap() {
|
||||
return Collections.unmodifiableMap(this.args);
|
||||
}
|
||||
|
||||
String[] asArray() {
|
||||
List<String> args = new ArrayList<>(this.args.size());
|
||||
for (Map.Entry<String, String> arg : this.args.entrySet()) {
|
||||
args.add(arg.getKey() + SPACE + arg.getValue());
|
||||
}
|
||||
return args.toArray(new String[args.size()]);
|
||||
}
|
||||
|
||||
|
||||
private Map<String, String> getArgs(Map<String, String> args) {
|
||||
|
||||
if (args == null || args.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
Map<String, String> result = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, String> e : args.entrySet()) {
|
||||
if (hasText(e.getKey())) {
|
||||
result.put(e.getKey(), getValue(e.getValue()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getValue(String value) {
|
||||
if (hasText(value)) {
|
||||
return value;
|
||||
}
|
||||
return NO_VALUE;
|
||||
}
|
||||
|
||||
private boolean hasText(String source) {
|
||||
return source != null && !source.trim().isEmpty();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugins.annotations.Execute;
|
||||
@@ -34,6 +35,7 @@ import org.springframework.boot.loader.tools.RunProcess;
|
||||
* Run an executable archive application.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dmytro Nosan
|
||||
* @author Stephane Nicoll
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@@ -64,14 +66,14 @@ public class RunMojo extends AbstractRunMojo {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runWithForkedJvm(File workingDirectory, List<String> args)
|
||||
protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
|
||||
throws MojoExecutionException {
|
||||
try {
|
||||
RunProcess runProcess = new RunProcess(workingDirectory,
|
||||
new JavaExecutable().toString());
|
||||
Runtime.getRuntime()
|
||||
.addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
|
||||
int exitCode = runProcess.run(true, args.toArray(new String[0]));
|
||||
int exitCode = runProcess.run(true, args.toArray(new String[0]), environmentVariables);
|
||||
if (exitCode == 0 || exitCode == EXIT_CODE_SIGINT) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.net.ConnectException;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import javax.management.MBeanServerConnection;
|
||||
@@ -46,6 +47,7 @@ import org.springframework.boot.loader.tools.RunProcess;
|
||||
* stopped after.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Dmytro Nosan
|
||||
* @since 1.3.0
|
||||
* @see StopMojo
|
||||
*/
|
||||
@@ -88,9 +90,9 @@ public class StartMojo extends AbstractRunMojo {
|
||||
private final Object lock = new Object();
|
||||
|
||||
@Override
|
||||
protected void runWithForkedJvm(File workingDirectory, List<String> args)
|
||||
protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
|
||||
throws MojoExecutionException, MojoFailureException {
|
||||
RunProcess runProcess = runProcess(workingDirectory, args);
|
||||
RunProcess runProcess = runProcess(workingDirectory, args, environmentVariables);
|
||||
try {
|
||||
waitForSpringApplication();
|
||||
}
|
||||
@@ -100,12 +102,12 @@ public class StartMojo extends AbstractRunMojo {
|
||||
}
|
||||
}
|
||||
|
||||
private RunProcess runProcess(File workingDirectory, List<String> args)
|
||||
private RunProcess runProcess(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
|
||||
throws MojoExecutionException {
|
||||
try {
|
||||
RunProcess runProcess = new RunProcess(workingDirectory,
|
||||
new JavaExecutable().toString());
|
||||
runProcess.run(false, args.toArray(new String[0]));
|
||||
runProcess.run(false, args.toArray(new String[0]), environmentVariables);
|
||||
return runProcess;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
||||
Reference in New Issue
Block a user