问题描述
我正在编写一个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 ?
推荐答案
您可以在 MavenProject
通过调用:
You can retrieve the classpath elements on the MavenProject
by calling:
-
getCompileClasspathElements()
检索由编译范围内的依赖项形成的类路径 -
getRuntimeClasspathElements()
检索由运行时范围内的依赖关系(即编译+运行时)形成的类路径 -
getTestClasspathElements()
检索由测试范围的依赖关系(即编译+系统+提供的+运行时+测试)形成的类路径
示例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,如何在插件中使用项目类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!