forked from loafle/openapi-generator-original
Merge pull request #898 from garethjevans/develop_2.0
Contributed maven plugin to the swagger-codegen project
This commit is contained in:
commit
11e56f5c9c
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,6 +9,7 @@ version.properties
|
|||||||
lib/*
|
lib/*
|
||||||
build/*
|
build/*
|
||||||
generated-files/*
|
generated-files/*
|
||||||
|
generated-sources/*
|
||||||
generated-code/*
|
generated-code/*
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
41
modules/swagger-codegen-maven-plugin/README.md
Normal file
41
modules/swagger-codegen-maven-plugin/README.md
Normal file
@ -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
|
||||||
|
<plugin>
|
||||||
|
<groupId>io.swagger</groupId>
|
||||||
|
<artifactId>swagger-codegen-maven-plugin</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>generate</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<inputSpec>src/main/resources/api.yaml</inputSpec>
|
||||||
|
<language>java</language>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
```
|
||||||
|
|
||||||
|
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)
|
87
modules/swagger-codegen-maven-plugin/pom.xml
Normal file
87
modules/swagger-codegen-maven-plugin/pom.xml
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>io.swagger</groupId>
|
||||||
|
<artifactId>swagger-codegen-project</artifactId>
|
||||||
|
<version>2.1.3-SNAPSHOT</version>
|
||||||
|
<relativePath>../..</relativePath>
|
||||||
|
</parent>
|
||||||
|
<artifactId>swagger-codegen-maven-plugin</artifactId>
|
||||||
|
<name>swagger-codegen (maven-plugin)</name>
|
||||||
|
<packaging>maven-plugin</packaging>
|
||||||
|
<description>maven plugin to build modules from swagger codegen</description>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.maven</groupId>
|
||||||
|
<artifactId>maven-core</artifactId>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.maven</groupId>
|
||||||
|
<artifactId>maven-artifact</artifactId>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.maven</groupId>
|
||||||
|
<artifactId>maven-compat</artifactId>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.maven</groupId>
|
||||||
|
<artifactId>maven-plugin-api</artifactId>
|
||||||
|
<version>3.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.maven.plugin-tools</groupId>
|
||||||
|
<artifactId>maven-plugin-annotations</artifactId>
|
||||||
|
<version>3.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger</groupId>
|
||||||
|
<artifactId>swagger-codegen</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-plugin-plugin</artifactId>
|
||||||
|
<version>3.4</version>
|
||||||
|
<configuration>
|
||||||
|
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>mojo-descriptor</id>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>descriptor</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>help-goal</id>
|
||||||
|
<goals>
|
||||||
|
<goal>helpmojo</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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<CodegenConfig> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -397,6 +397,7 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<module>modules/swagger-codegen</module>
|
<module>modules/swagger-codegen</module>
|
||||||
<module>modules/swagger-codegen-cli</module>
|
<module>modules/swagger-codegen-cli</module>
|
||||||
|
<module>modules/swagger-codegen-maven-plugin</module>
|
||||||
<module>modules/swagger-generator</module>
|
<module>modules/swagger-generator</module>
|
||||||
</modules>
|
</modules>
|
||||||
<reporting>
|
<reporting>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user