简体   繁体   English

如何以编程方式调用 maven-resources-plugin

[英]How to call maven-resources-plugin programmatically

I am writing a custom Maven plugin and part of the plugin's job is to filter-copy some resources.我正在编写一个自定义 Maven 插件,该插件的部分工作是过滤复制一些资源。

The code I've written looks like this:我写的代码是这样的:

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<Resource>
rm.execute(); //NulPointerException because rm's project member is null.

This throws a NullPointerException because the CopyResourceMojo doesn't have access to my mojo's project , and the CopyResourceMojo doesn't have a setProject() method I could use.这将引发NullPointerException因为CopyResourceMojo无权访问我的 mojo 的project ,并且CopyResourceMojo没有我可以使用的setProject()方法。

I have looked into using mojo-executor-maven-plugin .已经研究过使用mojo-executor-maven-plugin The problem I have with it is that using this library to call a plugin involves writing code that mirrors what the XML config would be for that plugin.我遇到的问题是,使用这个库来调用插件涉及编写代码来反映该插件的 XML 配置。 My plugin will receive a list of resources (just like the maven-resources-plugin could) so if I am going to use mojo-executor-maven-plugin , I think I would have to write code to evaluate the inclusions and exclusions.我的插件将收到一个资源列表(就像maven-resources-plugin )所以如果我要使用mojo-executor-maven-plugin ,我想我将不得不编写代码来评估包含和排除。 I was hoping to make use of the maven-resources-plugin for this, so that behaviour is 100% consistent with other plugins that take resource lists as parameters.我希望为此使用maven-resources-plugin ,因此该行为与其他将资源列表作为参数的插件 100% 一致。

Is there any other way to call a plugin from code, or to inject a project into another mojo?有没有其他方法可以从代码中调用插件,或者将项目注入另一个 mojo? Or some other way to accomplish this?或者其他一些方法来实现这一点?

I did eventually find a way to do this using MavenResourcesFiltering and MavenResourcesExecution.我最终找到了一种使用 MavenResourcesFiltering 和 MavenResourcesExecution 来做到这一点的方法。 Document is here: http://maven.apache.org/shared/maven-filtering/usage.html文档在这里: http : //maven.apache.org/shared/maven-filtering/usage.html

I was able to get this code to do what I wanted:我能够让这段代码做我想做的事:

/* 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<Resource> resources;


...

//Inside my plugin's execute() method: 
List<String> nonFilteredFileExtensions = new ArrayList<String>();
//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);

And I define resources in my plugin's configuration the usual way:我以通常的方式在我的插件配置中定义资源:

<resources>
    <resource>
        <directory>G:/MyPluginProject/src/test/resources/project-to-test/resources</directory>
        <excludes>
            <exclude>*.DAT</exclude>
        </excludes>
        <filtering>true</filtering>
    </resource>
</resources>

What's odd is that for <directory> , absolute paths work fine but I can't seem to get it to work with an expression such as ${basedir} .奇怪的是,对于<directory> ,绝对路径工作正常,但我似乎无法让它与诸如${basedir}的表达式一起使用。 I'm probably overlooking something obvious here...我可能在这里忽略了一些明显的东西......

Besides the mojo-executor there is no direct way to call another plugin from plugin code.除了 mojo-executor 之外,没有直接的方法可以从插件代码调用另一个插件。

However plugins can execute a parallel lifecycle which can call other plugins via its own XML configuration.然而,插件可以执行一个并行生命周期,它可以通过自己的 XML 配置调用其他插件。

http://books.sonatype.com/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html http://books.sonatype.com/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM