I\'m trying to use the foreach loop in an Ant script but I get the message: Problem: failed to create task or type foreach Cause: The name is undefined.
I don\'t und
I can't find the foreach task in the Ant 1.8 manual - where is it? I only know the task from ant-contrib, and it requires to specify the 'target' attribute: http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html
have you ever considered <script>
? in this tag you can use some famous script language like javascript and python. they can also interact with the Project, Task... Object of Ant, which means you can set/get the properties and even excute another task. look at this example which comes from the book "java development with ant"
<project name="script_example" default="test-random">
<description>
Use a script task to generate a random number, then
print it
</description>
<target name="random">
<script language="javascript"><![CDATA[
//NB: an unqualified Math is the JavaScript object
var r=java.lang.Math.random();
var num = Math.round(r*10);
project.setNewProperty("random", num);
self.log("Generated random number " + num, project.MSG_DEBUG);
]]>
</script>
</target>
<target name="test-random" depends="random">
<echo>Random number is ${random}</echo>
</target>
</project>
It is a standard task that would be part of the latest version of Ant (1.8).
No, it's not. At least I can't find it in the list of core and optional tasks in the ant manual. It seems to be part of the ant-contrib project and thus needs to be installed separately.
I had the same issue under eclipse with various versions of ant.
Add this into your code WITHOUT adding parameters under eclipse (do not specify any classpath) :
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="/path/to/ant-contrib/ant-contrib-1.0b3.jar"/>
You haven't defined a target to call:
<foreach param="instance" list="a,b,c,d,e" target="processListItem" />
alternatively:
<for param="instance" list="a,b,c,d,e" >
<sequential>
<!-- Do Something with @{instance} -->
</sequential>
</for>