Troubleshooting “failed to create task or type foreach” when using

前端 未结 5 1764
无人共我
无人共我 2020-12-17 15:48

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

相关标签:
5条回答
  • 2020-12-17 16:10

    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

    0 讨论(0)
  • 2020-12-17 16:21

    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>
    
    0 讨论(0)
  • 2020-12-17 16:27

    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.

    0 讨论(0)
  • 2020-12-17 16:31

    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"/>
    
    0 讨论(0)
  • 2020-12-17 16:35

    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>
    
    0 讨论(0)
提交回复
热议问题