Ant if和unless都是<target>元素(tasks)的屬性。 這些屬性用於控制任務是否運行的任務。
除了target之外,它還可以與<junit>元素一起使用。
在早期版本和Ant 1.7.1中,這些屬性僅是屬性名稱。 如果定義了屬性,則即使值為false也會運行。
例如,即使在傳遞false之後也無法停止執行。
檔:build.xml -
<project name="java-ant project" default="run">
<target name="compile">
    <available property="file.exists" file="some-file"/>
    <echo>File is compiled</echo>
</target>
<target name="run" depends="compile" if="file.exists">
    <echo>File is executed</echo>
</target>
</project>
輸出:
無參數:沒有命令行參數運行它。 只需輸入ant到終端,但首先找到專案位置,它將顯示空輸出。

使用參數:現在只傳遞參數:false。
Ant -Dfile.exists = false
得到以下結果:
E:\worksp\ant\AntProject>Ant -Dfile.exists = false
Buildfile: E:\worksp\ant\AntProject\build.xml
compile:
     [echo] File is compiled
run:
     [echo] File is executed
BUILD SUCCESSFUL
Total time: 0 seconds
使用參數:現在只傳遞參數:true。
Ant -Dfile.exists = true
執行上面示例代碼,得到以下結果:
E:\worksp\ant\AntProject>Ant -Dfile.exists = true
Buildfile: E:\worksp\ant\AntProject\build.xml
compile:
     [echo] File is compiled
run:
     [echo] File is executed
BUILD SUCCESSFUL
Total time: 0 seconds
從Ant 1.8.0開始,可以使用屬性擴展,只有當value為true時才允許執行。在新版本中,它提供了更大的靈活性,現在可以從命令行覆蓋條件值。請參閱下麵的示例。
檔:build.xml
<project name="java-ant project" default="run">
<target name="compile" unless="file.exists">
    <available property="file.exists" file="some-file"/>
</target>
<target name="run" depends="compile" if="${file.exists}">
    <echo>File is executed</echo>
</target>
</project>
輸出
無參數:在沒有命令行參數的情況下運行它。 只需輸入ant到終端,但首先找到專案的位置,它將顯示空輸出。
使用參數:現在傳遞參數,但只使用false。
Ant -Dfile.exists = false
沒有輸出,因為這次沒有執行。
使用參數:現在傳遞參數,但只使用true。 現在它顯示輸出,因為if被評估。
Ant -Dfile.exists = true
E:\worksp\ant\AntProject>Ant -Dfile.exists = true
Buildfile: E:\worksp\ant\AntProject\build.xml
compile:
run:
     [echo] File is executed
BUILD SUCCESSFUL
Total time: 0 seconds
					
						上一篇:
								Apache Ant命令行參數
												下一篇:
								Apache Ant類型
					
					