使用 Maven 3,如何在插件中使用项目类路径?
Using maven 3, how to use project classpath in a plugin?
我正在编写一个 maven 3 插件,我想为此使用项目类路径。
我试过使用Add maven-build-classpath to plugin execution classpath中提到的方法,但maven似乎找不到编写的组件。 (我在插件执行开始时有一个 ComponentNotFoundException
)。
那么,"reference" 在 Maven 3 插件中使用项目类路径的方法是什么?或者如果组件方式是正确的,除了将组件添加为 @Mojo
注释的 configurator
属性 之外是否还有任何配置步骤?
有时看一下功能完全相同的插件代码可以更好地解释它。如果有一个插件需要知道类路径,那就是 maven-compiler-plugin (source)。只需查找 classpathElements
.
您可以通过调用以下方法检索 MavenProject
上的类路径元素:
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
将使插件能够访问具有上述解析范围的项目的依赖项。
我正在编写一个 maven 3 插件,我想为此使用项目类路径。
我试过使用Add maven-build-classpath to plugin execution classpath中提到的方法,但maven似乎找不到编写的组件。 (我在插件执行开始时有一个 ComponentNotFoundException
)。
那么,"reference" 在 Maven 3 插件中使用项目类路径的方法是什么?或者如果组件方式是正确的,除了将组件添加为 @Mojo
注释的 configurator
属性 之外是否还有任何配置步骤?
有时看一下功能完全相同的插件代码可以更好地解释它。如果有一个插件需要知道类路径,那就是 maven-compiler-plugin (source)。只需查找 classpathElements
.
您可以通过调用以下方法检索 MavenProject
上的类路径元素:
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}
属性 注入 requiresDependencyResolution
将使插件能够访问具有上述解析范围的项目的依赖项。
@Parameter
注解