2012-02-08 63 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