Skip to content

Commit 0e2a9a3

Browse files
committed
gh-11065. Set up environment variables using the Maven plugin.
1 parent c43eb89 commit 0e2a9a3

File tree

14 files changed

+348
-12
lines changed

14 files changed

+348
-12
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,14 @@ See {spring-boot-maven-plugin-site}/examples/run-debug.html[this example] for mo
27742774
details.
27752775

27762776

2777+
[[howto-set-env-maven-run]]
2778+
=== Run Spring Boot Application with Environment variables Started with Maven
2779+
To set up the environment variables to a Spring Boot application that was started with Maven, you
2780+
can use the `environmentVariables` property of the {spring-boot-maven-plugin-site}[maven plugin].
2781+
2782+
See {spring-boot-maven-plugin-site}/examples/run-with-env.html[this example] for more
2783+
details.
2784+
27772785

27782786
[[howto-build-an-executable-archive-with-ant]]
27792787
=== Build an Executable Archive from Ant without Using `spring-boot-antlib`

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/RunProcess.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.IOException;
2121
import java.util.Arrays;
2222
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.Map;
2325

2426
/**
2527
* Utility used to run a process.
@@ -28,6 +30,7 @@
2830
* @author Dave Syer
2931
* @author Andy Wilkinson
3032
* @author Stephane Nicoll
33+
* @author Dmytro Nosan
3134
* @since 1.1.0
3235
*/
3336
public class RunProcess {
@@ -63,14 +66,19 @@ public RunProcess(File workingDirectory, String... command) {
6366
}
6467

6568
public int run(boolean waitForProcess, String... args) throws IOException {
66-
return run(waitForProcess, Arrays.asList(args));
69+
return run(waitForProcess, Arrays.asList(args), Collections.emptyMap());
6770
}
6871

69-
protected int run(boolean waitForProcess, Collection<String> args)
72+
public int run(boolean waitForProcess, String[] args, Map<String, String> environmentVariables) throws IOException {
73+
return run(waitForProcess, Arrays.asList(args), environmentVariables);
74+
}
75+
76+
protected int run(boolean waitForProcess, Collection<String> args, Map<String, String> environmentVariables)
7077
throws IOException {
7178
ProcessBuilder builder = new ProcessBuilder(this.command);
7279
builder.directory(this.workingDirectory);
7380
builder.command().addAll(args);
81+
builder.environment().putAll(environmentVariables);
7482
builder.redirectErrorStream(true);
7583
builder.inheritIO();
7684
try {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.springframework.boot.maven.it</groupId>
6+
<artifactId>run-envargs</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>@java.version@</maven.compiler.source>
11+
<maven.compiler.target>@java.version@</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<executions>
20+
<execution>
21+
<phase>package</phase>
22+
<goals>
23+
<goal>run</goal>
24+
</goals>
25+
<configuration>
26+
<environmentVariables>
27+
<ENV1>5000</ENV1>
28+
<ENV2>Some Text</ENV2>
29+
<ENV3/>
30+
<ENV4></ENV4>
31+
</environmentVariables>
32+
</configuration>
33+
</execution>
34+
</executions>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
public class SampleApplication {
20+
21+
22+
public static void main(String[] args) {
23+
assertEnvValue("ENV1", "5000");
24+
assertEnvValue("ENV2", "Some Text");
25+
assertEnvValue("ENV3", "");
26+
assertEnvValue("ENV4", "");
27+
28+
System.out.println("I haz been run");
29+
30+
}
31+
32+
33+
static void assertEnvValue(String envKey, String expectedValue) {
34+
String actual = System.getenv(envKey);
35+
if (!expectedValue.equals(actual)) {
36+
throw new IllegalStateException("env property [" + envKey + "] mismatch (got [" + actual + "], expected [" + expectedValue + "]");
37+
}
38+
}
39+
40+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def file = new File(basedir, "build.log")
2+
return file.text.contains("I haz been run")
3+

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.Collections;
27+
import java.util.LinkedHashMap;
2728
import java.util.List;
29+
import java.util.Map;
2830
import java.util.Set;
2931

3032
import org.apache.maven.artifact.Artifact;
@@ -46,6 +48,7 @@
4648
* @author Stephane Nicoll
4749
* @author David Liu
4850
* @author Daniel Young
51+
* @author Dmytro Nosan
4952
* @see RunMojo
5053
* @see StartMojo
5154
*/
@@ -105,6 +108,15 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
105108
@Parameter(property = "spring-boot.run.jvmArguments")
106109
private String jvmArguments;
107110

111+
/**
112+
* List of Environment variables that should be associated with the forked process used to run the
113+
* application.
114+
* <p>NOTE: the use of Environment variables means that processes will be started by forking a
115+
* new JVM.
116+
*/
117+
@Parameter(property = "spring-boot.run.environmentVariables")
118+
private Map<String, String> environmentVariables;
119+
108120
/**
109121
* Arguments that should be passed to the application. On command line use commas to
110122
* separate multiple arguments.
@@ -193,7 +205,7 @@ protected boolean isFork() {
193205
* @see #logDisabledFork()
194206
*/
195207
protected boolean enableForkByDefault() {
196-
return hasAgent() || hasJvmArgs() || hasWorkingDirectorySet();
208+
return hasAgent() || hasJvmArgs() || hasEnvVariables() || hasWorkingDirectorySet();
197209
}
198210

199211
private boolean hasAgent() {
@@ -204,6 +216,11 @@ private boolean hasJvmArgs() {
204216
return (this.jvmArguments != null && !this.jvmArguments.isEmpty());
205217
}
206218

219+
private boolean hasEnvVariables() {
220+
return (this.environmentVariables != null && !this.environmentVariables.isEmpty());
221+
}
222+
223+
207224
private boolean hasWorkingDirectorySet() {
208225
return this.workingDirectory != null;
209226
}
@@ -243,22 +260,27 @@ protected void logDisabledFork() {
243260
private void doRunWithForkedJvm(String startClassName)
244261
throws MojoExecutionException, MojoFailureException {
245262
List<String> args = new ArrayList<>();
263+
Map<String, String> envVariables = new LinkedHashMap<>();
246264
addAgents(args);
247265
addJvmArgs(args);
248266
addClasspath(args);
249267
args.add(startClassName);
250268
addArgs(args);
251-
runWithForkedJvm(this.workingDirectory, args);
269+
addEnvironmentVariables(envVariables);
270+
runWithForkedJvm(this.workingDirectory, args, envVariables);
252271
}
253272

273+
254274
/**
255275
* Run with a forked VM, using the specified command line arguments.
256276
* @param workingDirectory the working directory of the forked JVM
257277
* @param args the arguments (JVM arguments and application arguments)
278+
* @param environmentVariables the environment variables;
258279
* @throws MojoExecutionException in case of MOJO execution errors
259280
* @throws MojoFailureException in case of MOJO failures
260281
*/
261-
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args)
282+
protected abstract void runWithForkedJvm(File workingDirectory, List<String> args,
283+
Map<String, String> environmentVariables)
262284
throws MojoExecutionException, MojoFailureException;
263285

264286
/**
@@ -281,12 +303,29 @@ protected RunArguments resolveApplicationArguments() {
281303
return runArguments;
282304
}
283305

306+
307+
/**
308+
* Resolve the environment arguments to use.
309+
*
310+
* @return a {@link EnvVariables} defining the environment arguments
311+
*/
312+
protected EnvVariables resolveEnvVariables() {
313+
return new EnvVariables(this.environmentVariables);
314+
}
315+
316+
284317
private void addArgs(List<String> args) {
285318
RunArguments applicationArguments = resolveApplicationArguments();
286319
Collections.addAll(args, applicationArguments.asArray());
287320
logArguments("Application argument(s): ", this.arguments);
288321
}
289322

323+
private void addEnvironmentVariables(Map<String, String> environmentVariables) {
324+
EnvVariables envVariables = resolveEnvVariables();
325+
environmentVariables.putAll(envVariables.asMap());
326+
logArguments("Environment variable(s): ", envVariables.asArray());
327+
}
328+
290329
/**
291330
* Resolve the JVM arguments to use.
292331
* @return a {@link RunArguments} defining the JVM arguments
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.maven;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.LinkedHashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
/**
26+
* Helper class for working with Env variables.
27+
*
28+
* @author Dmytro Nosan
29+
*/
30+
class EnvVariables {
31+
32+
private static final String SPACE = "=";
33+
private static final String NO_VALUE = "";
34+
35+
private final Map<String, String> args;
36+
37+
EnvVariables(Map<String, String> args) {
38+
this.args = getArgs(args);
39+
}
40+
41+
public Map<String, String> asMap() {
42+
return Collections.unmodifiableMap(this.args);
43+
}
44+
45+
public String[] asArray() {
46+
List<String> args = new ArrayList<>(this.args.size());
47+
for (Map.Entry<String, String> arg : this.args.entrySet()) {
48+
args.add(arg.getKey() + SPACE + arg.getValue());
49+
}
50+
return args.toArray(new String[args.size()]);
51+
}
52+
53+
54+
private Map<String, String> getArgs(Map<String, String> args) {
55+
56+
if (args == null || args.isEmpty()) {
57+
return Collections.emptyMap();
58+
}
59+
60+
Map<String, String> result = new LinkedHashMap<>();
61+
for (Map.Entry<String, String> e : args.entrySet()) {
62+
result.put(e.getKey(), getValue(e.getValue()));
63+
}
64+
65+
return result;
66+
}
67+
68+
private String getValue(String value) {
69+
if (value == null || value.trim().isEmpty()) {
70+
return NO_VALUE;
71+
}
72+
return value;
73+
}
74+
75+
76+
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunMojo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URL;
2121
import java.net.URLClassLoader;
2222
import java.util.List;
23+
import java.util.Map;
2324

2425
import org.apache.maven.plugin.MojoExecutionException;
2526
import org.apache.maven.plugins.annotations.Execute;
@@ -34,6 +35,7 @@
3435
* Run an executable archive application.
3536
*
3637
* @author Phillip Webb
38+
* @author Dmytro Nosan
3739
* @author Stephane Nicoll
3840
* @author Andy Wilkinson
3941
*/
@@ -64,14 +66,14 @@ protected void logDisabledFork() {
6466
}
6567

6668
@Override
67-
protected void runWithForkedJvm(File workingDirectory, List<String> args)
69+
protected void runWithForkedJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
6870
throws MojoExecutionException {
6971
try {
7072
RunProcess runProcess = new RunProcess(workingDirectory,
7173
new JavaExecutable().toString());
7274
Runtime.getRuntime()
7375
.addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
74-
int exitCode = runProcess.run(true, args.toArray(new String[0]));
76+
int exitCode = runProcess.run(true, args.toArray(new String[0]), environmentVariables);
7577
if (exitCode == 0 || exitCode == EXIT_CODE_SIGINT) {
7678
return;
7779
}

0 commit comments

Comments
 (0)