2011-09-30 46 views
6

如何檢查一個數字屬性小於Apache Ant?Apache Ant小於

<property name="small" value="15"/> 
<property name="big" value="156"/> 
<fail message="small is less than big!"> 
    <condition> 
    <lessthan val1="${small}" val2="${big}"/> 
    </condition> 
</fail> 

從我所看到的(我是新來的螞蟻)你只能做<equal/>

+0

類似http://stackoverflow.com/questions/4639902/apache-ant-comparing-properties-variables這可悲的是使得它看起來像你必須嵌入一​​些腳本。 – ccoakley

+0

謝謝ccoakley。可惜我把標題放在比標題更少,而不是比Stackoverflow搜索沒有選擇的更大:(選擇關閉重複。 –

回答

4

您可以使用<scriptcondition>(請參閱http://ant.apache.org/manual/Tasks/conditions.html)。

仔細閱讀文檔,因爲它需要在ant中安裝附加的jar依賴關係。

狀況可以看起來像(未測試):

<scriptcondition language="javascript"> 
    var small = parseInt(project.getProperty("small")); 
    var big = parseInt(project.getProperty("big")); 

    self.setValue(small < big); 
</scriptcondition> 
+0

我喜歡Ant-Contrib中的'isgreaterthan'任務的外觀(已經使用了),我不知道如何使用它。 –

+0

http://ant-contrib.sourceforge.net/tasks/tasks/more_conditions.html你試過嗎? – FailedDev

+1

@MattClarkson你可能試圖使用'在''任務中'isgreaterthan>'從#FailedDev提供的鏈接中:'不幸的是,他們不能用於任務。「 – Jesse

2

乾杯JB Nizet,終於到了那裏。

<!-- Test the Ant Version --> 
<property name="burning-boots-web-build.required-ant-version" value="1.8.2"/> 
<script language="javascript"> 
    <![CDATA[ 
     var current  = project.getProperty("ant.version").match(/([0-9](\.)?)+/)[0].replace(/\./g,""); 
     var required = project.getProperty("burning-boots-web-build.required-ant-version").match(/([0-9](\.)?)+/)[0].replace(/\./g,""); 
     project.setProperty('ant.valid-version', current < required ? "false" : "true"); 
    ]]> 
</script> 
<fail message="This build requires Ant version ${burning-boots-web-build.required-ant-version}."> 
    <condition> 
     <isfalse value="${ant.valid-version}"/> 
    </condition> 
</fail> 
0

性質 A「小於」 comparioson是不可能的,而不任一定製任務或嵌入式腳本。

但是在大多數情況下,您可以通過將測試應用到屬性而不是值的來源來逃脫。在構建系統中,這些「源」通常是文件。在文件上,您可以使用isfileselectedconditionselector。大多數選擇器接受when屬性,如lessmoreequal

isfileselected條件的手冊顯示了一個示例。

0

Ant插件Flaka提供了一個評估EL表達式f.e的失敗任務。 :

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka"> 
<property name="small" value="15"/> 
<property name="big" value="156"/> 
<fl:fail message="small is less than big!" test="small lt big"/> 
</project> 

輸出:

BUILD FAILED 
/home/rosebud/workspace/Ant/demo.xml:7: small is less than big! 

看到Flaka manual進一步的細節

2

這裏是<isgreaterthan>條件的任務與任何腳本使用:

<if> 
    <isgreaterthan arg1="100" arg2="10"/> 
    <then> 
     <echo>Number 100 is greater than number 10</echo> 
    </then> 
</if> 

此外,ARG1, arg2值可以是屬性變量。

注:<isgreaterthan>可與螞蟻的Contrib一個附加條件:

http://ant-contrib.sourceforge.net/tasks/tasks/more_conditions.html

+0

確定這是否正常?從文檔:」這些條件適用於元素。不幸的是,他們不能用於任務......「。當我嘗試這個時,構建失敗:」if不支持嵌套「isgreaterthan」元素「 –

0

事實上,由@GnanaPrakash提供的答案是不完整的。 從Ant Contrib文檔:

這些條件適合於在元件的使用。不幸的是,它們不能在<條件>任務中使用,雖然任務的所有條件也可以與<布爾>和<布爾>使用可以在任何地方使用的<條件>可以使用。

因此,islessthan或替代isgreaterthan元素必須被包裹在一個bool元素,像這樣:

<property name="small" value="15" /> 
<property name="big" value="156" /> 
<if> 
    <bool> 
     <islessthan arg1="${small}" arg2="${big}"/> 
    </bool> 
    <then> 
     <echo message="small is less than big!" /> 
    </then> 
    <else> 
     <echo message="big is less than small!" /> 
    </else> 
</if> 

如果你不這樣做,這樣你會得到一個錯誤說:

if doesn't support the nested "islessthan" element. 
+0

小記:appar目前這不適用於antcontrib-1.0b3:https://stackoverflow.com/a/12391161/2581587 –