使用 presetdef 和 antcall 时异常的 ant 行为
考虑以下来自 ant build.xml 的摘录:
<presetdef name="echo1def">
<echo message="prop: ${foo}" />
</presetdef>
<presetdef name="echo2def">
<sequential>
<echo message="prop: ${foo}" />
</sequential>
</presetdef>
<target name="echotarget1">
<property name="foo" value="bar" />
<echo1def/>
</target>
<target name="echotarget2">
<property name="foo" value="bar" />
<echo2def/>
</target>
<target name="echo1">
<antcall target="echotarget1" />
</target>
<target name="echo2">
<antcall target="echotarget2" />
</target>
调用任何 {echotarget1, echotarget2, echo1} 都会产生 prop: bar
的预期输出。然而,调用 echo2 会生成 prop: ${foo}
。
为什么 echo2def
无法解析 ${foo}
属性?它是在同一个项目中立即定义的(即,甚至不在 antcall 的另一侧)。 echo1 调用执行相同的操作,只是预设定义没有包含在
中,没有任何问题。
最后,
<target name="echo3">
<property name="foo" value="baz" />
<antcall target="echotarget2" />
</target>
报告 prop: baz
- 因此可以看到来自 antcalling 项目的属性,即使它是在预设定义之后定义的。
Consider the following excerpt from an ant build.xml:
<presetdef name="echo1def">
<echo message="prop: ${foo}" />
</presetdef>
<presetdef name="echo2def">
<sequential>
<echo message="prop: ${foo}" />
</sequential>
</presetdef>
<target name="echotarget1">
<property name="foo" value="bar" />
<echo1def/>
</target>
<target name="echotarget2">
<property name="foo" value="bar" />
<echo2def/>
</target>
<target name="echo1">
<antcall target="echotarget1" />
</target>
<target name="echo2">
<antcall target="echotarget2" />
</target>
Calling any of {echotarget1, echotarget2, echo1} produce the expected output of prop: bar
. Calling echo2, however, produces prop: ${foo}
.
Why can't the echo2def
resolve the ${foo}
property? It's defined immediately before, in the same project (i.e., not even on the other side of the antcall). The echo1 call, which does the same thing except the presetdef is not wrapped in <sequential>
, has no issue.
Finally,
<target name="echo3">
<property name="foo" value="baz" />
<antcall target="echotarget2" />
</target>
reports prop: baz
- so the property from the antcalling project can be seen, even though it is defined after the presetdef is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需摆脱 echo2def 预设定义中的
即可,意味着:一切都会按预期工作。
这是因为属性范围存在于 Apache Ant 的各种“块”级别(包括顺序级别),这就是 local task(Ant 1.8.0 中的新功能)有效。
antcall
打开一个新的项目范围,没有 antcall - 意味着直接调用这些目标 echotarget1 和 echotarget2 - 它也可以工作,解析属性 ${foo}。Just get rid of
<sequential>
in your echo2def presetdef, means :and all will work as expected.
It's because property scopes exist at Apache Ant's various "block" levels including sequential, that's how i.e. the local task (new in Ant 1.8.0) works.
antcall
opens a new project scope, without antcall - means calling those targets echotarget1 and echotarget2 directly - it works also, resolving the property ${foo}.