2012-02-08 62 views
1

从蚂蚁的build.xml考虑以下摘录:异常蚂蚁行为和antcall

<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的调用不包含在<sequential>中,没有问题。

最后,

<target name="echo3"> 
    <property name="foo" value="baz" /> 
    <antcall target="echotarget2" /> 
</target> 

报告prop: baz - 所以从antcalling项目该属性可以可以看出,即使后presetdef是它被定义。

回答

-1

就在您的echo2def presetdef摆脱<sequential>,意思是:

<presetdef name="echo2def"> 
<echo message="prop: ${foo}" /> 
</presetdef> 

和预期都将正常工作。
这是因为属性范围存在于Apache Ant的各种“块”级别,包括顺序,这就是local task(Ant 1.8.0中的新增功能)的工作原理。

antcall打开一个新的项目范围,没有antcall - 意味着直接调用这些目标echotarget1和echotarget2 - 它也可以解析属性$ {foo}。

+0

对不起,我的意思是presetdef,对不起,会改正这一点。 – BeeOnRope 2012-02-08 22:17:08

+1

...但问题是 - presetdef可以使用或任何其他任务 - 我只是使用,因为它是最简单的,但您可能会遇到与或其他任何问题相同的问题。 – BeeOnRope 2012-02-08 22:18:04

+0

你是对的,它可能包含任何嵌套的任务,但看到我编辑的答案 – Rebse 2012-02-09 10:52:00