7

Currently I do

<foreach list="${myfolders}" target="bundle"
     param="worksheet" inheritall="true"/>

to execute the target "bundle" on a list of folders. However the problem is I need to set this list. How do I use Ant to just loop through all the folders given the parent directory?

If there is a way to do this and also exclude specific folders that would be even better. Thanks.

David W.
  • 105,218
  • 39
  • 216
  • 337
Solomon
  • 71
  • 1
  • 1
  • 3

3 Answers3

10

You can provide a <dirset> for the <foreach> task to operate on:

 <foreach target="bundle" param="worksheet" inheritall="true">
      <path>
          <dirset dir="${subDir}">
               <include name="*"/>
          </dirset>
      </path>
 </foreach>

Notice that the list parameter isn't used when I do it this way.

You can't use <dirset> directly under the <foreach> as you can with <fileset>. However, you can put the <dirset> under the <path> as shown above. The <include name="*"/> prevents recursing down the directory tree.

Mark
  • 7,507
  • 12
  • 52
  • 88
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Thank you. When I try it I get an error "The `` type doesn't support the nested 'path' element". If I take out `` then I get the error "The `` type doesn't support the nested 'dirset' element" – Solomon Jun 06 '11 at 17:54
  • What version of the antcontrib jar are you using? Try my [build.xml](http://dl.dropbox.com/u/433257/build.xml). That's what I used for testing. – David W. Jun 06 '11 at 21:29
0

You can do this with subant

Example:

<?xml version="1.0" encoding="UTF-8"?>
<project name="subant" default="subant1">

      <target name="subant1">

            <subant target="">

                 <dirset dir="." includes="*"/>
                 <target name="release" />

             </subant>

        </target>

</project>
Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69
-1

I use the foreach task from ant-contrib for a similar job. This will call a specified target for each entry in a list, passing the entry as a parameter each time.

Ben
  • 2,348
  • 2
  • 19
  • 28
  • I am looking for a way to do this without providing a list. The set of subfolders constantly changes in my case, and I don't want to always have to update the list accordingly. – Solomon Jun 06 '11 at 16:05
  • Looks like David W has already expanded on this. The "list" can actually be something that is generated automatically. – Ben Jun 07 '11 at 10:43