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 0415f0648ca..77091969f03 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 @@ -131,88 +131,104 @@ public void execute() throws MojoExecutionException { CodegenConfig config = CodegenConfigLoader.forName(language); config.setOutputDir(output.getAbsolutePath()); + + Map storedEnvironmentVariables = new HashMap(); + + try { - if (environmentVariables != null) { - for(String key : environmentVariables.keySet()) { - String value = environmentVariables.get(key); - if(value == null) { - // don't put null values - value = ""; + if (environmentVariables != null) { + for(String key : environmentVariables.keySet()) { + storedEnvironmentVariables.put(key, System.getProperty(key)); + String value = environmentVariables.get(key); + if(value == null) { + // don't put null values + value = ""; + } + System.setProperty(key, value); } - System.setProperty(key, value); } - } - if (null != library) { - config.setLibrary(library); - } - if (null != templateDirectory) { - config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath()); - } - if (null != modelPackage) { - config.additionalProperties().put(MODEL_PACKAGE_PARAM, modelPackage); - } - if (null != apiPackage) { - config.additionalProperties().put(API_PACKAGE_PARAM, apiPackage); - } - if (null != invokerPackage) { - config.additionalProperties().put(INVOKER_PACKAGE_PARAM, invokerPackage); - } - - Set definedOptions = new HashSet(); - for (CliOption langCliOption : config.cliOptions()) { - definedOptions.add(langCliOption.getOpt()); - } - - if (configOptions != null) { - if(configOptions.containsKey("import-mappings")) { - Map mappings = createMapFromKeyValuePairs(configOptions.remove("import-mappings").toString()); - config.importMapping().putAll(mappings); + if (null != library) { + config.setLibrary(library); } - - if(configOptions.containsKey("type-mappings")) { - Map mappings = createMapFromKeyValuePairs(configOptions.remove("type-mappings").toString()); - config.typeMapping().putAll(mappings); + if (null != templateDirectory) { + config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath()); } - - if(configOptions.containsKey("instantiation-types")) { - Map mappings = createMapFromKeyValuePairs(configOptions.remove("instantiation-types").toString()); - config.instantiationTypes().putAll(mappings); + if (null != modelPackage) { + config.additionalProperties().put(MODEL_PACKAGE_PARAM, modelPackage); } - addAdditionalProperties(config, definedOptions, configOptions); - } - - if (null != configurationFile) { - Config genConfig = ConfigParser.read(configurationFile); - if (null != genConfig) { - addAdditionalProperties(config, definedOptions, genConfig.getOptions()); - } else { - throw new RuntimeException("Unable to read configuration file"); + if (null != apiPackage) { + config.additionalProperties().put(API_PACKAGE_PARAM, apiPackage); } - } - - ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger); - input.setConfig(config); - - if(configHelp) { + if (null != invokerPackage) { + config.additionalProperties().put(INVOKER_PACKAGE_PARAM, invokerPackage); + } + + Set definedOptions = new HashSet(); for (CliOption langCliOption : config.cliOptions()) { - System.out.println("\t" + langCliOption.getOpt()); - System.out.println("\t " + langCliOption.getOptionHelp().replaceAll("\n", "\n\t ")); - System.out.println(); + definedOptions.add(langCliOption.getOpt()); + } + + if (configOptions != null) { + if(configOptions.containsKey("import-mappings")) { + Map mappings = createMapFromKeyValuePairs(configOptions.remove("import-mappings").toString()); + config.importMapping().putAll(mappings); + } + + if(configOptions.containsKey("type-mappings")) { + Map mappings = createMapFromKeyValuePairs(configOptions.remove("type-mappings").toString()); + config.typeMapping().putAll(mappings); + } + + if(configOptions.containsKey("instantiation-types")) { + Map mappings = createMapFromKeyValuePairs(configOptions.remove("instantiation-types").toString()); + config.instantiationTypes().putAll(mappings); + } + addAdditionalProperties(config, definedOptions, configOptions); + } + + if (null != configurationFile) { + Config genConfig = ConfigParser.read(configurationFile); + if (null != genConfig) { + addAdditionalProperties(config, definedOptions, genConfig.getOptions()); + } else { + throw new RuntimeException("Unable to read configuration file"); + } + } + + ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger); + input.setConfig(config); + + if(configHelp) { + for (CliOption langCliOption : config.cliOptions()) { + System.out.println("\t" + langCliOption.getOpt()); + System.out.println("\t " + langCliOption.getOptionHelp().replaceAll("\n", "\n\t ")); + System.out.println(); + } + return; + } + try { + new DefaultGenerator().opts(input).generate(); + } catch (Exception e) { + // Maven logs exceptions thrown by plugins only if invoked with -e + // I find it annoying to jump through hoops to get basic diagnostic information, + // so let's log it in any case: + getLog().error(e); + throw new MojoExecutionException("Code generation failed. See above for the full exception."); + } + + if (addCompileSourceRoot) { + project.addCompileSourceRoot(output.toString()); + } + + } finally { + for (String key : storedEnvironmentVariables.keySet()) { + String value = storedEnvironmentVariables.get(key); + if (value == null) { + System.clearProperty(key); + } else { + System.setProperty(key, value); + } } - return; - } - try { - new DefaultGenerator().opts(input).generate(); - } catch (Exception e) { - // Maven logs exceptions thrown by plugins only if invoked with -e - // I find it annoying to jump through hoops to get basic diagnostic information, - // so let's log it in any case: - getLog().error(e); - throw new MojoExecutionException("Code generation failed. See above for the full exception."); - } - - if (addCompileSourceRoot) { - project.addCompileSourceRoot(output.toString()); } }