swagger-gradle-plugin:Task.Resolve中的ClassCastException在多个子项目中

时间:2019-02-26 21:18:03

标签: gradle swagger

我在与不同API对应的多个子项目中使用了swagger-gradle-plugin。在每个子项目中,它使用Task.Resolve生成API文档,然后将文档复制到Tomcat登台目录中。这些文档由Web应用程序中托管的Swagger UI使用。在顶级gradle项目中,我有一个任务取决于这些任务。

顶级项目:

task stageSwagger {
    dependsOn { project('subproject-1').tasks.resolve }
    dependsOn { project('subproject-2').tasks.resolve }
    dependsOn { project('subproject-3').tasks.resolve }
}

子项目:

resolve {
    outputFileName = "sample-openapi"
    classpath = getSwaggerClasspath()
    resourceClasses = getSwaggerResourceClasses()
    outputPath = buildDir.toPath()
}

task swagger(type: Copy) {
    dependsOn resolve
    from buildDir
    include 'sample-openapi.json'
    into tomcatStagingDir
}

当我从顶级项目运行task stageSwagger时,我能够成功执行subproject-1:resolve,但是任何后续的resolve任务都会失败,但以下情况除外:

> Task :subproject-1:resolve
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

> Task :subproject-2:resolve FAILED
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':subproject-1:resolve'.
> org.gradle.api.GradleException (no error message)

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':subproject-1:resolve'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:96)

...

Caused by: java.lang.reflect.InvocationTargetException
        at io.swagger.v3.plugins.gradle.tasks.ResolveTask.resolve(ResolveTask.java:388)
        ... 49 more
Caused by: java.lang.ExceptionInInitializerError
        at io.swagger.v3.jaxrs2.integration.JaxrsAnnotationScanner.classes(JaxrsAnnotationScanner.java:73)
        at io.swagger.v3.jaxrs2.integration.JaxrsApplicationAndAnnotationScanner.classes(JaxrsApplicationAndAnnotationScanner.java:10)
        at io.swagger.v3.oas.integration.GenericOpenApiContext.read(GenericOpenApiContext.java:469)
        at io.swagger.v3.jaxrs2.integration.SwaggerLoader.resolve(SwaggerLoader.java:215)
        ... 50 more
Caused by: java.lang.ClassCastException: com.sample.MyLogger cannot be cast to com.sample.MyLogger
        at com.sample.MyLogger.getLogger(MyLogger.java:61)
        at com.sample.MyClient.<clinit>(MyClient.java:64)
        ... 54 more

我对上述异常的猜测是com.sample.MyLogger由类加载器加载,但是在强制转换类时,它使用的是来自不同类加载器的com.sample.MyLogger,因此即使它是同一类,也会导致ClassCastException

MyLogger.java是从java.util.logging.Logger扩展的。 LogManager创建MyLogger的实例。因为LogManager只能返回Logger的实例,所以我必须将MyLogger强制转换为Logger对象,以便使用它的类可以访问MyLogger中的方法。

    public static MyLogger getLogger() {
        synchronized (Logger.class) {
            // Calling class is always at index 1
            String className = new Exception().getStackTrace()[1].getClassName();
            return (MyLogger) getLogger(className);
        }
    }

    public static Logger getLogger(String name) {
        synchronized (Logger.class) {
            LogManager manager = LogManager.getLogManager();
            Logger result = manager.getLogger(name);
            if (result == null) {
                result = new MyLogger(name);
                manager.addLogger(result);
                return manager.getLogger(name);
            }
            return result;
        }
    }

我的问题是:

swagger-gradle-plugin中的错误是它试图从其他类加载器强制转换类吗?

OR

我可以通过不同的方式配置gradle项目,以便在不同的子项目中使用相同的类加载器吗?

0 个答案:

没有答案