如何以编程方式调用maven-resources-plugin
我正在编写一个自定义Maven插件,插件的一部分工作是过滤复制一些资源。
我写的代码看起来像这样:
CopyResourcesMojo rm = new CopyResourcesMojo(); rm.setOutputDirectory(outputDir); //determined dynamically in a loop rm.setOverwrite(true); rm.setFilters(filters); //determined dynamically in a loop rm.setResources(this.resources); //Actually is of type List rm.execute(); //NulPointerException because rm's project member is null.
这会抛出NullPointerException
因为CopyResourceMojo
无权访问我的mojo project
,而CopyResourceMojo
没有我可以使用的setProject()
方法。
我已经研究过使用mojo-executor-maven-plugin
。 我遇到的问题是使用这个库调用插件涉及编写代码,该镜像反映了该插件的XML配置。 我的插件会收到一个资源列表(就像maven-resources-plugin
)所以如果我要使用mojo-executor-maven-plugin
,我想我必须编写代码来评估包含和排除。 我希望为此使用maven-resources-plugin
,因此该行为与将资源列表作为参数的其他插件100%一致。
有没有其他方法从代码调用插件,或将项目注入另一个mojo? 或者其他一些方法来实现这一目标?
我最终使用MavenResourcesFiltering和MavenResourcesExecution找到了一种方法。 文档在这里: http : //maven.apache.org/shared/maven-filtering/usage.html
我能够得到这个代码来做我想要的:
/* Class members */ @Parameter(defaultValue="${project}",required=true, readonly=true) protected MavenProject project; @Parameter( defaultValue = "${session}", required = true, readonly = true ) protected MavenSession session; @Component( role=org.apache.maven.shared.filtering.MavenResourcesFiltering.class, hint="default") protected MavenResourcesFiltering mavenResourcesFiltering; @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" ) protected String encoding; @Parameter protected List resources; ... //Inside my plugin's execute() method: List nonFilteredFileExtensions = new ArrayList (); //resources is a parameter to my plugin, dir and filters are calculated within the plugin MavenResourcesExecution mre = new MavenResourcesExecution(resources, dir, this.project, this.encoding, filters, nonFilteredFileExtensions, session); mavenResourcesFiltering.filterResources(mre);
我通常以这种方式在插件的配置中定义资源:
G:/MyPluginProject/src/test/resources/project-to-test/resources *.DAT true
奇怪的是,对于
,绝对路径工作正常,但我似乎无法使用${basedir}
这样的表达式。 我可能会忽略一些明显的东西……
除了mojo-executor之外,没有直接的方法可以从插件代码中调用另一个插件。
但是,插件可以执行并行生命周期,可以通过自己的XML配置调用其他插件。
http://books.sonatype.com/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html