26

I want to use swagger-codegen to generate REST clients and possibly static HTML documentation.

However, swagger-codegen needs swagger.json for input.

I am aware, that I can get this from a running REST server equipped with Swagger.

But is there a way to obtain swagger.json directly from my Java code - i.e. to generate it with gradle from the source code - without the need to run the application in a web container, and pointing curl or a browser to it?

tbsalling
  • 4,477
  • 4
  • 30
  • 51
  • I am still looking into it. – tbsalling Sep 22 '16 at 19:44
  • 1
    https://github.com/gigaSproule/swagger-gradle-plugin Did you try this plugin? It claims to do exactly what you are asking for. – Doron Gold Mar 12 '17 at 00:39
  • On using swagger-gradle-plugin, I am running into the following error: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input at [Source: UNKNOWN; line: 1, column: 0] – lex Dec 21 '17 at 00:49

2 Answers2

9

This is a bit old but I was wondering exactly the same... In short I've started the research with:

  • A sample Spring Boot app exposing minimalistic REST API;
  • Swagger annotations on the API methods;
  • Springfox;
  • Gradle as a build tool;

I managed to generate the JSON spec as a build artifact using two different approaches:

  1. By using a gradle port of the swagger-maven-plugin of kongchen.
  2. (Not sure if this counts, because it starts a server anyways) By executing an integration test (Spring's mock MVC) which generates the specification. I borrowed the idea from here.

I've summarized my research in a simple project located here. See the Automation section. Code and examples are included.

Lachezar Balev
  • 11,498
  • 9
  • 49
  • 72
2

The main idea is to add swagger-maven-plugin and your java classes into classpath for buildScript to be able to use them in the gradle, something like this:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath files(project(':swagger-maven-example').configurations['runtime'].files)
        classpath files(project(':swagger-maven-example').sourceSets['main'].output.classesDir)
    }
}

where the first line in the dependencies gets swagger libs from sub project and the second line gets your classes which should contain swagger annotations.

After this you can invoke maven plugin in the gradle as a simple java class:

// a trick to have all needed classes in the classpath
def customClass = new GroovyClassLoader()

buildscript.configurations.classpath.each {
    // println it.toURI().toURL()
    customClass.addURL(it.toURI().toURL())
}

final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo',true, customClass).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")
                )
        ]
)

// maven plugin
mavenTask.execute()

Here you can find this example.

Bendik
  • 928
  • 11
  • 23