请考虑从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的另一端)。除了presetdef没有包装在echo1中之外,echo1调用也没有问题。
最后,
<target name="echo3">
<property name="foo" value="baz" />
<antcall target="echotarget2" />
</target>reports prop: baz -因此可以看到来自反调用项目的属性,即使它是在预设值之后定义的。
发布于 2012-02-08 21:08:39
只需在您的<sequential>预设中去掉echo2def,就意味着:
<presetdef name="echo2def">
<echo message="prop: ${foo}" />
</presetdef>一切都会如愿以偿。
这是因为属性作用域存在于Apache的各种“块”级别,包括顺序级,这就是局部任务 (Ant1.8.0中的新版本)的工作方式。
antcall打开一个新的项目作用域,而不需要进行反调用--这意味着直接调用目标echotarget1和echotarget2 --它也能工作,解析属性${foo}。
https://stackoverflow.com/questions/9187425
复制相似问题