2014-03-31 31 views
0

我如何可以調用基於命令行參數的目標?例如,我想有Apache Ant的 - 使用命令行參數爲目標

ant -Dbits=32 test呼叫<target name="test-32">

ant -Dbits=64 test呼叫<target name="test-64">

我嘗試這樣做:

ant -Dbits=32 test 

<target name="test-32"> 
... 
</target> 

<target name="test-64"> 
... 
</target> 

<target name="test" depends="test-${bits}"> 

但是,當我運行該腳本

我收到以下錯誤:

Target "test-${bits}" does not exist in the project

+0

爲什麼不'螞蟻test_32'和'螞蟻test_64'? – hmjd

+0

代碼重複。我想分解出通用的代碼。兩個目標之間的唯一區別是單個編譯器標誌。 –

+0

如果它只是一個不同的標誌,你可以在「$ {位}」中的測試目標本身的價值在使用'',而不是單獨的32倍64的目標設置好了。 –

回答

2

你可以嘗試像

<target name="test.type"> 
    <property name="run.test${bits}" value="yes"/> 
</target> 

<target name="test32" if="run.test32"> 
    ... 
</target> 

<target name="test64" if="run.test64"> 
    ... 
</target> 

<target name="test" depends="test.type, test32, test64"/> 

一招或者,如果你在評論表明,32位和64位的情況之間的差異僅僅是一個單個屬性值(一個編譯器標誌),那麼你可以設置該屬性與<condition>,例如

<target name="test"> 
    <!-- value="..." is the value to use if the condition is true, 
     else="..." is the value to use if the condition is false --> 
    <condition property="compiler.arch" value="x86_64" else="i386"> 
    <equals arg1="${bits}" args2="64" /> 
    </condition> 

    <!-- use ${compiler.arch} here --> 
</target> 
-1

嘗試以下操作:

<target name="test"> 
    <antcall target="test-${bits}"/> 
</target> 
+0

與antcall的問題是,它調用一個新的「工程」目標,因此受到所謂的目標製作的特性設置不會傳播給調用者。如果您需要從被調用任務中將事情複製回調用者,您需要使用類似'ant-contrib'中的'antcallback'任務。 –

+0

@IanRoberts謝謝你指出,我沒有意識到這一點。但根據主叫方和被叫任務的責任,這可能是也可能不是問題。例如 - 如果調用者僅用於選擇實際任務,並且所有工作都發生在被調用的任務內,則屬性不會傳播回去。因人而異。 –