diff --git a/.gitignore b/.gitignore index 24bb9709fc3..a0e39ffd165 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ version.properties lib/* build/* generated-files/* +generated-sources/* generated-code/* *.swp *.swo diff --git a/modules/swagger-codegen-maven-plugin/README.md b/modules/swagger-codegen-maven-plugin/README.md new file mode 100644 index 00000000000..a7eaf852a22 --- /dev/null +++ b/modules/swagger-codegen-maven-plugin/README.md @@ -0,0 +1,41 @@ +swagger-codegen-maven-plugin +============================ + +A Maven plugin to support the [swagger](http://swagger.io) code generation project + +Usage +============================ + +Add to your `build->plugins` section (default phase is `generate-sources` phase) +```xml + + io.swagger + swagger-codegen-maven-plugin + ${project.version} + + + + generate + + + src/main/resources/api.yaml + java + + + + +``` + +Followed by: + +``` +mvn clean compile +``` + +### Configuration parameters + +- `inputSpec` - swagger spec file path +- `language` - target generation language +- `output` - target output path (default is `${project.build.directory}/generated-sources/swagger`) +- `templateDirectory` - directory with mustache templates +- `addCompileSourceRoot` - add the output directory to the project as a source root (`true` by default) diff --git a/modules/swagger-codegen-maven-plugin/pom.xml b/modules/swagger-codegen-maven-plugin/pom.xml new file mode 100644 index 00000000000..7ad4496872c --- /dev/null +++ b/modules/swagger-codegen-maven-plugin/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + + io.swagger + swagger-codegen-project + 2.1.3-SNAPSHOT + ../.. + + swagger-codegen-maven-plugin + swagger-codegen (maven-plugin) + maven-plugin + maven plugin to build modules from swagger codegen + + UTF-8 + + + + org.apache.maven + maven-core + 3.2.5 + + + org.apache.maven + maven-artifact + 3.2.5 + provided + + + org.apache.maven + maven-compat + 3.2.5 + + + org.apache.maven + maven-plugin-api + 3.2.5 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.4 + + + io.swagger + swagger-codegen + ${project.version} + + + junit + junit + 4.12 + test + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.4 + + true + + + + mojo-descriptor + process-classes + + descriptor + + + + help-goal + + helpmojo + + + + + + + + diff --git a/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/AdditionalParams.java b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/AdditionalParams.java new file mode 100644 index 00000000000..aa137cbf6c3 --- /dev/null +++ b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/AdditionalParams.java @@ -0,0 +1,16 @@ +package io.swagger.codegen.plugin; + +/** + * User: lanwen + * Date: 24.03.15 + * Time: 14:47 + */ +public final class AdditionalParams { + public static final String TEMPLATE_DIR_PARAM = "templateDir"; + + private AdditionalParams() { + + } + + +} 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 new file mode 100644 index 00000000000..26b6fef1135 --- /dev/null +++ b/modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java @@ -0,0 +1,118 @@ +package io.swagger.codegen.plugin; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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 java.io.File; +import java.util.ServiceLoader; + +import static io.swagger.codegen.plugin.AdditionalParams.TEMPLATE_DIR_PARAM; + +/** + * Goal which generates client/server code from a swagger json/yaml definition. + */ +@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) +public class CodeGenMojo extends AbstractMojo { + + /** + * Location of the output directory. + */ + @Parameter(name = "output", + property = "swagger.codegen.maven.plugin.output", + defaultValue = "${project.build.directory}/generated-sources/swagger") + private File output; + + /** + * Location of the swagger spec, as URL or file. + */ + @Parameter(name = "inputSpec", required = true) + private String inputSpec; + + /** + * Folder containing the template files. + */ + @Parameter(name = "templateDirectory") + private File templateDirectory; + + /** + * Client language to generate. + */ + @Parameter(name = "language", required = true) + private String language; + + + /** + * Add the output directory to the project as a source root, so that the + * generated java types are compiled and included in the project artifact. + */ + @Parameter(defaultValue = "true") + private boolean addCompileSourceRoot = true; + + /** + * The project being built. + */ + @Parameter(readonly = true, required = true, defaultValue = "${project}") + private MavenProject project; + + @Override + public void execute() throws MojoExecutionException { + Swagger swagger = new SwaggerParser().read(inputSpec); + + CodegenConfig config = forName(language); + config.setOutputDir(output.getAbsolutePath()); + + if (null != templateDirectory) { + config.additionalProperties().put(TEMPLATE_DIR_PARAM, templateDirectory.getAbsolutePath()); + } + + ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger); + input.setConfig(config); + new DefaultGenerator().opts(input).generate(); + + if (addCompileSourceRoot) { + project.addCompileSourceRoot(output.toString()); + } + } + + private CodegenConfig forName(String name) { + ServiceLoader loader = ServiceLoader.load(CodegenConfig.class); + for (CodegenConfig config : loader) { + if (config.getName().equals(name)) { + return config; + } + } + + // else try to load directly + try { + return (CodegenConfig) Class.forName(name).newInstance(); + } catch (Exception e) { + throw new RuntimeException("Can't load config class with name ".concat(name), e); + } + } +} diff --git a/pom.xml b/pom.xml index ca707056230..10bd7340167 100644 --- a/pom.xml +++ b/pom.xml @@ -397,6 +397,7 @@ modules/swagger-codegen modules/swagger-codegen-cli + modules/swagger-codegen-maven-plugin modules/swagger-generator