I am writing some code-gen maven-plugin.
I need my project classpath be injected in to my plugin execution classpath.
I found this article. The solution ther
I took this approach and apparently it's working:
1 - a MavenProject parameter is needed within your Mojo class:
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
2 - and then you can get the classpath elements from project instance:
try {
Set urls = new HashSet<>();
List elements = project.getTestClasspathElements();
//getRuntimeClasspathElements()
//getCompileClasspathElements()
//getSystemClasspathElements()
for (String element : elements) {
urls.add(new File(element).toURI().toURL());
}
ClassLoader contextClassLoader = URLClassLoader.newInstance(
urls.toArray(new URL[0]),
Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(contextClassLoader);
} catch (DependencyResolutionRequiredException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}