Skip to content

Commit

Permalink
Merge pull request #1004 from mhardorf/conf-file-maven-plugin
Browse files Browse the repository at this point in the history
Add support for specifying configuration file in maven plugin
  • Loading branch information
wing328 committed Jul 27, 2015
2 parents fe0e43f + 037f59d commit c01c06b
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@
* limitations under the License.
*/

import io.swagger.codegen.CliOption;
import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.ClientOpts;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.DefaultGenerator;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import config.Config;
import config.ConfigParser;

import java.io.File;
import java.util.ServiceLoader;

Expand Down Expand Up @@ -66,6 +71,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "language", required = true)
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
Expand All @@ -90,7 +101,20 @@ public void execute() throws MojoExecutionException {
if (null != templateDirectory) {
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);
input.setConfig(config);
new DefaultGenerator().opts(input).generate();
Expand Down

0 comments on commit c01c06b

Please sign in to comment.