【问题标题】:ReflectionsException: Could not get type for name org.gradle.api.PluginReflectionsException:无法获取名称 org.gradle.api.Plugin 的类型
【发布时间】:2019-01-28 15:45:26
【问题描述】:

我已经实现了自定义 Gradle 插件并且它可以工作,但是在 Gradle 构建期间,由于反射扫描,我有一个 ReflectionsException: Could not get the type for name org.gradle.api.Plugin。为什么类加载器看不到类型?

Caused by: java.lang.ClassNotFoundException: org.gradle.api.Plugin
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.reflections.ReflectionUtils.forName(ReflectionUtils.java:388)

自定义插件实现Plugin<Project>,如下所示:

//CommonGradlePlugin
import org.gradle.api.Plugin;
import org.gradle.api.Project; 

public class CommonGradlePlugin implements Plugin<Project> {
    //...
}

【问题讨论】:

    标签: java gradle gradle-plugin


    【解决方案1】:

    为什么类加载器看不到类型?

    我认为 gradle 类不包含在编译/运行时类路径中,只是因为它是一个构建工具。这就是 Reflections 找不到 gradle 类的原因。

    您可以使用 Reflection 的 ConfigurationBuilder 排除带有 gradle 插件的包:

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    FilterBuilder filterBuilder = new FilterBuilder();
    
    configurationBuilder.addUrls(ClasspathHelper.forPackage(PACKAGE_TO_SCAN));
    filterBuilder
            .includePackage(PACKAGE_TO_SCAN)
            .excludePackage(PACKAGE_TO_EXCLUDE);
    configurationBuilder.filterInputsBy(filterBuilder);
    
    Reflections reflections = new Reflections(configurationBuilder);
    

    【讨论】:

    • 就我而言,它有效,但由于许多地方都使用了反射,我认为最好将带有 gradle 内容的包从基础包中移走。就像 WA 一样,可以添加如下所示的 gradle API 依赖项:implementation gradleApi()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2019-09-30
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多