Java 如何使用 Ant 删除目录的目录集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/158665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:53:23  来源:igfitidea点击:

How do I delete a dirset of directories with Ant?

javaantbuild

提问by jamesh

I want to delete all directories and subdirectories under a root directory that are contain "tmp" in their names. This should include any .svn files too. My first guess is to use

我想删除根目录下所有名称中包含“tmp”的目录和子目录。这也应该包括任何 .svn 文件。我的第一个猜测是使用

<delete>
    <dirset dir="${root}">
          <include name="**/*tmp*" />
    </dirset>
</delete>

This does not seem to work as you can't nest a dirsetin a deletetag.

这似乎不起作用,因为您不能dirsetdelete标签中嵌套 a 。

Is this a correct approach, or should I be doing something else?

这是一种正确的方法,还是我应该做其他事情?

  • ant version == 1.6.5.
  • java version == 1.6.0_04
  • 蚂蚁版本 == 1.6.5。
  • Java 版本 == 1.6.0_04

采纳答案by jamesh

Here's the answer that worked for me:

这是对我有用的答案:

<delete includeemptydirs="true">
    <fileset dir="${root}" defaultexcludes="false">
       <include name="**/*tmp*/**" />
    </fileset>
</delete>

I had an added complication I needed to remove .svndirectories too. With defaultexcludes, .*files were being excluded, and so the empty directories weren't really empty, and so weren't getting removed.

我还有一个额外的复杂问题,我也需要删除.svn目录。使用defaultexcludes.*文件被排除在外,因此空目录并不是真正的空目录,因此不会被删除。

The attribute includeemptydirs(thanks, flicken, XL-Plüschhase) enables the trailing **wildcard to match the an empty string.

属性includeemptydirs(thanks, flicken, XL-Plüschhase) 使尾随**通配符能够匹配空字符串。

回答by Blauohr

try:

尝试:

<delete includeemptydirs="true">
    <fileset dir="${root}">
          <include name="**/*tmp*/*" />
    </fileset>
</delete>


ThankYou flicken !

谢谢你闪!

回答by XORshift

I just wanted to add that the part of the solution that worked for me was appending /**to the end of the include path. I tried the following to delete Eclipse .settings directories:

我只是想补充一点,对我有用的解决方案部分附加/**到包含路径的末尾。我尝试了以下删除 Eclipse .settings 目录:

<delete includeemptydirs="true">
    <fileset dir="${basedir}" includes"**/.settings">
</delete>

but it did not work until I changed it to the following:

但直到我将其更改为以下内容后,它才起作用:

<delete includeemptydirs="true">
    <fileset dir="${basedir}" includes"**/.settings/**">
</delete>

For some reason appending /**to the path deletes files in the matching directory, all files in all sub-directories, the sub-directories, and the matching directories. Appending /*only deletes files in the matching directory but will not delete the matching directory.

由于某种原因,追加/**到路径会删除匹配目录中的文件、所有子目录中的所有文件、子目录和匹配目录。追加/*只会删除匹配目录中的文件,但不会删除匹配目录。