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

查看:74
本文介绍了如何以编程方式调用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的project,并且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天全站免登陆