2

In an Ant target I get a property, containing the list of directories to be included in further action (copying, filtering, etc.). It looks like this:

directories=dir1, dir2, dir3

I need a way to convert this list to a fileset or patternset that selects all the files in these directories.

I know I can use a script to generate pattern strings and then use it in the "include" or "exclude", but is there are a way to avoid scripts?

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Max Kosyakov
  • 292
  • 2
  • 5

3 Answers3

2

Note that as of Ant 1.9.4, there is a new construct <multirootfileset> that provides that functionality, even if the dirs are not siblings:

<multirootfileset basedirs="${directories}" includes="**/*">
Patrice M.
  • 4,209
  • 2
  • 27
  • 36
1

How about using the antcontrib propertyregex task to convert the comma-separated list into wildcards suitable for a fileset?

<property name="directories" value="dir1, dir2, dir3" />

<property name="wildcard" value="${file.separator}**${file.separator}*" />
<propertyregex property="my_pattern"
               input="${directories}" 
               regexp=", " 
               replace="${wildcard}," />

At this point we now have:

my_pattern=dir1/**/*,dir2/**/*,dir3

That can be used with a further suffixed wildcard to get the full fileset:

<fileset dir="." id="my_fileset" includes="${my_pattern}${wildcard}" />

(The fiddly ${wildcard} is to ensure portability between unix and windows filesystems, you could use /**/* if you're pure unix.)

martin clayton
  • 76,436
  • 32
  • 213
  • 198
0

Something like this should work:

<dirset includes="${directories}"/>

Yes, dirset isn't fileset. However, it may be enough, or else you can probably use a for or foreach from ant-contrib to iterate over the directories in your target. You might also be able to define a ResourceCollection based around the dirset. It might help to know what the "further action" is expected to be.

However, this feels like too much work ...

Zac Thompson
  • 12,401
  • 45
  • 57
  • Thanks a lot for your answer, however, as you noticed this is too much work. Using script to convert list to pattern is more concise. I can switch to semicolon-delimited list, but why this should make any difference? – Max Kosyakov Aug 05 '10 at 03:29
  • Yah, I guess it doesn't make that much difference. It seems like it should be possible to do something with a resource collection, but I wasn't able to get it to work: http://ant.apache.org/manual/Types/resources.html#restrict – Zac Thompson Aug 05 '10 at 23:45