首页 文章

自定义java gradle插件的思考

提问于
浏览
0

我有一个包含多个模块的gradle项目,每个模块都是根据自己的.jar文件构建的 . 我想在自定义Java gradle插件中使用反射,以生成带注释的类/方法的报告 .

/build.gradle

apply plugin: com.my.plugins.MyPlugin

/buildSrc/build.gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.reflections:reflections:0.9.11'
    compile 'javax.annotation:javax.annotation-api:1.3.2'
}

/buildSrc/src/main/java/com/my/plugins/MyPlugin.java

@NonNullApi
public class MyPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        MyTask myTask = project.getTasks().create("mytask", MyTask.class);
        Task javaCompileTask = project.getTasks().getByName("compileJava");
        myTask.dependsOn(javaCompileTask);
    }
}

/buildSrc/src/main/java/com/my/plugins/MyTask.java

public class MyTask extends DefaultTask {

    @TaskAction
    public void perform() throws MalformedURLException, ClassNotFoundException {

        List<URL> listOfURL = new ArrayList<>();
        Set<File> runtimeFiles = getProject().getConfigurations().findByName("runtime").getFiles();
        for (File file : runtimeFiles) {
            // TODO: Exclude files from gradle cache folder
            if (file.getPath().contains("build\\libs")) {
                listOfURL.add(file.toURI().toURL());
            }
        }

        URL[] urls = listOfURL.toArray(new URL[0]);
        System.out.println("\nURLs:");
        for (URL u : urls) {
            System.out.println("  " + u.toString());
        }

        ClassLoader classLoader = new URLClassLoader(urls, null);
        classLoader.loadClass("com.my.services.sample.MyClass");
        System.out.println("\nClassLoader OK!\n");

        Configuration config = new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forClassLoader(classLoader))
            .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("com.my.services")))
            .addClassLoader(classLoader)
            .setScanners(new SubTypesScanner(),
                    new TypeAnnotationsScanner(),
                    new MethodAnnotationsScanner());

        Reflections reflections = new Reflections(config);

        System.out.println("\nPermitAll Classes\n");
        for (Class c : reflections.getTypesAnnotatedWith(PermitAll.class)) {
            System.out.println(c.getName());
        }

        System.out.println("\nPermitAll Methods\n");
        for (Method m : reflections.getMethodsAnnotatedWith(PermitAll.class)) {
            System.out.println(m.getName());
        }

    }
}

当我运行 gradlew mytask 时,我在URL列表中看到build \ libs下所有.jar文件的列表 .

虽然classLoader可以成功加载(例如)MyClass,但新的Reflections()行会生成大量“无法获取名称类型”错误,例如:

无法从任何类加载器获取名称com.my.services.sample.MyClass的类型

然后,PermitAll类的列表生成正确,但PermitAll方法列表失败并出现异常,例如:

任务':mytask'的执行失败 . > org / dom4j / DocumentException

我已经使用以下作为指南,但我无法取得任何进一步的进展:

https://discuss.gradle.org/t/gradle-plugin-with-reflections/22690

Reflections could not get class type

1 回答

  • 0

    您应该使用以下内容来创建类加载器

    URL[] urls = project.sourceSets.main.runtimeClasspath.files.collect {
        it.toURI().toURL()
    } as URL[]
    ClassLoader classloader = new URLClassLoader(urls, null)
    

相关问题