主要思想是将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)
}
}
其中,依赖项中的第一行从子项目中获取 swagger libs,第二行获取应包含 swagger 注释的类。
在此之后,您可以在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: '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()
在这里,您可以找到此示例。