I have made a Maven plugin that validates and instantiates the classes in the project. When does a Maven plugin have on the classpath the classes in the project?
The pluging keeps throwing a ClassNotFoundException.
Asking the question after looking through the Maven documentation and searching.
Any help would be greatly appreciated. Thank you!
When does a Maven plugin have on the classpath the classes in the project?
The short answer is it doesn't by default.
Have a look at Guide to Maven Classloading
Specifically:
Please note that the plugin classloader does neither contain the dependencies of the current project nor its build output. Instead, plugins can query the project's compile, runtime and test class path from the MavenProject in combination with the mojo annotation requiresDependencyResolution from the Mojo API Specification.
If you are missing classes from a well known artifact, you can add that artifact as a project dependency.
Okay, so I was able to modify the classload by adding the output directory to the ClassRealm for the plugin. This still sounds strange to me, but it works.
final PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
final ClassRealm classRealm = pluginDescriptor.getClassRealm();
final File classes = new File(getProject().getBuild().getOutputDirectory());
try
{
classRealm.addURL(classes.toURI().toURL());
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With