2009-07-22 100 views

回答

90

使用類型設置爲「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羚羊任務。

+1

但是,這將設置屬性值爲true。那我應該如何檢查一下情況。我的意思是任何「如果」? – 2009-07-23 05:34:43

+0

爲什麼驗證dir.check在doFoo和doBar之後?不應該是另一種方式? @賣方 – 2017-07-24 17:06:01

29

下面是將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. 
+2

我喜歡這種解決方案的簡單性和表現力。值得額外的繁重工作來安裝ant-contrib.jar。 – 2010-12-28 22:05:20

8

這裏是我的解決方案,它不需要設置屬性,並使用目標與「如果」或「除非」:

宏:

<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}" /> 
1

使用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開始,您可以改爲使用屬性擴展。與舊款相比,這給你更多的靈活性。

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> 
相關問題