当执行gradle clean然后进行gradle swagger时,抛出ClassNotFoundException。如果随后再次运行gradle swagger(基本上在上次运行中完成api构建之后),则它将正常工作。
build.gradle如下所示:
buildscript {
repositories {
maven { url hydraMavenRepo }
maven { url hydraPluginsRepo }
}
dependencies {
classpath "com.github.kongchen:swagger-maven-plugin:3.1.4"
}
}
apply plugin: 'java'
configurations {
addclasspath
}
dependencies {
addclasspath files(project(':api:desktop-api').configurations['runtime'].files)
addclasspath files(project(':api:desktop-api').sourceSets['main'].output)
addclasspath files(project(':api:desktop-api').sourceSets.main.output.classesDir)
runtime project(':api:desktop-api')
}
sourceSets {
main {
runtimeClasspath += files(project(':api:desktop-api').sourceSets['main'].output)
runtimeClasspath += files(project(':api:desktop-api').sourceSets['main'].output.classesDir)
runtimeClasspath += files(project(':api:desktop-api').configurations['runtime'].files)
}
}
import com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo
import com.github.kongchen.swagger.docgen.mavenplugin.ApiSource
import io.swagger.models.Info
task swagger(dependsOn: [':api:desktop-api:build']) {
doLast {
logger.info 'Swagger GenDoc...'
project.file(reportsDir).mkdirs()
// a trick to have all needed classes in the classpath
def customClassLoader = new GroovyClassLoader()
buildscript.configurations.classpath.each {
//println it.toURI().toURL()
customClassLoader.addURL(it.toURI().toURL())
}
configurations.addclasspath.each {
customClassLoader.addURL(it.toURI().toURL())
}
// the same settings as in the swagger-maven-example/pom.xml
final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo', true, customClassLoader).newInstance(
apiSources: [
new ApiSource(
springmvc: false,
locations: ['com/vmware/vdi/hydra'],
schemes: ['http', 'https'],
host: 'vmware.com',
basePath: '/api',
info: new Info(
title: "Hydra DS-REST API's",
version: 'v100',
description: "Hydra DS-REST API's",
),
swaggerDirectory: reportsDir
)
]
)
mavenTask.execute()
logger.info 'Swagger GenDoc task is completed'
}
}
最佳答案
您的构建脚本中有几个缺陷。
您不应该依赖构建脚本依赖项中的构建事物。这是一个母鸡和鸡蛋的问题。您需要执行构建以获取执行构建所需的类。这根本无法可靠地工作。
相反,您应该将它们声明为buildscript
块之外的依赖项。 buildscript
块仅适用于构建脚本自身运行所需的依赖项,例如Gradle Tasks和Gradle Plugins或您在构建过程中使用的类,例如swagger-maven-plugin
块中正确的buildscript
东西。
除此之外,您还可以在配置阶段而不是在执行阶段执行部分工作(实例化,执行和打印)。您在任务关闭中执行的所有操作,但在doFirst
或doLast
块之外,均在配置阶段运行,在配置了构建之后,因此始终运行,无论您实际上要执行什么任务,也不管该任务是否已经启动至今为止。为了使最新的检查工作并节省您的时间,您需要声明所有输入,例如文件和属性,它们可能在执行和生成的所有输出之间发生变化,然后Gradle可以发挥作用,仅在实际需要时执行任务。
另外,您不应在构建脚本中使用println
。就像在Java程序中使用System.out.println
一样。相反,您应该直接使用提供的日志记录工具,例如。 G。在做logger.info 'Swagger GenDoc task is completed'
。
关于gradle - 使用kongchen swagger-maven-plugin时,对gradle task swagger不可见的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44906015/