aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Ant - copy dir, exclude subdir Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Ant - copy dir, exclude subdir" Watch "Ant - copy dir, exclude subdir" New topic
Author

Ant - copy dir, exclude subdir

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
In a nutshell, using Ant, I can't seem to figure out copying everything in a directory to another directory, while excluding a certain subdirectory in the source, or originating, directory.
In other words, if I have the following directory structure

I'd like to copy everything inside the origin directory to a destination directory, excluding subdirectory1, such that the contents of the destination directory would look like

Note that the origin directory might dynamically contain many other subdirectories and files that I'd also want copied. The subdirectory that I don't want copied is always known.
I can only seem to figure out copying everything over. I realize I could just copy everything and then delete what's not wanted, but that seems silly.
I figured I'd surely be able to figure out specifying a fileset to exclude certain subdirectories, but I can't. I was hoping that the following fileset would work, but it doesn't - it just includes everything, including the excluded subdirectory.
Any ideas?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Aha!
exclude name="**/subdirectory1/**" seems to work.
[ April 19, 2004: Message edited by: Dirk Schreckmann ]
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
Originally posted by Dirk Schreckmann:
Aha!
exclude name="**/subdirectory1/**" seems to work.
[ April 19, 2004: Message edited by: Dirk Schreckmann ]

I believe you can omit the last 2 asterisks:
exclude name="**/subdirectory1/" should also work.


Ever Existing, Ever Conscious, Ever-new Bliss
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
I thought the "**" part of paths meant that it would match any depth of directory.
So, <exclude name="**/directory/*"/> would actually exclude all directories "named directory" in ANY subdirectory of the outer fileset dir attribute's value.
So, <exclude name="directory/**/*"/> would exclude all files and subdirectories of only one directory (directory). I'm not sure about that leading slash though. I don't think it should be there. Also, I'm not sure if the extra "/*" is needed here.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Now for my correction:

You need to:
  • Leave out the beginning ** and / from your include and exclude
  • Leave the training ** but no /* on your exclude

  • If you have <exclude name=something/**/*"/> it will include the directory "something" though it will be empty. With just <exclude name="something/**"/> it will not be there at all. You definitely don't want the beginning ** (my other post explains).
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    You are right, of course. Thank you for the correction.
    Now, for what I'd actually like to be doing.
    I'd prefer to exclude a directory from the fileset, where I know the absolute directory location, but I don't necessarily know just the name of the directory. As previously described, this directory is a subdirectory of the origin directory.
    Currently, I use a properties file to specify both the absolute directory location, which is used in one part of my Ant script, and to specify the relative location, which is used only to be certain to exclude it from the fileset I'm trying to build as explained from the beginning of this thread.
    I'd really prefer to just specify the absolute location in the properties file.
    Any ideas?
    Nathaniel Stoddard
    Ranch Hand

    Joined: May 29, 2003
    Posts: 1258
    Dirk,
    I may be misunderstanding part/all of what you said. But why not just do:

    If you're asking if you can combine the full absolute path into one <exclude> then I don't think you can do that, as the fileset requires a "dir" attribute, with the nested "exclude"s that list subdirectories/files to exclude and so forth. So, it really does require two properties.
    Did any of this help? Or am I way off?
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    I'd like to exclude a known absolute directory from a fileset, in order to reduce the number of properties I'm carrying around.
    It appears that I can't do this with an exclude in a fileset.
    Thanks for the confirmation, Nathaniel.
     
    jQuery in Action, 2nd edition
     
    subject: Ant - copy dir, exclude subdir