简体   繁体   中英

Ant delete first works, then doesn't

I'm working on a big Ant script (> 1000 lines), which I inherited from someone else (no longer available). I'm having problems with Ant not deleting directories. At one point in the script it works, at another, it doesn't: Ant does not give an error message, but it also leaves the directories where they are.

I put some groovy around it, to check for existence of directories:

  <groovy>
    println("Directories in @{outFolder} are:")
    new File("@{outFolder}").eachDir() { dir -> println dir.getName() }
  </groovy>
  <delete verbose="true" includeEmptyDirs="true" >
    <dirset dir="@{outFolder}" includes="**/*" />
  </delete>
  <echo>End of delete</echo>
  <groovy>
    println("Directories in @{outFolder} are:")
    new File("@{outFolder}").eachDir() { dir -> println dir.getName() }
  </groovy>

So, the lines are invoked and they work. Next, some other app is called, which creates new directories. The exact same lines are invoked again (copy and paste, and yes, they are equal), but now the directories aren't deleted. Also: no error, Ant keeps running. I use Ant 1.8.1 on Windows Server 2008 R2. I have tried adding a sleep, to prevent lock problems. The app that creates the directories is a Java app (Tibco appManage). The directories contain XML files, no jar files. I checked ANT_HOME and CLASSPATH: no problems there. What do I miss?

For the smart asses: of course the second delete should not delete everything that was created by the app, but to nail down the problem I made the script lines as simple as possible.

Some more information: as I can't get the delete working, I tried to use Ant move, as a work-around. This is what I see: the move creates empty directories at the destination, does not remove any files or directories from the source, and does not report an error. It seems like there's something wrong with those source directories/files (that I tried to delete before).

Another attempt: downloaded latest versions of Ant and Groovy. Same results.

You're using a dirset. Use a fileset.

Explanation: A dirset is not what you think it is. You almost always want to use fileset.

The first time, it is working because the directories are empty. The second time, they are not. I can tell this because a <dirset> includes the directory objects themselves, but none of the files inside them, and you are using "includeEmptyDirs" in your delete task, which makes no sense with a dirset.

In a hypothetical tree like this:

top/
  sub1/
    file.txt
  sub2/ (empty)

... a dirset collection would select top/, top/sub1/, and top/sub2/, but not top/sub1/file.txt. The <delete> acts on the collection, and will not delete non-empty directories. So in the above case, it would delete top/sub2 (which is empty) but not top/sub1 or top/. This should also explain for you the results of your <move> attempt, which gives you some idea of cases for which a dirset can actually be useful.

A fileset includes files and directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM