run "mvn formatter:format" on maven plug-in

This commit is contained in:
wing328 2017-07-13 15:37:15 +08:00
parent 1e53557570
commit eb498e23b8

View File

@ -3,17 +3,15 @@ package io.swagger.codegen.plugin;
/* /*
* Copyright 2001-2005 The Apache Software Foundation. * Copyright 2001-2005 The Apache Software Foundation.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* you may not use this file except in compliance with the License. * in compliance with the License. You may obtain a copy of the License at
* You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software distributed under the License
* distributed under the License is distributed on an "AS IS" BASIS, * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * or implied. See the License for the specific language governing permissions and limitations under
* See the License for the specific language governing permissions and * the License.
* limitations under the License.
*/ */
import static io.swagger.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp; import static io.swagger.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp;
@ -49,7 +47,7 @@ import io.swagger.codegen.config.CodegenConfigurator;
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class CodeGenMojo extends AbstractMojo { public class CodeGenMojo extends AbstractMojo {
@Parameter(name="verbose", required = false, defaultValue = "false") @Parameter(name = "verbose", required = false, defaultValue = "false")
private boolean verbose; private boolean verbose;
/** /**
@ -61,8 +59,7 @@ public class CodeGenMojo extends AbstractMojo {
/** /**
* Location of the output directory. * Location of the output directory.
*/ */
@Parameter(name = "output", @Parameter(name = "output", property = "swagger.codegen.maven.plugin.output",
property = "swagger.codegen.maven.plugin.output",
defaultValue = "${project.build.directory}/generated-sources/swagger") defaultValue = "${project.build.directory}/generated-sources/swagger")
private File output; private File output;
@ -91,10 +88,10 @@ public class CodeGenMojo extends AbstractMojo {
private File templateDirectory; private File templateDirectory;
/** /**
* Adds authorization headers when fetching the swagger definitions remotely. * Adds authorization headers when fetching the swagger definitions remotely. " Pass in a
" Pass in a URL-encoded string of name:header with a comma separating multiple values * URL-encoded string of name:header with a comma separating multiple values
*/ */
@Parameter(name="auth") @Parameter(name = "auth")
private String auth; private String auth;
/** /**
@ -106,13 +103,13 @@ public class CodeGenMojo extends AbstractMojo {
/** /**
* Specifies if the existing files should be overwritten during the generation. * Specifies if the existing files should be overwritten during the generation.
*/ */
@Parameter(name="skipOverwrite", required=false) @Parameter(name = "skipOverwrite", required = false)
private Boolean skipOverwrite; private Boolean skipOverwrite;
/** /**
* Specifies if the existing files should be overwritten during the generation. * Specifies if the existing files should be overwritten during the generation.
*/ */
@Parameter(name="removeOperationIdPrefix", required=false) @Parameter(name = "removeOperationIdPrefix", required = false)
private Boolean removeOperationIdPrefix; private Boolean removeOperationIdPrefix;
/** /**
@ -197,7 +194,7 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean generateModels = true; private Boolean generateModels = true;
/** /**
* A comma separated list of models to generate. All models is the default. * A comma separated list of models to generate. All models is the default.
*/ */
@Parameter(name = "modelsToGenerate", required = false) @Parameter(name = "modelsToGenerate", required = false)
private String modelsToGenerate = ""; private String modelsToGenerate = "";
@ -209,7 +206,7 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean generateSupportingFiles = true; private Boolean generateSupportingFiles = true;
/** /**
* A comma separated list of models to generate. All models is the default. * A comma separated list of models to generate. All models is the default.
*/ */
@Parameter(name = "supportingFilesToGenerate", required = false) @Parameter(name = "supportingFilesToGenerate", required = false)
private String supportingFilesToGenerate = ""; private String supportingFilesToGenerate = "";
@ -251,8 +248,8 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean skip; private Boolean skip;
/** /**
* Add the output directory to the project as a source root, so that the * Add the output directory to the project as a source root, so that the generated java types
* generated java types are compiled and included in the project artifact. * are compiled and included in the project artifact.
*/ */
@Parameter(defaultValue = "true") @Parameter(defaultValue = "true")
private boolean addCompileSourceRoot = true; private boolean addCompileSourceRoot = true;
@ -277,7 +274,7 @@ public class CodeGenMojo extends AbstractMojo {
@Override @Override
public void execute() throws MojoExecutionException { public void execute() throws MojoExecutionException {
if(skip) { if (skip) {
getLog().info("Code generation is skipped."); getLog().info("Code generation is skipped.");
// Even when no new sources are generated, the existing ones should // Even when no new sources are generated, the existing ones should
// still be compiled if needed. // still be compiled if needed.
@ -285,37 +282,37 @@ public class CodeGenMojo extends AbstractMojo {
return; return;
} }
//attempt to read from config file // attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile);
//if a config file wasn't specified or we were unable to read it // if a config file wasn't specified or we were unable to read it
if(configurator == null) { if (configurator == null) {
configurator = new CodegenConfigurator(); configurator = new CodegenConfigurator();
} }
configurator.setVerbose(verbose); configurator.setVerbose(verbose);
if(skipOverwrite != null) { if (skipOverwrite != null) {
configurator.setSkipOverwrite(skipOverwrite); configurator.setSkipOverwrite(skipOverwrite);
} }
if(removeOperationIdPrefix != null) { if (removeOperationIdPrefix != null) {
configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix); configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix);
} }
if(isNotEmpty(inputSpec)) { if (isNotEmpty(inputSpec)) {
configurator.setInputSpec(inputSpec); configurator.setInputSpec(inputSpec);
} }
if(isNotEmpty(gitUserId)) { if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId); configurator.setGitUserId(gitUserId);
} }
if(isNotEmpty(gitRepoId)) { if (isNotEmpty(gitRepoId)) {
configurator.setGitRepoId(gitRepoId); configurator.setGitRepoId(gitRepoId);
} }
if(isNotEmpty(ignoreFileOverride)) { if (isNotEmpty(ignoreFileOverride)) {
configurator.setIgnoreFileOverride(ignoreFileOverride); configurator.setIgnoreFileOverride(ignoreFileOverride);
} }
@ -323,43 +320,43 @@ public class CodeGenMojo extends AbstractMojo {
configurator.setOutputDir(output.getAbsolutePath()); configurator.setOutputDir(output.getAbsolutePath());
if(isNotEmpty(auth)) { if (isNotEmpty(auth)) {
configurator.setAuth(auth); configurator.setAuth(auth);
} }
if(isNotEmpty(apiPackage)) { if (isNotEmpty(apiPackage)) {
configurator.setApiPackage(apiPackage); configurator.setApiPackage(apiPackage);
} }
if(isNotEmpty(modelPackage)) { if (isNotEmpty(modelPackage)) {
configurator.setModelPackage(modelPackage); configurator.setModelPackage(modelPackage);
} }
if(isNotEmpty(invokerPackage)) { if (isNotEmpty(invokerPackage)) {
configurator.setInvokerPackage(invokerPackage); configurator.setInvokerPackage(invokerPackage);
} }
if(isNotEmpty(groupId)) { if (isNotEmpty(groupId)) {
configurator.setGroupId(groupId); configurator.setGroupId(groupId);
} }
if(isNotEmpty(artifactId)) { if (isNotEmpty(artifactId)) {
configurator.setArtifactId(artifactId); configurator.setArtifactId(artifactId);
} }
if(isNotEmpty(artifactVersion)) { if (isNotEmpty(artifactVersion)) {
configurator.setArtifactVersion(artifactVersion); configurator.setArtifactVersion(artifactVersion);
} }
if(isNotEmpty(library)) { if (isNotEmpty(library)) {
configurator.setLibrary(library); configurator.setLibrary(library);
} }
if(isNotEmpty(modelNamePrefix)) { if (isNotEmpty(modelNamePrefix)) {
configurator.setModelNamePrefix(modelNamePrefix); configurator.setModelNamePrefix(modelNamePrefix);
} }
if(isNotEmpty(modelNameSuffix)) { if (isNotEmpty(modelNameSuffix)) {
configurator.setModelNameSuffix(modelNameSuffix); configurator.setModelNameSuffix(modelNameSuffix);
} }
@ -394,42 +391,49 @@ public class CodeGenMojo extends AbstractMojo {
if (configOptions != null) { if (configOptions != null) {
if(configOptions.containsKey("instantiation-types")) { if (configOptions.containsKey("instantiation-types")) {
applyInstantiationTypesKvp(configOptions.get("instantiation-types").toString(), configurator); applyInstantiationTypesKvp(configOptions.get("instantiation-types").toString(),
configurator);
} }
if(importMappings == null && configOptions.containsKey("import-mappings")) { if (importMappings == null && configOptions.containsKey("import-mappings")) {
applyImportMappingsKvp(configOptions.get("import-mappings").toString(), configurator); applyImportMappingsKvp(configOptions.get("import-mappings").toString(),
configurator);
} }
if(configOptions.containsKey("type-mappings")) { if (configOptions.containsKey("type-mappings")) {
applyTypeMappingsKvp(configOptions.get("type-mappings").toString(), configurator); applyTypeMappingsKvp(configOptions.get("type-mappings").toString(), configurator);
} }
if(configOptions.containsKey("language-specific-primitives")) { if (configOptions.containsKey("language-specific-primitives")) {
applyLanguageSpecificPrimitivesCsv(configOptions.get("language-specific-primitives").toString(), configurator); applyLanguageSpecificPrimitivesCsv(configOptions
.get("language-specific-primitives").toString(), configurator);
} }
if(configOptions.containsKey("additional-properties")) { if (configOptions.containsKey("additional-properties")) {
applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(), configurator); applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(),
configurator);
} }
if(configOptions.containsKey("reserved-words-mappings")) { if (configOptions.containsKey("reserved-words-mappings")) {
applyReservedWordsMappingsKvp(configOptions.get("reserved-words-mappings").toString(), configurator); applyReservedWordsMappingsKvp(configOptions.get("reserved-words-mappings")
.toString(), configurator);
} }
} }
if (importMappings != null && !configOptions.containsKey("import-mappings")) { if (importMappings != null && !configOptions.containsKey("import-mappings")) {
String importMappingsAsString = importMappings.toString(); String importMappingsAsString = importMappings.toString();
applyImportMappingsKvp(importMappingsAsString.substring(0, importMappingsAsString.length() - 1), configurator); applyImportMappingsKvp(
importMappingsAsString.substring(0, importMappingsAsString.length() - 1),
configurator);
} }
if (environmentVariables != null) { if (environmentVariables != null) {
for(String key : environmentVariables.keySet()) { for (String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, System.getProperty(key)); originalEnvironmentVariables.put(key, System.getProperty(key));
String value = environmentVariables.get(key); String value = environmentVariables.get(key);
if(value == null) { if (value == null) {
// don't put null values // don't put null values
value = ""; value = "";
} }
@ -441,19 +445,20 @@ public class CodeGenMojo extends AbstractMojo {
final ClientOptInput input = configurator.toClientOptInput(); final ClientOptInput input = configurator.toClientOptInput();
final CodegenConfig config = input.getConfig(); final CodegenConfig config = input.getConfig();
if(configOptions != null) { if (configOptions != null) {
for (CliOption langCliOption : config.cliOptions()) { for (CliOption langCliOption : config.cliOptions()) {
if (configOptions.containsKey(langCliOption.getOpt())) { if (configOptions.containsKey(langCliOption.getOpt())) {
input.getConfig().additionalProperties().put(langCliOption.getOpt(), input.getConfig().additionalProperties()
configOptions.get(langCliOption.getOpt())); .put(langCliOption.getOpt(), configOptions.get(langCliOption.getOpt()));
} }
} }
} }
if(configHelp) { if (configHelp) {
for (CliOption langCliOption : config.cliOptions()) { for (CliOption langCliOption : config.cliOptions()) {
System.out.println("\t" + langCliOption.getOpt()); System.out.println("\t" + langCliOption.getOpt());
System.out.println("\t " + langCliOption.getOptionHelp().replaceAll("\n", "\n\t ")); System.out.println("\t "
+ langCliOption.getOptionHelp().replaceAll("\n", "\n\t "));
System.out.println(); System.out.println();
} }
return; return;
@ -465,25 +470,30 @@ public class CodeGenMojo extends AbstractMojo {
// I find it annoying to jump through hoops to get basic diagnostic information, // I find it annoying to jump through hoops to get basic diagnostic information,
// so let's log it in any case: // so let's log it in any case:
getLog().error(e); getLog().error(e);
throw new MojoExecutionException("Code generation failed. See above for the full exception."); throw new MojoExecutionException(
"Code generation failed. See above for the full exception.");
} }
addCompileSourceRootIfConfigured(); addCompileSourceRootIfConfigured();
} }
private void addCompileSourceRootIfConfigured() { private void addCompileSourceRootIfConfigured() {
if(addCompileSourceRoot) { if (addCompileSourceRoot) {
final Object sourceFolderObject = configOptions == null ? null : configOptions.get(CodegenConstants.SOURCE_FOLDER); final Object sourceFolderObject =
final String sourceFolder = sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString(); configOptions == null ? null : configOptions
.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder =
sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();
String sourceJavaFolder = output.toString() + "/" + sourceFolder; String sourceJavaFolder = output.toString() + "/" + sourceFolder;
project.addCompileSourceRoot(sourceJavaFolder); project.addCompileSourceRoot(sourceJavaFolder);
} }
// Reset all environment variables to their original value. This prevents unexpected behaviour // Reset all environment variables to their original value. This prevents unexpected
// behaviour
// when running the plugin multiple consecutive times with different configurations. // when running the plugin multiple consecutive times with different configurations.
for(Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) { for (Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if(entry.getValue() == null) { if (entry.getValue() == null) {
System.clearProperty(entry.getKey()); System.clearProperty(entry.getKey());
} else { } else {
System.setProperty(entry.getKey(), entry.getValue()); System.setProperty(entry.getKey(), entry.getValue());