Logo Questions Linux Laravel Mysql Ubuntu Git Menu

When does a Maven plugin have access to the projects classes?

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!

like image 285
Thomas Beauvais Avatar asked Mar 21 '23 21:03

Thomas Beauvais

2 Answers

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


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.

like image 125
Ori Dar Avatar answered Apr 07 '23 12:04

Ori Dar

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());
catch (MalformedURLException e)
like image 40
Thomas Beauvais Avatar answered Apr 07 '23 13:04

Thomas Beauvais