Maven和Ant插件可以智能地复制资源

时间:2013-04-18 20:21:23

标签: maven ant

使用org.apache.maven.plugins:maven-antrun-plugin复制资源并重命名它们很容易,但有没有办法通过通配符或其他机制智能地执行此操作以符合个人规则?

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

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

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

<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个文件,那将非常无聊......谢谢

3 个答案:

答案 0 :(得分:15)

你知道蚂蚁吗?所有Maven Ant插件正在使用您列出的任务调用Ant。您可以执行任何Ant任务,包括<taskdef>等。

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

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

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

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

如果您需要使用文件名,可以使用Mappers。最简单的映射器就是展平映射器:

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

这将复制整个目录树并将所有文件压缩到一个目录中。有些映射器可以匹配全局,正则表达式甚至脚本。

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

我很惊讶没有Maven插件允许你这样做。

答案 1 :(得分:3)

好的,我终于使用这个插件来做到这一点:

<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 todir="${swf.output.location}">
                <fileset dir="${project.build.directory}" includes="*.swf">
                    <filename regex="^sim-flex.*"/>
                </fileset>
            <mapper type="regexp" from=".*" to="sim.swf"/>
            </copy>
            <copy todir="${swf.output.location}" >
                <fileset dir="${project.build.directory}" includes="*.swf">
                    <not>
                        <filename regex="^sim-flex.*"/>
                    </not>                                  
                </fileset>
            </copy>
         </target>
      </configuration>                       
   </execution> 
 </executions>
</plugin>

答案 2 :(得分:1)

使用maven-assembly-plugin和“dir”“formats / format”的汇编描述符,一个“fileSets / fileSet”,它将复制除了特殊swf之外的大部分swf,以及一个将复制该文件的“文件/文件”一个swf到一个名字不同的文件。