使用Maven 3,如何在插件中使用项目类路径? [英] Using maven 3, how to use project classpath in a plugin?

查看:62
本文介绍了使用Maven 3,如何在插件中使用项目类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个maven 3插件,我想为其使用项目类路径.

I'm writing a maven 3 plugin, for which I would like to use project classpath.

我尝试使用将maven-build-classpath添加到插件执行类路径中提到的方法,但是看来Maven找不到书面组件. (我在插件执行开始时有一个ComponentNotFoundException.

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).

那么,在Maven 3插件中使用项目类路径的引用"方式是什么?或者如果组件方式正确,那么除了将组件添加为@Mojo批注的configurator属性之外,是否还有其他配置步骤?

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:

示例MOJO为:

@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);
        }
    }

}

这项工作的成因:

  • MavenProject使用${project}属性注入@Parameter批注
  • requiresDependencyResolution将使插件能够使用上述解决方案范围访问项目的依赖项.
  • 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.

这篇关于使用Maven 3,如何在插件中使用项目类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆