A rather dull worKLOG. This is just a scratchpad for solutions to IT problems that might be useful to someone else. Expect no opinions, no brilliant insights and definitely no pictures of pets or children. Expect stack traces, code snippets and other hints for the Google Indexer.

Wednesday, August 23, 2006

Copying a list of files in Ant

One might think that such an everyday task as copying a list of files would be trivial in Ant. Not so - it's a bloody nightmare.
  1. First attempt: creating a FileList would seem to be what you want. Nope - the Copy task doesn't support FileLists. Why?????
  2. Second attempt: create a FileSet (with the usual substitution of angle brackets....would it be too much to ask to have a blog site that doesn't stuff up xml?):


  3. [copy todir="foo"]
    [fileset dir="bar"]
    [filename name="pathfrombarto/file"/]
    [filename name="differentpathfrombarto/file2"/]
    [/fileset]
    [/copy]

    Nope - the filenames are mutually exclusive - the fileset does an "and" on them, and since your files won't match both names, it copies nothing. Ridiculous.
In any case, the fact that you're forced to use a FileSet and its pattern matching means that if you have a large set of files below "dir" (e.g. a Maven repository) then the above takes an age.

The solution, incidentally, is:

[copy todir="foo"]
[fileset dir="bar"]
[or]
[filename name="pathfrombarto/file"/]
[filename name="differentpathfrombarto/file2"/]
[/or]
[/fileset]
[/copy]

Though it still takes an age to process.