Fix environment variable support in successive Maven plugin executions (#5351)

* Fix environment variable support in successive Maven plugin executions

System properties were retained across multiple successive executions,
resulting in unpredictable behavior. Property values are now properly reset
to their original value after plugin execution.

Fixes #5350

* Add explanation to environment variable reset mechanism in Maven plugin
This commit is contained in:
Simon Marti 2017-04-10 17:25:09 +02:00 committed by wing328
parent 2e46bb9b9f
commit fea8699d8b

View File

@ -381,9 +381,12 @@ public class CodeGenMojo extends AbstractMojo {
}
}
Map<String, String> originalEnvironmentVariables = new HashMap<>();
if (environmentVariables != null) {
for(String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, System.getProperty(key));
String value = environmentVariables.get(key);
if(value == null) {
// don't put null values
@ -431,5 +434,15 @@ public class CodeGenMojo extends AbstractMojo {
String sourceJavaFolder = output.toString() + "/" + sourceFolder;
project.addCompileSourceRoot(sourceJavaFolder);
}
// Reset all environment variables to their original value. This prevents unexpected behaviour
// when running the plugin multiple consecutive times with different configurations.
for(Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if(entry.getValue() == null) {
System.clearProperty(entry.getKey());
} else {
System.setProperty(entry.getKey(), entry.getValue());
}
}
}
}