I'm writing a maven 3 plugin, for which I would like to use project classpath.
I've tried using the approach mentionned in Add maven-build-classpath to plugin execution classpath, but it seems the written component is not found by maven. (I have a ComponentNotFoundException
at plugin execution start).
So, what is the "reference" way to use project classpath in a maven 3 plugin ? Or if the component way is the right one, is there any configuration step beside adding the component as configurator
property of the @Mojo
annotation ?
You can retrieve the classpath elements on the
MavenProject
by calling:getCompileClasspathElements()
to retrieve the classpath formed by compile scoped dependenciesgetRuntimeClasspathElements()
to retrieve the classpath formed by runtime scoped dependencies (i.e. compile + runtime)getTestClasspathElements()
to retrieve the classpath formed by test scoped dependencies (i.e. compile + system + provided + runtime + test)A sample MOJO would be:
What makes this work:
MavenProject
is injected with the@Parameter
annotation using the${project}
propertyrequiresDependencyResolution
will enable the plugin accessing the dependencies of the project with the mentioned resolution scope.Sometimes having a look at the code of a plugin which does exactly the same explains it a lot better. If there's one plugin which needs to know classpaths it's the maven-compiler-plugin (source). Just look for
classpathElements
.