java 如何使用 gradle 生成 swagger.json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39616160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:31:11  来源:igfitidea点击:

How to generate swagger.json using gradle?

javaswaggerswagger-codegen

提问by tbsalling

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

我想使用 swagger-codegen 来生成 REST 客户端和可能的静态 HTML 文档。

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

但是,swagger-codegen 需要 swagger.json 进行输入。

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

我知道,我可以从配备 Swagger 的正在运行的 REST 服务器中获取此信息。

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 curlor a browser to it?

但是有没有办法直接从我的 Java 代码中获取 swagger.json - 即从源代码中用 gradle 生成它 - 而不需要在 web 容器中运行应用程序,并指向curl它或浏览器?

回答by Lachezar Balev

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;
  • 一个展示简约 REST API 的示例 Spring Boot 应用程序;
  • API 方法上的 Swagger 注释;
  • 春狐;
  • Gradle 作为构建工具;

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

我设法使用两种不同的方法将 JSON 规范生成为构建工件:

  1. By using a gradle portof 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.
  1. 通过使用kongchenswagger-maven-plugin 的 gradle 端口
  2. (不确定这是否重要,因为它无论如何都会启动服务器)通过执行生成规范的集成测试(Spring 的模拟 MVC)。我从这里借用了这个想法。

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

我已经在位于此处的一个简单项目中总结了我的研究。见Automation小节。包括代码和示例。

回答by Uladzimir Zhuraulevich

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:

主要思想是将 swagger-maven-plugin 和您的 java 类添加到 classpath 中,以便 buildScript 能够在 gradle 中使用它们,如下所示:

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.

其中依赖项中的第一行从子项目中获取 swagger 库,第二行获取应包含 swagger 注释的类。

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

在此之后,您可以在 gradle 中调用 maven 插件作为一个简单的 java 类:

// 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: '[email protected]',
                                        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()

Hereyou can find this example.

在这里你可以找到这个例子。