当文件集包含与globmapper模式不匹配的文件时,如何强制错误

时间:2015-12-13 02:45:29

标签: ant

以下代码演示ant文件copy任务globmapper的行为。我想复制一组文件。复制某些文件时需要重命名与模式匹配的选择文件。我认为文件匹配模式将使用新名称和其他原始名称进行复制。

这是错误的:如果源集包含的文件与模式不匹配,则静默会被忽略。

<project name="trial" default="test" basedir=".">
    <target name="test">
        <property name="mydir" value="${java.io.tmpdir}/demo/src"/>
        <mkdir dir="${mydir}" />
        <touch file="${mydir}/abc0.txt"/>
        <touch file="${mydir}/abc1.java"/>
        <touch file="${mydir}/abc2.java"/>

        <property name="dest" value="${java.io.tmpdir}/demo/dest"/>
        <copy todir="${dest}" >
            <fileset dir="${mydir}">
                <include name="**/*.java"/>
                <include name="**/*.txt"/>
            </fileset>
            <globmapper from="*.txt" to="*.bak"/>
        </copy>
    </target>

    <target name="clean" >
        <delete dir="${java.io.tmpdir}/demo"/>
    </target>
</project>

简单的ant输出并不表示某些文件被复制。

test:
    [mkdir] Created dir: C:\cygwin64\tmp\demo\src
    [touch] Creating C:\cygwin64\tmp\demo\src\abc0.txt
    [touch] Creating C:\cygwin64\tmp\demo\src\abc1.java
    [touch] Creating C:\cygwin64\tmp\demo\src\abc2.java
     [copy] Copying 1 file to C:\cygwin64\tmp\demo\dest

BUILD SUCCESSFUL
Total time: 0 seconds

调试ant -v

的输出
Setting project property: mydir -> C:\cygwin64\tmp\/demo/src                                                                     
    [mkdir] Created dir: C:\cygwin64\tmp\demo\src                                                                                
    [touch] Creating C:\cygwin64\tmp\demo\src\abc0.txt                                                                           
    [touch] Creating C:\cygwin64\tmp\demo\src\abc1.java                                                                          
    [touch] Creating C:\cygwin64\tmp\demo\src\abc2.java                                                                          
Setting project property: dest -> C:\cygwin64\tmp\/demo/dest                                                                     
fileset: Setup scanner in dir C:\cygwin64\tmp\demo\src with patternSet{ includes: [**/*.java, **/*.txt] excludes: [] }           
     [copy] abc0.txt added as abc0.bak doesn't exist.                                                                            
     [copy] C:\cygwin64\tmp\demo\src\abc1.java skipped - don't know how to handle it                                             
     [copy] C:\cygwin64\tmp\demo\src\abc2.java skipped - don't know how to handle it                                             
     [copy] No sources found.                                                                                                    
     [copy] Copying 1 file to C:\cygwin64\tmp\demo\dest                                                                          
     [copy] Copying C:\cygwin64\tmp\demo\src\abc0.txt to C:\cygwin64\tmp\demo\dest\abc0.bak                                      

BUILD SUCCESSFUL                                                                                                                 
Total time: 0 seconds   

我现在理解了行为(以及可能的修复),但是在这种情况下我可以强制ant出错吗?

1 个答案:

答案 0 :(得分:1)

<firstmatchmapper>包含应用于文件的嵌套映射器,直到其中一个嵌套映射器匹配...

<copy todir="${dest}" >
    <fileset dir="${mydir}">
        <include name="**/*.java"/>
        <include name="**/*.txt"/>
    </fileset>
    <firstmatchmapper>
        <globmapper from="*.txt" to="*.bak"/>
        <identitymapper/>
    </firstmatchmapper>
</copy>

输出

 [copy] Copying 3 files to C:\cygwin\tmp\demo\dest
 [copy] Copying C:\cygwin\tmp\demo\src\abc0.txt to C:\cygwin\tmp\demo\dest\abc0.bak
 [copy] Copying C:\cygwin\tmp\demo\src\abc1.java to C:\cygwin\tmp\demo\dest\abc1.java
 [copy] Copying C:\cygwin\tmp\demo\src\abc2.java to C:\cygwin\tmp\demo\dest\abc2.java

请注意,abc0.txt的扩展名已更改为.bak,但其他文件的.java扩展名保持不变。

解释

在上面的示例中,<firstmatchmapper>包含两个映射器:

  1. <globmapper from="*.txt" to="*.bak"/>
  2. <identitymapper/>
  3. abc0.txt

    1. abc0.txt匹配<globmapper from="*.txt" ...>,因此应用<globmapper from="*.txt" to="*.bak"/>来获取abc0.bak
    2. 由于已经匹配,因此会跳过<identitymapper/>
    3. abc1.java

      1. abc1.java<globmapper from="*.txt" ...>不匹配,因此会跳过此映射器。
      2. 由于尚未匹配,因此<identitymapper/>已应用。