I wrote a gradle plugin where I do expect to access sources of the project and generate some files. Everything works when I run my project from Java, however when I try to do the same via plugin it does not work. It does not see the sources of the project.
Is it true, that in gradle, sources are not visible to buildscript and therefore to plugin as well? Is it possible to make them available for the plugin?
This class is used to get the list of classes.
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());
}
...
}
I can use it either in main method or in plugin. In main it finds all the classes in my current project. In plugin it does not find anything (except libraries).
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
JavaExectask with the classes from the compilation task on the classpath.