From fea8699d8b12ee63c627f007767c646e1469f35c Mon Sep 17 00:00:00 2001 From: Simon Marti Date: Mon, 10 Apr 2017 17:25:09 +0200 Subject: [PATCH] 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 --- .../java/io/swagger/codegen/plugin/CodeGenMojo.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java index b3df005872b..32a3b65cd63 100644 --- a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java +++ b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java @@ -381,9 +381,12 @@ public class CodeGenMojo extends AbstractMojo { } } + Map 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 entry : originalEnvironmentVariables.entrySet()) { + if(entry.getValue() == null) { + System.clearProperty(entry.getKey()); + } else { + System.setProperty(entry.getKey(), entry.getValue()); + } + } } }