使用kongchen swagger maven插件时gradle task swagger看不到的类

使用kongchen swagger maven插件时gradle task swagger看不到的类,gradle,swagger,swagger-maven-plugin,Gradle,Swagger,Swagger Maven Plugin,执行gradle clean和gradle swagger时,会抛出一个ClassNotFoundException。如果gradle swagger随后再次运行(基本上是在上一次运行中完成api构建之后),那么它可以正常工作 build.gradle如下所示: buildscript { repositories { maven { url hydraMavenRepo } maven { url hydraPluginsRepo } }

执行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任务和Gradle插件或类,如
swagger maven插件
块中正确的东西

除此之外,您还可以在配置阶段而不是在执行阶段执行部分招摇过市的工作(实例化、执行和打印)。在任务结束时,除了任何
doFirst
doLast
块之外,您在任务结束时所做的一切都将在配置阶段运行,在配置构建时,因此始终运行,无论您实际想要执行什么任务,也不管任务是否已经是最新的。为了使最新检查生效并节省时间,您需要声明所有输入,如在执行和生成的所有输出之间可能发生变化的文件和属性,然后Gradle可以发挥神奇的作用,仅在实际需要时执行任务


此外,不应在生成脚本中使用
println
。这就像在Java程序中使用
System.out.println
。相反,您应该直接使用提供的日志记录工具,例如。G执行
logger.info“Swagger GenDoc任务已完成”

buildscript.classloader就是我要找的

以下是有效的代码:

buildscript {
    repositories {
        maven { url mavenRepo }
    }
    dependencies {
        classpath "com.github.kongchen:swagger-maven-plugin:3.1.4"
    }
}

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:build') {
    doLast {
        logger.info 'Swagger GenDoc...'
        project.file(<dir>).mkdirs()

        FileCollection apiRuntimeFiles = files(project(':api').configurations['runtime'].files)
        apiRuntimeFiles.each {
            buildscript.classLoader.addURL(it.toURI().toURL())
        }

        FileCollection apiClassFiles =files(project(':api').sourceSets['main'].output)
        apiClassFiles.each {
            buildscript.classLoader.addURL(it.toURI().toURL())
        }

        final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo', true, buildscript.classLoader).newInstance(
                apiSources: [
                        new ApiSource(
                                springmvc: false,
                                locations: ['<loc>'],
                                schemes: ['http', 'https'],
                                host: '<host>',
                                basePath: '/api',
                                info: new Info(
                                        title: "REST API's",
                                        version: 'v1',
                                        description: "REST API's",
                                ),
                                swaggerDirectory: <dir>
                        )
                ]
        )
        mavenTask.execute()
        logger.info 'Swagger GenDoc task is completed'
    }
}
buildscript{
存储库{
maven{url mavenRepo}
}
依赖关系{
classpath“com.github.kongchen:swagger-maven插件:3.1.4”
}
}
导入com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo
导入com.github.kongchen.swagger.docgen.mavenplugin.ApiSource
导入io.swagger.models.Info
任务招摇过市(dependsOn:':api:build'){
多拉斯特{
logger.info“大摇大摆的性别…”
project.file().mkdirs()
FileCollection APIRuntTimeFiles=文件(项目(“:api”).configurations['runtime'].files)
apiRuntimeFiles.each{
buildscript.classLoader.addURL(it.toURI().toURL())
}
FileCollection apiClassFiles=文件(项目(':api').sourceSets['main'].output)
apiClassFiles.each{
buildscript.classLoader.addURL(it.toURI().toURL())
}
final apicumentmojo mavenTask=Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.apicumentmojo',true,buildscript.classLoader)。newInstance(
资料来源:[
新蜂毒源(
springmvc:错,
地点:[''],
方案:['http','https'],
主机:“”,
基本路径:'/api',
信息:新信息(
标题:“RESTAPI”,
版本:“v1”,
description:“RESTAPI”,
),
招摇过市目录:
)
]
)
mavenTask.execute()
logger.info“Swagger GenDoc任务已完成”
}
}

您应该编辑此问题,并提供堆栈跟踪和其他支持信息,如如何运行Gradle、工具版本等。谢谢,感谢您的输入。现在已在buildscript之外添加了依赖项。然而,当我看到扫描的URL时,java反射仍然缺少第一次运行时api类的路径。编辑了代码(也遗漏了一篇结语,造成混乱)。关于你关于最新工作的建议,是的,我也会补充,首先我需要这个工作。有任何建议/指针吗?这是因为您使用
文件
解析实际文件。你不应该明确地这样做。如果手头没有项目,很难描述如何正确地执行。您是否尝试过使用路径(目录)而不使用文件,这对解决文件同样没有帮助,因为解决文件问题太早了。您需要使用适当的依赖项声明和引用。但正如我所说,如果手头没有这个项目,除了阅读用户指南并学习如何正确地完成它之外,我不能说更多了,对不起。