如果在目录中找到文件,则为Ant复制目录

时间:2013-03-05 15:32:22

标签: ant

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

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

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

target
    reports
        folder1
        folder2
        folder3
    failures

因此,如果在folder1中发现故障,我想将整个内容复制到故障,然后继续浏览其余文件夹。看起来它应该很简单,但我似乎无法找到一个内置的任务来实现这一点,任何想法?

1 个答案:

答案 0 :(得分:0)

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

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

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