Ant: How do I interate over all subfolders and perform a task in ant

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.

Answers


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.


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>

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.


Need Your Help

How do I read an attribute on a class at runtime?

c# generics custom-attributes

I am trying to create a generic method that will read an attribute on a class and return that value at runtime. How do would I do this?

Partition Lucene Index by ID across multiple indexes

lucene.net lucene

I am trying to put together my Lucene search solution, and I'm having trouble figuring out how to start.