• warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 2 to gmap_gmap() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.
  • warning: Parameter 1 to tac_lite_node_grants() expected to be a reference, value given in /home/rbezemer/softwarebyrichard.com/includes/module.inc on line 483.

Iterating through a directory with Ant

Ant is a great build system, but often it can be very confusing trying to wade through all it's documentation and find out how to do something you could write in a batch file in minutes. Case in point, iterating through a directory.

The easiest way to do this is to use the ant-contrib tasks. These add some very usefull functionality to Ant that really improves your productivity. You can find full details of all the packages here. How to use foreach. It looks really complicated but it's not that bad.

  1. <target name="parse_directory" description="Parses out all the files from the directory">
  2. <foreach target="build_my_file" param="the_file">
  3. <path>
  4. <fileset dir="path/to/buildfiles">
  5. <include name="**/*.mxml"/>
  6. </fileset>
  7. </path>
  8. </foreach>
  9. </target>
  10. <target name="build_my_file" description="Builds a mxml file">
  11. <mxmlc file="${the_file}"/>
  12. </target>

So to break down what it's doing, in the first target (parse_directory) it is passing a fileset tag to the foreach task to iterate over, it doesn't have to be a fileset, you can also iterate over directories as long as you wrap it in a path tag. the foreach task takes two important attributes. target specifies the ant target that should be called for each item in the fileset and param specifies the name of the variable that will contain the variable for the iteration. In the build_my_file target you can see where we reference this variable with the ${the_file} statement.

Hope that helps anyone to make Ant a little more usefull.

Your rating: None Average: 3.5 (30 votes)