Skip to content

A very easy way how to use swagger-maven-plugin in gradle projects.

License

Notifications You must be signed in to change notification settings

zhurlik/swagger-gradle-example

Repository files navigation

This example has been converted to gradle-swagger-plugin

https://github.com/zhurlik/gradle-swagger-plugin

swagger-gradle-example

A very easy way how to use swagger-maven-plugin in gradle projects. The idea is to reuse existing swagger-maven-plugin without any new gradle plugins and classes.

Projects

There 2 sub projects:

  • generate-doc to generate docs for Rest API.
  • link to original swagger-maven-example just to have sources for scanning swagger annotations.

NOTE:
If you would like to compare both results generated by original maven project and gradle project then you need to modify pom.xml and java sources to use the latest plugins:

...
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.4</version>
...
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-core</artifactId>
        <version>1.5.10</version>
...

How to execute swagger-maven-plugin

You can use Groovy to specify settings for swagger-maven-plugin

    final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo',true, buildscript.classLoader).newInstance(
            apiSources: [
                    new ApiSource(
                            springmvc: false,
                            locations: ['com/github/kongchen/swagger/sample/wordnik/resource'],
                            schemes: ['http', 'https'],
                            host: 'petstore.swagger.wordnik.com',
                            basePath: '/api',
                            info: new Info(
                                    title: 'Swagger Maven Plugin Sample',
                                    version: 'v1',
                                    description: 'This is a sample for swagger-maven-plugin',
                                    termsOfService: 'http://www.github.com/kongchen/swagger-maven-plugin',
                                    contact: new Contact(
                                            email: 'kongchen@gmail.com',
                                            name: 'Kong Chen',
                                            url: 'http://kongch.com'
                                    ),
                                    license: new License(
                                            url: 'http://www.apache.org/licenses/LICENSE-2.0.html',
                                            name: 'Apache 2.0'
                                    )
                            ),
                            outputPath: file("${buildDir}/swagger/document.html").path,
                            swaggerDirectory: file("${buildDir}/swagger/swagger-ui").path,
                            templatePath: file("${project(':swagger-maven-example').projectDir}/templates/strapdown.html.hbs")
                    )
            ]
    )

    mavenTask.execute()

However there is a small trick for adding into classpath all required libs and compiled classes which will be used by swagger-maven-plagin.
The following code shows how to add swagger-maven-plugin to the gradle script and how to add a reference to classes to be scanned by swagger plugin.

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath group: 'io.swagger', name: 'swagger-core', version: '1.5.10'
        classpath group: 'com.github.kongchen', name: 'swagger-maven-plugin', version: '3.1.4'
        
        // java.xml.bind (JAXB) - REMOVED in JAVA 11    
        if (JavaVersion.current() == JavaVersion.VERSION_11) {    
            classpath group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'    
            classpath group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0.1'    
            classpath group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.5'    
        }    
    }
}

Gradle task

You can create a simple gradle task with custom GroovyClassLoader that loads all classes and executes swagger.

task swagger(dependsOn: ':swagger-maven-example:build') << {
    logger.quiet '>> Swagger GenDoc...'

    // a trick to have all needed project classes in the classpath
    buildscript.classLoader.addURL project(':swagger-maven-example').sourceSets['main'].output.classesDir.toURI().toURL()
    ...

About

A very easy way how to use swagger-maven-plugin in gradle projects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published