Reputation: 65
I have implemented the custom Gradle plugin and it works, but during the Gradle build I have a ReflectionsException: Could not get the type for name org.gradle.api.Plugin
because of Reflections scanning. Why class loader doesn't see the type?
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)
The custom plugin implements Plugin<Project>
like below:
//CommonGradlePlugin
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class CommonGradlePlugin implements Plugin<Project> {
//...
}
Upvotes: 4
Views: 2747
Reputation: 26
Why class loader doesn't see the type?
I suppose that gradle classes are not included in compile/runtime classpath, just because it's a build tool. That's why gradle classes cannot be found by Reflections.
You can exclude package with gradle plugin using Reflection's ConfigurationBuilder:
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);
Upvotes: 1