Ý tưởng chính là thêm vênh vang-maven-plugin và các lớp học java của bạn vào classpath cho buildscript để có thể sử dụng chúng trong các gradle, một cái gì đó như thế này:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath files(project(':swagger-maven-example').configurations['runtime'].files)
classpath files(project(':swagger-maven-example').sourceSets['main'].output.classesDir)
}
}
đó dòng đầu tiên trong phụ thuộc được swagger libs từ tiểu dự án và dòng thứ hai được các lớp học của bạn mà nên chứa chú thích swagger.
Sau này, bạn có thể gọi Plugin maven trong gradle như là một lớp java đơn giản:
// 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()
Here bạn có thể tìm thấy ví dụ này.
Nguồn
2016-10-05 08:37:59
Tôi vẫn đang xem xét. – tbsalling
https://github.com/gigaSproule/swagger-gradle-plugin Bạn đã thử plugin này chưa? Nó tuyên bố làm chính xác những gì bạn đang yêu cầu. –
Khi sử dụng trình cắm thêm-gradle-plugin, tôi đang gặp lỗi sau: com.fasterxml.jackson.databind.JsonMappingException: Không có nội dung để ánh xạ do kết thúc đầu vào tại [Nguồn: UNKNOWN; dòng: 1, cột: 0] – lex