Add support for specifying configuration file in maven plugin

This commit is contained in:
Martin Hardorf 2015-07-23 13:10:59 +02:00
parent cde8d7ba4c
commit 037f59d0df

View File

@ -16,12 +16,14 @@ package io.swagger.codegen.plugin;
* limitations under the License. * limitations under the License.
*/ */
import io.swagger.codegen.CliOption;
import io.swagger.codegen.ClientOptInput; import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.ClientOpts; import io.swagger.codegen.ClientOpts;
import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.DefaultGenerator; import io.swagger.codegen.DefaultGenerator;
import io.swagger.models.Swagger; import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser; import io.swagger.parser.SwaggerParser;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
@ -29,6 +31,9 @@ import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import config.Config;
import config.ConfigParser;
import java.io.File; import java.io.File;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -66,6 +71,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "language", required = true) @Parameter(name = "language", required = true)
private String language; private String language;
/**
* Path to json configuration file.
*/
@Parameter(name = "configurationFile", required = false)
private String configurationFile;
/** /**
* 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
@ -90,7 +101,20 @@ public class CodeGenMojo extends AbstractMojo {
if (null != templateDirectory) { if (null != templateDirectory) {
config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath()); config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath());
} }
if (null != configurationFile) {
Config genConfig = ConfigParser.read(configurationFile);
if (null != genConfig) {
for (CliOption langCliOption : config.cliOptions()) {
if (genConfig.hasOption(langCliOption.getOpt())) {
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
}
}
} else {
throw new RuntimeException("Unable to read configuration file");
}
}
ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger); ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger);
input.setConfig(config); input.setConfig(config);
new DefaultGenerator().opts(input).generate(); new DefaultGenerator().opts(input).generate();