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

查看:30
本文介绍了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 和数据,当我们移动该数据的位置时,我们的复制"和签入构建过程不会t 真的有效,而且因为我们正在检查另一个源代码管理 (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,我最终会得到一个空文件夹/projectA/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天全站免登陆