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()); + } + } } }