如何以编程方式调用maven-resources-plugin [英] How to call maven-resources-plugin programatically

查看:162
本文介绍了如何以编程方式调用maven-resources-plugin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义Maven插件,插件的一部分工作是过滤复制一些资源。

I am writing a custom Maven plugin and part of the plugin's job is to filter-copy some resources.

我写的代码如下所示:

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.

这会抛出 NullPointerException ,因为 CopyResourceMojo 无法访问我的mojo的项目,以及 CopyResourceMojo 没有 setProject()我可以使用的方法。

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.

研究使用 mojo-executor-maven-plugin 。我遇到的问题是使用这个库调用插件涉及编写代码,该镜像反映了该插件的XML配置。我的插件会收到一个资源列表(就像 maven-resources-plugin 那样),所以如果我打算使用 mojo-executor-maven -plugin ,我想我必须编写代码来评估包含和排除。我希望为此使用 maven-resources-plugin ,以便该行为与将资源列表作为参数的其他插件100%一致。

I have looked into using 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. 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. 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.

有没有其他方法可以从代码中调用插件,或者将项目注入另一个mojo?或者其他一些方法来实现这个目标?

Is there any other way to call a plugin from code, or to inject a project into another mojo? Or some other way to accomplish this?

推荐答案

我最终找到了一种方法,使用MavenResourcesFiltering和MavenResourcesExecution来完成此任务。文档在这里: http://maven.apache.org/shared/maven-filtering /usage.html

I did eventually find a way to do this using MavenResourcesFiltering and MavenResourcesExecution. Document is here: 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>

奇怪的是< directory> ,绝对路径工作正常,但我似乎无法使用表达式,如 $ {basedir} 。我可能会忽略一些明显的东西......

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}. I'm probably overlooking something obvious here...

这篇关于如何以编程方式调用maven-resources-plugin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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