ANT如何仅递归删除空目录 [英] ANT How to delete ONLY empty directories recursively

查看:106
本文介绍了ANT如何仅递归删除空目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用ANT递归删除空"目录(空包括仅包含".svn"等的目录).

Does anyone know how to recursively delete "empty" directories with ANT (empty includes directories that only contain ".svn" etc).

我知道ant允许您"includeEmptyDirs = true",但我希望它仅在目录为空的情况下删除该目录(实际上,我可能需要遍历递归链并删除目录中包含的目录,如果现在为空).

I know ant allows you to "includeEmptyDirs=true" but I want it to ONLY delete a directory if it is empty (and actually I'd probably need to walk up the recursive chain and delete the directory it was contained in if it is now empty).

基本上,在构建过程中,我们复制一组目录,其中包含一堆其他嵌套目录,这些目录包含各种XML和数据,并且在移动数据位置时,我们的复制"和签入构建过程不会执行不能真正起作用,并且因为我们正在检查另一个源代码控制(SVN),所以清除目录并进行复制也不是一种选择(我们将删除".svn"文件夹).

Basically as part of our build process we copy over a set of directories that contain a bunch of other nested directories containing various XML and data, and when we move the location for that data our "copy" and checkin build process doesn't really work, and because we are checking into another source control (SVN), wiping out the directories and copying over isn't really an option either (we'd be blowing away the ".svn" folders).

在复制新版本之前,我可以通过执行以下操作清除"目录:

Before we copy over the new builds I can "clear" out the directories by doing the following:

<delete>
  <fileset dir="${webplatformBin}" includes="**/*"/>
</delete>

这将每个目录(带有".svn")保留为空目录,然后复制新文件.复制它们后,我不确定如何清除剩下的空目录(如果我们已经完全移动了顶层数据目录所在的位置,等等).

This leaves every directory (with the ".svn") as an empty directory and then I copy over the new files. After they're copied I'm not sure how I can clear out the empty directories that are left (if we've completely moved where the top-level data directory is etc.).

例如,如果我有一个/projectA/data/localization/text.xml文件,然后将其移至/projectB/data/localization/text.xml,则最终会得到一个空文件夹/p​​rojectA/data/localization/(只能包含.svn文件夹).

For example if I had a /projectA/data/localization/text.xml file and I moved it to /projectB/data/localization/text.xml, I would end up with an empty folder /projectA/data/localization/ (that would only contain a .svn folder).

推荐答案

这是我能想到的最好的答案:

Here's the best answer I've been able to come up with:

<delete includeemptydirs="true">
  <fileset dir="${dirToStartFrom}"  >
    <and>
      <size value="0"/>
      <type type="dir"/>
     </and>
  </fileset>
</delete>

然后我将其包装在宏中,以便可以从任何目标传递目录名称:

I then wrapped it in a macro so I can pass the dir name in from any target:

<!-- Find and delete empty folders under dir -->
<macrodef name="deleteEmptyFolders">
    <attribute name="dir"/>
    <sequential>
        <delete includeemptydirs="true">
            <fileset dir="@{dir}"  >
                <and>
                    <size value="0"/>
                    <type type="dir"/>
                </and>
            </fileset>
        </delete>
    </sequential>
</macrodef>

像这样:

<target name="clean">
  <deleteEmptyFolders dir="build"/>
  <deleteEmptyFolders dir="common"/>
  <deleteEmptyFolders dir="lib"/>
</target>

这篇关于ANT如何仅递归删除空目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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