Skip to content

A Gradle plugin to generate server code, client code and API document from Swagger specification

License

Notifications You must be signed in to change notification settings

hetzijzo/gradle-swagger-generator-plugin

 
 

Repository files navigation

Gradle Swagger Generator Plugin Build Status

This is a Gradle plugin to generate server code, client code and API document, using Swagger Codegen and Swagger2Markup.

See also following examples:

Getting Started: Code Generation

Create following build script.

plugins {
  id 'org.hidetake.swagger.generator' version '1.5.2'
}

repositories {
  jcenter()
}

dependencies {
  // define dependency for Swagger Codegen CLI
  swaggerCodegen 'io.swagger:swagger-codegen-cli:2.2.1'
}

generateSwaggerCode {
  language = 'spring'
  inputFile = file('petstore.yaml')
  configFile = file('config.json')
  components = ['models', 'apis']
}

The task generates source code into build/swagger-code.

% ./gradlew generateSwaggerCode
:generateSwaggerCode
[main] INFO io.swagger.parser.Swagger20Parser - reading from ...

Run the task with Help postfix to show available JSON configuration.

% ./gradlew generateSwaggerCodeHelp
:generateSwaggerCodeHelp
Available JSON configuration for task ':generateSwaggerCode':

CONFIG OPTIONS
	sortParamsByRequiredFlag
  ...

Getting Started: Document Generation

Create following build script.

plugins {
  id 'org.hidetake.swagger.generator' version '1.5.2'
  id 'org.asciidoctor.convert' version '1.5.3'
}

repositories {
  jcenter()
}

generateSwaggerDoc {
  inputFile = file('petstore.yaml')
}

The task generates an API document as build/swagger-doc/index.html.

% ./gradlew generateSwaggerDoc
:generateSwaggerDoc_swagger2markup
:generateSwaggerDoc_asciidoctor
:generateSwaggerDoc

Getting Started: Swagger UI Generation

Create following build script.

plugins {
  id 'org.hidetake.swagger.generator' version '1.5.2'
}

repositories {
  jcenter()
}

dependencies {
    swaggerUI 'org.webjars:swagger-ui:2.2.6'
}

generateSwaggerUI {
  inputFile = file('petstore.yaml')
}

The task generates an API document as build/swagger-ui.

% ./gradlew generateSwaggerUI
:generateSwaggerUI

Custom code generation

Task type GenerateSwaggerCode accepts below properties.

Key Type Value Default value
language String Language to generate. Mandatory
inputFile File Swagger spec file. Mandatory
outputDir File Directory to write generated files. $buildDir/swagger-code
library String Library type. None
configFile File JSON configuration file. None
templateDir File Directory containing the template. None
components List of Strings Components to generate that is a list of models, apis and supportingFiles. All components

The task supports a custom generator class by passing classpath property. See projects under acceptance-test for more.

Custom document generation

Task type GenerateSwaggerDoc accepts below properties.

Key Type Value Default value
inputFile File Swagger spec file. Mandatory
outputDir File Directory to write the generated file. $buildDir/swagger-doc
config Map Configuration for Swagger2Markup. None
options Map Options for Asciidoctor Gradle Plugin. None
attributes Map Attributes for Asciidoctor Gradle Plugin. [toc: 'right', sectnums: '', sectanchors: '']

Task type GenerateSwaggerUI accepts below properties.

Key Type Value Default value
inputFile File Swagger spec file. Mandatory
outputDir File Directory to write the generated file. $buildDir/swagger-ui

See projects under acceptance-test for more.

Contributions

This is an open source software licensed under the Apache License Version 2.0. Feel free to open issues or pull requests.

Travis CI builds the plugin continuously. Following variables should be set.

Environment Variable Purpose
$GRADLE_PUBLISH_KEY Publish the plugin to Gradle Plugins
$GRADLE_PUBLISH_SECRET Publish the plugin to Gradle Plugins
$GITHUB_TOKEN Publish the example of API document to GitHub Pages

About

A Gradle plugin to generate server code, client code and API document from Swagger specification

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 70.4%
  • Groovy 28.7%
  • Scala 0.9%