Merge pull request #12800 from nosan:gh-11065
* pr/12800: Polish "Add support for environment variables" Add support for environment variablespull/12955/merge
commit
9ed169a70b
@ -0,0 +1,38 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.boot.maven.it</groupId>
|
||||
<artifactId>run-envargs</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>
|
||||
<environmentVariables>
|
||||
<ENV1>5000</ENV1>
|
||||
<ENV2>Some Text</ENV2>
|
||||
<ENV3/>
|
||||
<ENV4></ENV4>
|
||||
</environmentVariables>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.test;
|
||||
|
||||
public class SampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
assertEnvValue("ENV1", "5000");
|
||||
assertEnvValue("ENV2", "Some Text");
|
||||
assertEnvValue("ENV3", "");
|
||||
assertEnvValue("ENV4", "");
|
||||
|
||||
System.out.println("I haz been run");
|
||||
}
|
||||
|
||||
private static void assertEnvValue(String envKey, String expectedValue) {
|
||||
String actual = System.getenv(envKey);
|
||||
if (!expectedValue.equals(actual)) {
|
||||
throw new IllegalStateException("env property [" + envKey + "] mismatch "
|
||||
+ "(got [" + actual + "], expected [" + expectedValue + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
def file = new File(basedir, "build.log")
|
||||
return file.text.contains("I haz been run")
|
||||
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 final Map<String, String> variables;
|
||||
|
||||
EnvVariables(Map<String, String> variables) {
|
||||
this.variables = parseEnvVariables(variables);
|
||||
}
|
||||
|
||||
private static Map<String, String> parseEnvVariables(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 (e.getKey() != null) {
|
||||
result.put(e.getKey(), getValue(e.getValue()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String getValue(String value) {
|
||||
return (value != null ? value : "");
|
||||
}
|
||||
|
||||
public Map<String, String> asMap() {
|
||||
return Collections.unmodifiableMap(this.variables);
|
||||
}
|
||||
|
||||
public String[] asArray() {
|
||||
List<String> args = new ArrayList<>(this.variables.size());
|
||||
for (Map.Entry<String, String> arg : this.variables.entrySet()) {
|
||||
args.add(arg.getKey() + "=" + arg.getValue());
|
||||
}
|
||||
return args.toArray(new String[0]);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
-----
|
||||
Using environment variables
|
||||
-----
|
||||
Dmytro Nosan
|
||||
-----
|
||||
2018-04-08
|
||||
-----
|
||||
|
||||
Environment variables can be specified using the <<<environmentVariables>>> attribute.
|
||||
The following sets the 'ENV1', 'ENV2', 'ENV3', 'ENV4' env variables:
|
||||
|
||||
---
|
||||
<project>
|
||||
...
|
||||
<build>
|
||||
...
|
||||
<plugins>
|
||||
...
|
||||
<plugin>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>${project.artifactId}</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<configuration>
|
||||
<environmentVariables>
|
||||
<ENV1>5000</ENV1>
|
||||
<ENV2>Some Text</ENV2>
|
||||
<ENV3/>
|
||||
<ENV4></ENV4>
|
||||
</environmentVariables>
|
||||
</configuration>
|
||||
...
|
||||
</plugin>
|
||||
...
|
||||
</plugins>
|
||||
...
|
||||
</build>
|
||||
...
|
||||
</project>
|
||||
---
|
||||
|
||||
If the value is empty or not defined (i.e. <<<<MY_ENV/>>>>), the env variable is set
|
||||
with an empty String as the value.
|
||||
|
||||
Any String typed Maven variable can be passed as system properties. Any attempt to pass
|
||||
any other Maven variable type (e.g. a <<<List>>> or a <<<URL>>> variable) will cause the
|
||||
variable expression to be passed literally (unevaluated).
|
||||
|
||||
Environment variables defined this way take precedence over existing values.
|
||||
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
/**
|
||||
* Tests for {@link EnvVariables}.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
public class EnvVariablesTests {
|
||||
|
||||
@Test
|
||||
public void asNull() {
|
||||
Map<String, String> args = new EnvVariables(null).asMap();
|
||||
assertThat(args).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asArray() {
|
||||
assertThat(new EnvVariables(getTestArgs()).asArray())
|
||||
.contains("key=My Value", "key1= tt ", "key2= ", "key3=");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asMap() {
|
||||
assertThat(new EnvVariables(getTestArgs()).asMap()).containsExactly(
|
||||
entry("key", "My Value"), entry("key1", " tt "), entry("key2", " "),
|
||||
entry("key3", ""));
|
||||
}
|
||||
|
||||
private Map<String, String> getTestArgs() {
|
||||
Map<String, String> args = new LinkedHashMap<>();
|
||||
args.put("key", "My Value");
|
||||
args.put("key1", " tt ");
|
||||
args.put("key2", " ");
|
||||
args.put("key3", null);
|
||||
return args;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue