ANT:如何读取 foreach 循环中设置的属性
亲爱的,我目前在检索 foreach 循环中设置的属性值时遇到一些问题。也许你们中的一个人可以帮助我...
目的是检查自生成相应的jar以来文件夹中的一个文件是否已被修改。这样我就知道是否需要再次生成 jar。 我所做的是使用 foreach 循环遍历文件夹,如果一个文件与我的测试匹配,则将属性设置为 true。
问题是我的变量在循环后似乎不存在...这是一个具有相同问题的简化代码示例:
<target name="target">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${lib.dir}/ant-contrib.jar"></taskdef>
<foreach target="setVar" param="var" list="a,b"/>
<echo>myreturn in target: ${env.myreturn}</echo>
<property name="env.myreturn" value="c"/>
<echo>myreturn in second: ${env.myreturn}</echo>
</target>
<target name="setVar">
<property name="env.myreturn" value="${var}"/>
<echo>myreturn in setVar: ${env.myreturn}</echo>
</target>
此代码的结果是:
target:
setVar:
[echo] myreturn in setVar: a
setVar:
[echo] myreturn in setVar: b
[echo] myreturn in target: ${env.myreturn}
[echo] myreturn in second: c
BUILD SUCCESSFUL
看来该变量已正确设置,因为它可以被打印在“setVar”目标中,但无法从调用目标检索值。
我还知道不可能为一个属性分配两次值。但问题甚至不会发生...当出现这种情况时,我可以在分配属性之前添加对属性值的检查,以确保它尚未初始化...
您有线索吗我可以解决我的问题吗???
非常感谢您的帮助:)
Dear, I currently face some problem to retrieve the value of a property setted in a foreach loop. Maybe one of you could help me...
The purpose is to check if one file of a folder has been modified since the corresponding jar has been generated. This way I know if I have to generate the jar again.
What I do is to go through the folder with a foreach loop and if one file match my test, set a property to true.
The problem is that my variable doesn't seems to exist after my loop... Here is a simplified code example that has the same problem:
<target name="target">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${lib.dir}/ant-contrib.jar"></taskdef>
<foreach target="setVar" param="var" list="a,b"/>
<echo>myreturn in target: ${env.myreturn}</echo>
<property name="env.myreturn" value="c"/>
<echo>myreturn in second: ${env.myreturn}</echo>
</target>
<target name="setVar">
<property name="env.myreturn" value="${var}"/>
<echo>myreturn in setVar: ${env.myreturn}</echo>
</target>
The result of this code is:
target:
setVar:
[echo] myreturn in setVar: a
setVar:
[echo] myreturn in setVar: b
[echo] myreturn in target: ${env.myreturn}
[echo] myreturn in second: c
BUILD SUCCESSFUL
It seems that the variable is correctly set as it could be printed in the "setVar" target but no way to retrieve value from the calling target.
I also know it's not possible to assign a value to a property twice. But the problem doesn't even occurs... When it'll be the case I could add a check on the value of the property before to assign it to be sure it is not already initialized...
Do you have a clue on the way I can solve my problem ???
Many thanks in advance for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用 ant-contrib 中的
任务代替
。
任务利用了后来出现的 Ant 宏功能。与旧的
任务相比,它的工作速度更快且更灵活。使用
时,您处于同一项目上下文中。这意味着循环中设置的属性将在循环外部可见。当然,属性的正常规则适用...您只能设置一次...除非您使用 ant-contrib 的任务来覆盖或取消设置先前设置的属性。
啊,蚂蚁黑客的乐趣。
Try
<for>
task from ant-contrib instead of<foreach>
. The<for>
task takes advantage of Ant macro facility that came later. It works faster and is more flexible than the older<foreach>
task. You are in the same project context when using<for>
. That means properties set in the loop will be visible outside of the loop. Of course, normal rules for properties apply... you only get to set it once... unless you use<var>
task from ant-contrib to overwrite or unset previously set properties.Ah the joys of Ant hacking.
不确定您的 foreach 问题,但是您不能使用 uptodate 任务吗要求?
Not sure about your foreach problem, but can you not use the uptodate task for your requirement?
即使我不再需要它,多亏了 sudocode,我也找到了我的问题的解决方案。也许对其他人有用......
一位同事谈到了 ant-contrib 的“antcallback”目标:它允许将结果从被调用的目标返回到调用的目标。通过“for”目标和“antcallback”的组合,可以做我想做的事情:
“file”包含目录中文件的名称。它作为参数提供给被调用的目标,值为“@{file}”(由于“for”目标实现,“@”是必需的)。
在主目标的末尾,${retValue} 包含由“setVar”目标设置的第一个值。尝试多次设置它时不会引发错误,因此无需检查变量是否已在“setVar”目标中设置之前实例化。
Even if I don't need it anymore thanks to sudocode, I found a solution for my question. Maybe it could be useful for someone else...
A collegue talked about the "antcallback" target of ant-contrib: it allows to return a result from a called target to the calling one. With a combination of "for" target and "antcallback" it is possible to do what I wanted to do:
"file" contains the name of the file in the directory. It is given to the called target as parameter with value "@{file}" ('@' necessary due to "for" target implementation).
At the end of the main target, ${retValue} contains the first value setted by the "setVar" target. No error is thrown when trying to set it multiple times, so it's not necessary to check if variable has already been instantiated before to set it in "setVar" target.
任务在底层使用与
相同的逻辑,以及由换句话说,您在
setVar
目标中定义的env.myreturn
属性在该目标执行完成后就会丢失。这种脚本编写确实不是 Ant 的设计目的。 Ant-contrib 库试图修补这些漏洞,但它仍然使它变形。
如果您需要编写此类脚本,并希望使用 Ant 任务来实现它们,请查看 Gradle。它是 Groovy(用于脚本编写)和 Ant(用于任务)的完美结合。
The
<foreach>
task uses the same logic as<antcall>
under the covers, and any proprrties set inside a target invoked by<antcall>
do not have scope beyond the execution of that target.In other words, the
env.myreturn
property that you define in thesetVar
target is lost as soon as execution of that target completes.This sort of scripting really isn't what Ant is designed for. The Ant-contrib library tries to patch up the holes, but it's still bending it way out of shape.
If you need to write such scripts, and want to use Ant tasks to achieve them, have a look at Gradle instead. It's a rather lovely blend of Groovy (for scripting) and Ant (for the tasks).
这里的其他方法(
、、
properties.put(....)
、
、
)不适用于 ANT 1.9.4,因此我使用了与此类似的文件系统(伪代码) :也许其他方法因为我的
而不起作用,但我在这里需要它。 (outerprop
最初未设置)The other approaches here (
<for>
,<var>
,<groovy>properties.put(....)</groovy>
,<property>
,<antcallback>
) did not work with ANT 1.9.4, so I used the file system similar to this (pseudocode):Maybe the other approaches did not work because of my
<antcall>
, but I need it here. (outerprop
is initially unset)