2012-05-15 79 views
0

大家好,這是我的目標調用代碼。如何通過ant參考傳遞參數

<target name="abc"> 
    <var name="x" value="10"/> 
    <antcall target="def"/> 
    <!--Again Access The value of x here and also change it here--> 
</target> 

<target name="def"> 
    <!--Access The value of x here and also change it here--> 
</target> 

而且我想在其他構建文件訪問此X,有沒有什麼辦法

+0

什麼是'var'?我不承認這個名字的任何Ant任務。 – skaffman

+1

不可能,螞蟻不可能。 – oers

+0

它是螞蟻中的變量任務,用於在螞蟻中創建變量 – user1390517

回答

2

這是不可能的螞蟻。在一個屬性是不可變的,不能重置。 var task from ant contrib可用於覆蓋值,但應謹慎使用。

你可以使用臨時文件來實現你想要的。但是可能你正在嘗試一些奇怪的東西,這可以用不同的方式解決。
如果他們有權訪問屬性文件,這也可以在整個構建文件中起作用。

<target name="abc"> 
    <var name="x" value="10"/> 
    <antcall target="def"/> 
    <!--Again Access The value of x here and also change it here--> 
    <var unset="true" file="myproperty.properties" /> <!-- read variable from property file--> 
</target> 


<target name="def"> 
    <echo file="myproperty.properties" append="false">x=12</echo> <!-- create a new propertyfile--> 
</target> 
+1

'echoproperties'和'loadproperties' ant任務可用於存儲和加載具有臨時屬性值的文件。 'loadproperties'的'prefix'屬性有時有助於解決屬性不可變性的問題。 – Vadzim

+0

@Vadzim這個前綴的東西聽起來比我的var方法更hacky :)但echoproperties是一個很好的提示 – oers

1

對於正義起見,有一個hack允許改變螞蟻的不可變的屬性,而無需任何附加的庫(由於Java 6):

<scriptdef name="propertyreset" language="javascript" 
    description="Allows to assing @{property} new value"> 
    <attribute name="name"/> 
    <attribute name="value"/> 
     project.setProperty(attributes.get("name"), attributes.get("value")); 
</scriptdef> 

用法:

<target name="abc"> 
    <property name="x" value="10"/> 
    <antcall target="def"/> 
</target> 

<target name="def"> 
    <propertyreset name="x" value="11"/> 
</target> 

正如@oers所提到的,在所有規範方法證明不適合之後,應該謹慎使用。

如果不知道問題背後的目標,很難再提出建議。

+0

這將「生存」從antcall返回,即x = 11後的antcall?這是op問的。 – oers

+0

這只是一個相輔相成的部分答案。我猜想類似的腳本方法甚至可以用來訪問父項目的道具。這個問題不是很乾淨順便說一句。 – Vadzim

+0

用於將「分配」簡稱爲「分配」 –