| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Ant-contrib Tasks: For</title> |
| 5 | </head> |
| 6 | |
| 7 | <body> |
| 8 | <h1>For</h1> |
| 9 | |
| 10 | <p>The for task iterates over a list, a list of paths, or |
| 11 | any type that has a public iterator() method. |
| 12 | The list will be evaluated first. |
| 13 | Nested paths are evaluated in the order they |
| 14 | appear in the task. |
| 15 | Nested types will then be evalulated. |
| 16 | </p> |
| 17 | <p> |
| 18 | This task is the same as the <foreach> task, except that |
| 19 | <ul> |
| 20 | <li>it uses a nested sequential for each iteration; and |
| 21 | <li>it implements an additional "keepgoing" attribute. |
| 22 | </ul> |
| 23 | <for> makes use of ant's macrodef task, so the @{} notation |
| 24 | is used for parameter substition. |
| 25 | </p> |
| 26 | <p><em> |
| 27 | This task only works for ant version greater than or equal |
| 28 | to ant 1.6.0. |
| 29 | </em> |
| 30 | </p> |
| 31 | |
| 32 | <h2>Parameters</h2> |
| 33 | <table border="1" cellpadding="2" cellspacing="0"> |
| 34 | <tr> |
| 35 | <th>Attribute</th> |
| 36 | <th>Description</th> |
| 37 | <th>Required</th> |
| 38 | </tr> |
| 39 | <tr> |
| 40 | <td valign="top">list</td> |
| 41 | <td valign="top">The list of values to process, with the |
| 42 | delimiter character, indicated by the "delimiter" |
| 43 | attribute, separating each value.</td> |
| 44 | <td align="center" valign="top" rowspan="2">Yes, one of these need to |
| 45 | be set, unless a nested path |
| 46 | has been specified.</td> |
| 47 | </tr> |
| 48 | <tr> |
| 49 | <td valign="top">end</td> |
| 50 | <td valign="top"> |
| 51 | Sets the ending index value. If this attribute is |
| 52 | set, the <for> task will loop from "start" (default 1) |
| 53 | to "end", using "step" (default 1) increments. |
| 54 | </td> |
| 55 | </tr> |
| 56 | <tr> |
| 57 | <td valign="top">param</td> |
| 58 | <td valign="top">Name of the parameter to pass the tokens or |
| 59 | files in as to the sequential.</td> |
| 60 | <td align="center" valign="top">Yes</td> |
| 61 | </tr> |
| 62 | <tr> |
| 63 | <td valign="top">delimiter</td> |
| 64 | <td valign="top">The delimiter characters that separates the |
| 65 | values in the "list" attribute. Each character in the |
| 66 | supplied string can act as a delimiter. This follows the semantics |
| 67 | of the StringTokenizer class.</td> |
| 68 | <td align="center" valign="top">No, defaults to ",".</td> |
| 69 | </tr> |
| 70 | <tr> |
| 71 | <td valign="top">parallel</td> |
| 72 | <td valign="top">If <code>true</code>, all iterations of the nested |
| 73 | <sequential> |
| 74 | will execute in parallel. Defaults to <code>false</code>, |
| 75 | which forces sequential execution of the iterations. It is up to |
| 76 | the caller to ensure that parallel execution is safe. |
| 77 | <td align="center" valign="top">No</td> |
| 78 | </tr> |
| 79 | <tr> |
| 80 | <td valign="top">keepgoing</td> |
| 81 | <td valign="top">If <code>true</code>, all iterations of the called |
| 82 | <sequential> will be executed, even if a task in one or more of them fails. |
| 83 | Defaults |
| 84 | to <code>false</code>, which forces execution to stop as soon as a |
| 85 | task fails. At the end, if any iterator has failed, the <for> |
| 86 | task will fail, otherwise <for> will succeed. |
| 87 | <p> |
| 88 | Note that execution does not proceed keepgoing from one task |
| 89 | to the next within the <sequential>, but rather from one iteration to the |
| 90 | next. |
| 91 | </p> |
| 92 | <p>It is up to the caller to ensure that keepgoing execution is safe.</p> |
| 93 | </td> |
| 94 | <td align="center" valign="top">No</td> |
| 95 | </tr> |
| 96 | <tr> |
| 97 | <td valign="top">threadCount</td> |
| 98 | <td valign="top">The maximum number of allowable threads when executing |
| 99 | in parallel. |
| 100 | <td align="center" valign="top">No. Defaults to 5.</td> |
| 101 | </tr> |
| 102 | <tr> |
| 103 | <td valign="top">trim</td> |
| 104 | <td valign="top">If <code>true</code>, any leading or trailing |
| 105 | whitespace will be removed from the list item before it is passed |
| 106 | to the sequential. |
| 107 | </td> |
| 108 | <td align="center" valign="top">No. Defaults to false.</td> |
| 109 | </tr> |
| 110 | <tr> |
| 111 | <td valign="top">begin</td> |
| 112 | <td valign="top"> |
| 113 | Sets the starting index value. This in only used |
| 114 | if the "end" attribute is set. |
| 115 | </td> |
| 116 | <td align="center" valign="top">No. Defaults to "1".</td> |
| 117 | </tr> |
| 118 | <tr> |
| 119 | <td valign="top">step</td> |
| 120 | <td valign="top"> |
| 121 | Sets the index increment step. |
| 122 | This in only used if the "end" attribute is set. |
| 123 | </td> |
| 124 | <td align="center" valign="top">No. Defaults to "1".</td> |
| 125 | </tr> |
| 126 | </table> |
| 127 | |
| 128 | <h2>Parameters specified as nested elements</h2> |
| 129 | |
| 130 | <a name="path"><h3>path</h3></a> |
| 131 | |
| 132 | <p><a href="http://ant.apache.org/manual/using.html#path">Path</a>s |
| 133 | are used to select sets of files or directories to iterate over.</p> |
| 134 | |
| 135 | <p>Using a path allows you to determine the order by which files |
| 136 | are considered by using |
| 137 | <a href="http://ant.apache.org/manual/CoreTypes/filelist.html">filelist</a>s |
| 138 | or explicit <code>pathelements</code>. You also can specify |
| 139 | whether you want to iterate over files or directories by chosing |
| 140 | either filesets or |
| 141 | <a href="http://ant.apache.org/manual/CoreTypes/dirset.html">dirset</a>s.</p> |
| 142 | <a name="fileset"><h3>fileset</h3></a> |
| 143 | <p><a href="http://ant.apache.org/manual/CoreTypes/fileset.html">FileSet</a>s |
| 144 | are used to select sets of files to iterate over. |
| 145 | </p> |
| 146 | <a name="fileset"><h3>dirset</h3></a> |
| 147 | <p><a href="http://ant.apache.org/manual/CoreTypes/dirset.html">DirSet</a>s |
| 148 | are used to select sets of directories to iterate over. |
| 149 | </p> |
| 150 | |
| 151 | <a name="seqential"><h3>sequential</h3></a> |
| 152 | This is the list of tasks to be run for each iteration of |
| 153 | the list. |
| 154 | <h3>Example</h3> |
| 155 | <p> |
| 156 | To print out the first five letters of the latin alphabet: |
| 157 | </p> |
| 158 | <blockquote> |
| 159 | <pre> |
| 160 | <echo message="The first five letters of the alphabet are:"/> |
| 161 | <for list="a,b,c,d,e" param="letter"> |
| 162 | <sequential> |
| 163 | <echo>Letter @{letter}</echo> |
| 164 | </sequential> |
| 165 | </for> |
| 166 | </pre> |
| 167 | </blockquote> |
| 168 | <p> |
| 169 | A more complicated example to to iterate over a set |
| 170 | of c++ source files and invoke the <cc> task on them: |
| 171 | </p> |
| 172 | <blockquote> |
| 173 | <pre> |
| 174 | <for param="file"> |
| 175 | <path> |
| 176 | <fileset dir="${test.dir}/mains" includes="*.cpp"/> |
| 177 | </path> |
| 178 | <sequential> |
| 179 | <propertyregex override="yes" |
| 180 | property="program" input="@{file}" |
| 181 | regexp=".*/([^\.]*)\.cpp" replace="\1"/> |
| 182 | <mkdir dir="${obj.dir}/${program}"/> |
| 183 | <mkdir dir="${build.bin.dir}"/> |
| 184 | <cc link="executable" objdir="${obj.dir}/${program}" |
| 185 | outfile="${build.bin.dir}/${program}"> |
| 186 | <compiler refid="compiler.options"/> |
| 187 | <fileset file="@{file}"/> |
| 188 | <linker refid="linker-libs"/> |
| 189 | </cc> |
| 190 | </sequential> |
| 191 | </for> |
| 192 | </pre> |
| 193 | </blockquote> |
| 194 | The preceding example will stop as soon as one of the <cc> tasks fails. |
| 195 | If you change the first line of the example to |
| 196 | <pre> <for param="file" keepgoing="true"></pre> |
| 197 | All iterations will be executed, and then <for> will fail if any one |
| 198 | or more of the <cc> tasks failed. |
| 199 | <p> |
| 200 | The following example embeds an outofdate type and iterates over |
| 201 | the sources that are newer than their corresponding targets. |
| 202 | </p> |
| 203 | |
| 204 | <pre> |
| 205 | <ac:for param="xmlfile" xmlns:ac="antlib:net.sf.antcontrib"> |
| 206 | <ac:outofdate> |
| 207 | <ac:sourcefiles> |
| 208 | <ac:fileset dir="${basedir}/xdocs" includes="**/*.xml"/> |
| 209 | </ac:sourcefiles> |
| 210 | <ac:mapper dir="${basedir}/xdocs" |
| 211 | type="glob" from="*.xml" to="${basedir}/docs/*.html"/> |
| 212 | </ac:outofdate> |
| 213 | <ac:sequential> |
| 214 | <echo>Need to generate a target html file from source file @{xmlfile}</echo> |
| 215 | </ac:sequential> |
| 216 | </ac:for> |
| 217 | </pre> |
| 218 | |
| 219 | <p> |
| 220 | The following example loops from one to ten. |
| 221 | </p> |
| 222 | <pre> |
| 223 | <ac:for param="i" end="10"> |
| 224 | <sequential> |
| 225 | <echo>i is @{i}</echo> |
| 226 | </sequential> |
| 227 | </ac:for> |
| 228 | </pre> |
| 229 | <p> |
| 230 | The following example counts down from 10 to 0 in steps of -2. |
| 231 | </p> |
| 232 | <pre> |
| 233 | <ac:for param="i" begin="10" step="-2" end="0"> |
| 234 | <sequential> |
| 235 | <echo>i is @{i}</echo> |
| 236 | </sequential> |
| 237 | </ac:for> |
| 238 | </pre> |
| 239 | <hr> |
| 240 | <p align="center">Copyright © 2003-2006 Ant-Contrib Project. All |
| 241 | rights Reserved.</p> |
| 242 | |
| 243 | </body> |
| 244 | </html> |