Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using maven 3, how to use project classpath in a plugin?

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 ?

like image 619
Riduidel Avatar asked Mar 06 '16 17:03

Riduidel


People also ask

Does Maven use classpath?

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.

Does Maven add dependencies to classpath?

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.

What is exec Maven plugin?

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.


1 Answers

You can retrieve the classpath elements on the MavenProject by calling:

  • getCompileClasspathElements() to retrieve the classpath formed by compile scoped dependencies
  • getRuntimeClasspathElements() 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:

  • The MavenProject is injected with the @Parameter annotation using the ${project} property
  • requiresDependencyResolution will enable the plugin accessing the dependencies of the project with the mentioned resolution scope.
like image 61
Tunaki Avatar answered Sep 29 '22 10:09

Tunaki