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 ?
When Maven's Surefire plugin executes unit tests of a project, developers do not need to provide the classpath containing all dependencies. Instead, Maven sets up the required classpath. Other plugins utilize the Maven generated classpath, too.
Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath. This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.
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:
@Mojo(name = "foo", requiresDependencyResolution = ResolutionScope.TEST)
public class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
try {
getLog().info(project.getCompileClasspathElements().toString());
getLog().info(project.getRuntimeClasspathElements().toString());
getLog().info(project.getTestClasspathElements().toString());
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Error while determining the classpath elements", e);
}
}
}
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.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