回答
使用類型設置爲「dir」的available任務。
例如:
<available file="${dir}" type="dir"/>
的標準方法做條件處理與condition task。在下面的示例中,如果目錄存在,則運行doFoo將回顯消息,而運行doBar將回顯消息,除非該目錄存在。
的dir.check目標由兩個doFoo和doBar需要,它集dir.exists屬性取決於可用任務的結果真的還是假的。 doFoo目標將只在該propery設置爲true並且doBar只在未設置或設置爲false時才運行。
<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
<property name="directory" value="c:\test\directory"/>
<target name="doFoo" depends="dir.check" if="dir.exists">
<echo>${directory} exists</echo>
</target>
<target name="doBar" depends="dir.check" unless="dir.exists">
<echo>${directory} missing"</echo>
</target>
<target name="dir.check">
<condition property="dir.exists">
<available file="${directory}" type="dir"/>
</condition>
</target>
</project>
Antelope提供了額外的任務,包括一個如果任務,可以使處理更簡單(和我更直觀),您可以下載從download page羚羊任務。
下面是將available
元素合併到if
測試中的一個小例子。
<!-- Test if a directory called "my_directory" is present -->
<if>
<available file="my_directory" type="dir" />
<then>
<echo message="Directory exists" />
</then>
<else>
<echo message="Directory does not exist" />
</else>
</if>
警告:你需要螞蟻contrib.jar在ANT_HOME \ lib目錄,否則您將無法訪問if
元素,你的腳本將失敗,此錯誤:
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
我喜歡這種解決方案的簡單性和表現力。值得額外的繁重工作來安裝ant-contrib.jar。 – 2010-12-28 22:05:20
這裏是我的解決方案,它不需要設置屬性,並使用目標與「如果」或「除非」:
宏:
<macrodef name="assertDirAvailable">
<attribute name="dir" />
<sequential>
<fail message="The directory '@{dir}' was expected to be available but is not">
<condition>
<not>
<available file="@{dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</macrodef>
用法:
<assertDirAvailable dir="${dirToCheck}" />
使用ANT 1.8版本我的解決方案,舊版本可能無法正常工作,由於如果/除非不支持$ {} evalTrueOrFalse語法。
<?xml version="1.0" encoding="UTF-8"?>
<project name="DoMagic" default="build" basedir=".">
<property environment="env" />
<property name="name" value="Do the ANT Magic" />
<property name="somedir" value="./must_exist_folder"/>
<tstamp><format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>
<target name="doMagic" if="${dir.exists}">
<echo message="Do the magic stuff" />
</target>
<target name="doUsage" unless="${dir.exists}">
<echo message="Do usage and help" />
</target>
<target name="build">
<echo message="Do the magic" />
<condition property="dir.exists" else="false"><available file="${somedir}" type="dir" /></condition>
<echo message="Folder found: ${dir.exists}" />
<antcall target="doCustomize"></antcall>
<antcall target="doUsage"></antcall>
</target>
</project>
- ANT 1.6或早期ANT 1.7不起作用,升級到1.8 ANT釋放。
- 目標屬性如果和除非評估$ {VAR}語法真/假
- 條件屬性其他值設置爲屬性(如果可用)的條件是假的,沒有它變量沒有設置。 NotSet值與顯式的假值不同。
- 調用的任何目標,但如果/除非屬性定義其是否實際運行
http://ant.apache.org/manual/properties.html#if+unless
[如果/除非在螞蟻1.7.1及更早版本,這些屬性只能是屬性名稱。從Ant 1.8.0開始,您可以改爲使用屬性擴展。與舊款相比,這給你更多的靈活性。
這裏是另一種方法,只允許在不使用ant-contrib.jar的情況下調用一個任務。
<target name="my-task" depends="dir-check">
<antcall target="my-task-install"/>
<antcall target="my-task-update"/>
</target>
<target name="my-task-install" unless="dir.exists" >
{some task}
</target>
<target name="my-task-update" if="dir.exists" >
{another task}
</target>
<target name="dir-check">
<condition property="dir.exists">
<available file="my-dir" type="dir" />
</condition>
</target>
- 1. 有沒有辦法檢查Apache配置文件中是否存在目錄?
- 2. PHP檢查文件是否存在而不是目錄
- 3. 檢查目錄是否不存在
- 4. 檢查lua中是否存在目錄?
- 5. Ant檢查是否存在遠程目錄
- 6. 如何檢查Swift文檔目錄中是否存在文件?
- 7. 檢查文件是否保存在文檔目錄中
- 8. 檢查文檔目錄中是否存在文件
- 9. Perl - 檢查目錄中是否存在數組中的文件
- 10. 在Meteor公共目錄中檢查文件是否存在
- 11. 如何使用ANT在刪除目錄之前檢查目錄是否存在?
- 12. 檢查目錄中是否存在多個文件
- 13. 檢查目錄中是否存在兩個文件
- 14. 檢查文件是否存在於指定的目錄中
- 15. 如何檢查目錄中是否存在文件?
- 16. 檢查目錄中是否存在多個文件
- 17. 檢查文件是否存在於特定目錄的shell中
- 18. 檢查目錄中是否存在多個文件
- 19. 檢查文件是否存在與Unix中的目錄
- 20. Python:檢查給定目錄中是否存在任何文件
- 21. 檢查目錄中是否存在多個文件 - matlab
- 22. 如何檢查目錄中是否存在文件
- 23. 檢查Ant中是否存在「至少一個文件」
- 24. 可可 - 有沒有辦法檢查NSURLConnection是否存在緩存?
- 25. 有沒有辦法檢查文件是否在使用
- 26. 如何檢查目錄是否存在?
- 27. Laravel s3檢查目錄是否存在。
- 28. SVNKit - 檢查目錄是否存在
- 29. Talend - 檢查目錄是否存在
- 30. NSIS檢查是否存在空目錄
但是,這將設置屬性值爲true。那我應該如何檢查一下情況。我的意思是任何「如果」? – 2009-07-23 05:34:43
爲什麼驗證dir.check在doFoo和doBar之後?不應該是另一種方式? @賣方 – 2017-07-24 17:06:01