Foreach

Description

Iterates over a set of parameters to call another target within the same build-file.

The set of parameters are joined to form a cartesian product, and the specified target is called once for each element in the resulting set.

Parameters

Attribute Description Required
target The target to execute. Yes
type One of file, dir or both. If set to file, only the names of plain files will be sent to the command. If set to dir, only the names of directories are considered. No, if omitted default is file

Parameters specified as nested elements

param

Specifies the properties to set before running the specified target.

Each <param> forms a set of parameters with a single property name

The elements in the parameter set can be taken from either a fileset or a list of <item> elements. If the elements are taken from a fileset, the type attribute of <foreach> determines whether files, directories, or both files and directories from the fileset are included.

Examples

  <target name="default">
    <foreach target="compress">
      <param name="program">
        <item value="compress"/>
        <item value="gzip"/>
      </param>
      <param name="file">
        <fileset dir="." includes="**/*.tar"/>
      </param>
    </foreach>
  </target>

  <target name="compress">
    <exec executable="${program}">
      <arg file="${file}"/>
    </exec>
  </target>

Will pass each "tar" file through both "compress" and "gzip"

  <target name="default">
    <foreach target="zip-dirs" type="dir">
      <param name="src-dir">
        <fileset dir="." includes="*"/>
      </param>
    </foreach>
  </target>

  <target name="zip-dirs">
    <zip zipfile="${src-dir}.zip" >
      <zipfileset dir="${src-dir}" prefix="${src-dir}"/>
    </exec>
  </target>

Will create one zip archive in the current directory for every directory within the current directory. Files within the current directory will be ignored.


Copyright © 2000,2001 Apache Software Foundation. All rights Reserved.