Maven和Ant插件智能复制资源 [英] Maven and Ant plugin to copy resources intelligently

查看:27
本文介绍了Maven和Ant插件智能复制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 org.apache.maven.plugins:maven-antrun-plugin 来复制资源并重命名它们很容易,但是有没有一种方法可以使用通配符或其他机制智能地做到这一点以符合个人规则?

It's easy to use org.apache.maven.plugins:maven-antrun-plugin to copy resources and rename them but is there a way to do this intelligently with wildcards or other mecanisms to conform personnal rules ?

例如,如果我有这些文件:

For example if I have those files :

  • ${project.artifactId}-${project.version}.swf
  • a.swf
  • b.swf
  • ...
  • z.swf

我想复制目录中的所有文件,并仅将 ${project.artifactId}-${project.version}.swf 重命名为 foo.swf.我知道我可以使用:

I would like to copy all those files inside directory and rename only ${project.artifactId}-${project.version}.swf to foo.swf. I know I can use :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
       <executions>
          <execution>
             <id>copy-swf-files</id>
             <phase>compile</phase>
                <goals>
                   <goal>run</goal>
                </goals>
                <configuration>
                   <target name="copy swf files to web project">
                       <copy file="${project.build.directory}/${project.artifactId}-${project.version}.swf" tofile="${swf.output.location}/foo.swf" />
                       <copy file="${project.build.directory}/a.swf" tofile="${swf.output.location}/a.swf" />
                       <copy file="${project.build.directory}/z.swf" tofile="${swf.output.location}/z.swf" />
                    </target>
                 </configuration>                       
              </execution>
           </executions>
     </plugin>

这是可行的,但是否有另一种方便的方法来做到这一点,因为如果我要复制 1000 个文件,那将非常无聊...谢谢

it's work but is there another convenient way to do that because if I have 1000 files to copy, it will be very boring...Thanks

推荐答案

你知道 Ant 吗?Maven Ant 插件所做的就是使用您列出的任务调用 Ant.您可以执行任何 Ant 任务,包括 等.

Do you know Ant? All the Maven Ant plugin is doing is calling Ant with the tasks you list. You can do any Ant task including <taskdef>, etc.

看起来你正在做的事情可以用文件集来完成:

What it looks like you're doing can be done with fileset:

 <copy todir="${swf.output.location}">
    <fileset dir="${project.build.directory}">
        <include name="*.swf"/>
    </fileset>
</copy>

这会将位于 ${project.build.directory}(没有子目录)下的所有 *.swf 文件复制到 ${swf.output.location} 目录.如果要复制 *.swf 文件的整个目录树,只需更改 :

This will copy all *.swf files located directly under in the ${project.build.directory} (and no subdirectories) to the ${swf.output.location} directory. If you want to copy the entire directory tree of *.swf files, just change the <include>:

 <copy todir="${swf.output.location}">
    <fileset dir="${project.build.directory}">
        <include name="**/*.swf"/>  <!--NOTE DIFFERENCE HERE-->
    </fileset>
</copy>

如果需要修改文件名,可以使用Mappers.最简单的映射器是 flatten 映射器:

If you need to munge the file names, you can use Mappers. The simplest mapper would be the flatten mapper:

 <copy todir="${swf.output.location}">
    <fileset dir="${project.build.directory}">
        <include name="**/*.swf"/>  <!--NOTE DIFFERENCE HERE-->
    </fileset>
    <mapper type="flatten"/>
</copy>

这将复制整个目录树并将所有文件展平到一个目录中.有些映射器可以匹配 glob、正则表达式甚至脚本.

This will copy the entire directory tree and flatten all the files into a single directory. There are mappers that can match globs, regular expressions, and even scripting.

是一个 选择器 而不是映射器,因为它选择您要操作的文件.这些也可能非常复杂,您可以根据它们的名称 iva 正则表达式,甚至它们的内容来匹配文件.

The <include> is a selector and not a mapper because it selects what files you want to act upon. These too can be quite complex, and you can match file based upon their names iva regular expressions, or even their contents.

我很惊讶没有 Maven 插件允许您执行此操作.

I'm surprised there isn't a Maven plugin that allows you to do this.

这篇关于Maven和Ant插件智能复制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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