简体   繁体   中英

Ant: Two questions about dirsets

I have two questions about the dirset type in Apache Ant.

  • Is a dirset really a set, with no order guaranteed, or does it preserves the input order? I want to use ant:contrib to iterate over a set of directories and order matters, so if Ant's dirset does not preserve insertion-order, what alternatives do I have?
  • How can I test if a certain dir is included within a dirset?

[Edit]

If you look at the dirset source It looks like it uses java File.list() , whose documentation states that there is no gauranteed order. So no you can't count on that absolutely. However, before returning it calls Arrays.sort(files); See Line 1572.


As per preserving order I couldn't say, I would hazard that there is no guarantee but that it usually just happens to preserve the file systems order.

As to testing, I presume you want, do action if this file exists or something similar, using ant contrib,

<for param="directory">
<dirset dir="dirIneedtoexist">
</dirset>
<sequential>
     <!-- Stuff to do if it exists. -->          
</sequential>
</for>

If there is nothing in the dirset if won't do anything.

According to this page

dirset Adds a directory set to the implicit build path. Note that the directories will be added to the build path in no particular order, so if order is significant, one should use a file list instead!

Here's link on how to use FileList

There's not much in the Ant documentation about dirset .

If you use patternset , there's no guarantee of order.

If you use include , listing individual directories rather than patterns, dirset should preserve the order. I'd test this though, to be sure.

Edited to add: In the event that I'm wrong, you can write your own Ant custom task to preserve the order of the include directories.

No it does not preserve order.

A dirset's contents are based on what its parent class exposes via a DirectoryScanner . That scanner walks the file system and does not do so in any particular order.

It also uses File.list which is a source of order non-determinism:

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

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