Java 使用 <foreach> 时“无法创建任务或键入 foreach”疑难解答
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2608935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Troubleshooting "failed to create task or type foreach" when using <foreach>
提问by user305801
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.
我正在尝试在 Ant 脚本中使用 foreach 循环,但收到消息:问题:无法创建任务或键入 foreach 原因:名称未定义。
I don't understand why this doesn't work. It is not a 3rd party library. It is a standard task that would be part of the latest version of Ant (1.8).
我不明白为什么这不起作用。它不是第 3 方库。这是一个标准任务,将成为 Ant (1.8) 最新版本的一部分。
<target name="parse">
<echo message="The first five letters of the alphabet are:"/>
<foreach param="instance" list="a,b,c,d,e">
</foreach>
</target>
采纳答案by Michael Borgwardt
It is a standard task that would be part of the latest version of Ant (1.8).
这是一个标准任务,将成为 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 projectand thus needs to be installed separately.
不,这不对。至少我在ant手册中的核心和可选任务列表中找不到它。它似乎是ant-contrib 项目的一部分,因此需要单独安装。
回答by Chris Lercher
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
我在 Ant 1.8 手册中找不到 foreach 任务 - 它在哪里?我只知道 ant-contrib 的任务,它需要指定 'target' 属性:http: //ant-contrib.sourceforge.net/tasks/tasks/foreach.html
回答by julia2020
I had the same issue under eclipse with various versions of ant.
我在 eclipse 下使用各种版本的 ant 遇到了同样的问题。
Add this into your code WITHOUT adding parameters under eclipse (do not specify any classpath) :
将此添加到您的代码中,而无需在 eclipse 下添加参数(不要指定任何类路径):
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="/path/to/ant-contrib/ant-contrib-1.0b3.jar"/>
回答by cn1h
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"
你有没有考虑过<script>
?在这个标签中,你可以使用一些著名的脚本语言,比如 javascript 和 python。它们还可以与 Ant 的 Project、Task... Object 进行交互,这意味着您可以设置/获取属性,甚至可以执行另一个任务。看看这个例子,它来自“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>
回答by Chris.Holman
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>