1

我一直在使用简单的副本从目录中抓取一个特定的文件:

<copy todir="target/failures">
    <fileset dir="target/reports" includes="**/*FAILED.txt"/>
</copy>

现在,我想将包含此文件的整个文件夹复制到故障文件夹。目录结构如下所示:

target
    reports
        folder1
        folder2
        folder3
    failures

因此,如果在文件夹 1 中发现故障,我想将整个内容复制到故障中,然后继续浏览剩余的文件夹。看起来应该很简单,但我似乎找不到内置任务来完成此任务,有什么想法吗?

4

1 回答 1

0

使用条件来决定是否执行复制目标。

<project name="demo" default="copy">

    <fileset id="failures" dir="target/reports" includes="**/*FAILED.txt"/>

    <condition property="failures.found">
        <resourcecount refid="failures" when="greater" count="0" />
    </condition>

    <target name="copy" if="failures.found">
        <copy todir="target/failures" overwrite="true">
            <fileset id="failures" dir="target/reports"/>
        </copy>
    </target>

</project>

更新

更强大和更灵活的解决方案使用groovy ANT 任务

<project name="demo" default="copy">

    <path id="build.path">
        <pathelement location="/path/to/task/jars/groovy-all-2.1.1.jar"/>
    </path>

    <target name="copy">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <fileset id="failures" dir="target/reports" includes="**/*FAILED.txt"/>

        <groovy>
            project.references.failures.each {
                def failFile   = new File(it.toString())
                def failFolder = new File(failFile.parent)

                ant.copy(todir:"target/failures/${failFolder.name}", overwrite:true) {
                    fileset(dir:failFolder)
                }
            }
        </groovy>
    </target>

</project>
于 2013-03-05T18:56:12.693 回答