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

三世轮回 提交于 2019-12-04 09:54:44

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!