How to call maven-resources-plugin programmatically

耗尽温柔 提交于 2019-12-05 21:42:58

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>

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...

Besides the mojo-executor there is no direct way to call another plugin from plugin code.

However plugins can execute a parallel lifecycle which can call other plugins via its own XML configuration.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!