0

我写了一个 gradle 插件,我确实希望访问项目的源代码并生成一些文件。当我从 Java 运行我的项目时,一切正常,但是当我尝试通过插件执行相同操作时,它不起作用。它没有看到项目的来源。

是真的吗,在 gradle 中,源代码对 buildscript 是不可见的,因此对插件也是不可见的?是否可以使它们可用于插件?

该类用于获取类列表。

public class ClassFinder {
    private final List<? extends Class<?>> classes;

    public ClassFinder(String packageToScan) {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*")));
        Set<BeanDefinition> classes = provider.findCandidateComponents(packageToScan);

        this.classes = classes.stream()
                .map(bean -> {
                    try {
                        return Class.forName(bean.getBeanClassName());
                    } catch (ClassNotFoundException e) {
                        throw new IllegalStateException(e);
                    }
                })
                .collect(Collectors.toList());
    }

    ...
}

我可以在main方法或插件中使用它。在main其中找到我当前项目中的所有类。在插件中它找不到任何东西(库除外)。

4

1 回答 1

1

What you are referring to are not source files, but already compiled classes on the current classpath. Since Gradle compiled those classes, it is clear that they cannot appear on the classpath of the Gradle runtime and its plugins. So it will be impossible to collect the classes of your production code from a Gradle plugin. However, it would be possible to use Gradle to invoke your functionality from Gradle by using an JavaExec task with the classes from the compilation task on the classpath.

于 2018-07-23T09:51:03.040 回答