2011-08-03 38 views
4

我有一個build.xml螞蟻使用,而我試圖把目標內的條件:如何在ant中使用condition元素來設置另一個屬性?

首先,我在這裏它設置屬性工程確定:

<condition property="isWindows"> 
    <os family="windows"/> 
</condition> 

然後我試着在目標中使用它:

<target name="-post-jar"> 
    <condition property="isWindows" value="true"> 
     <!-- set this property, only if isWindows set --> 
     <property name="launch4j.dir" location="launch4j" /> 
    </condition> 

    <!-- Continue doing things, regardless of property --> 
    <move file="${dist.jar.dir}" tofile="myFile"/> 
    <!-- etc --> 
</target> 

我收到一個錯誤:「條件不支持嵌套的」屬性「元素。 問題是:我如何正確地將條件放置在目標中,爲什麼錯誤是指「嵌套」屬性?

+0

這看起來酷似螞蟻文檔的語法。你確定你沒有在條件任務內創建一個元素(你寫過「在這裏做事情」)? –

+0

啊......我在那裏創建了另一個屬性(下一行是是否是否定的? – Pete855217

回答

3

condition用於定義屬性,但不用於根據屬性的值執行某些操作。使用target with if or unless attribute來執行一些基於屬性值的任務。

+0

我原本有目標+ an if ,但目標名稱是固定的(-post-jar),所以我無法複製它,因此試圖將一些條件放入目標本身謝謝JB。 – Pete855217

+0

只需讓你的-post-jar目標取決於另一個目標一個if屬性 –

+0

謝謝JB Nizet。通過重新安排任務解決,爲一個新目標添加,然後在這個新目標上添加一個if =「isWindows」。我相信嵌套錯誤提到了一個標記正確在我的原始代碼中的 Pete855217

0

condition的標準嵌套在condition元素的內部。

指定要使用property屬性設置的屬性以及使用condition元素上的value屬性滿足條件時的值。此外,您可以爲該屬性設置一個值,該條件不符合else屬性。

要檢查屬性是否被設置爲標準的condition,使用isset

<condition property="isWindows"> 
    <os family="windows"/> 
</condition> 

<target name="-post-jar"> 
    <!--Only set property if isWindows --> 
    <condition property="launch4j.dir" value="launch4j"> 
     <isset property="isWindows"/> 
    </condition> 

    <!-- Continue doing things, regardless of property --> 
    <move file="${dist.jar.dir}" tofile="myFile"/> 
    <!-- etc --> 
</target> 
相關問題