在Ant中使用邏輯判斷 Condition

來源:互聯網
上載者:User

    好久不寫ant指令碼了,最近兩天在用ant做web應用的安裝部署指令碼,為了實現web伺服器的多版本相容,必然要使用邏輯判斷,比如我要判斷是安裝在weblogic8上還是weblogic9上,而在ant中處理邏輯判斷真是麻煩,只能作用於task,要利用property來做判斷,使用available來設定property。例如:

<?xml version="1.0" encoding="GB2312"?>
<project name="weblogic ant task" default="build">
 <target name="detect.file"   >  
  <condition property="fileIsExists"   >  
  <and>  
   <available file="c:/123.txt"/>
  </and>  
  </condition>
 </target>
 <target name="echoDemo" if="fileIsExists" depends="detect.file">  
  <echo message="hello ant"/>
 </target>
 <target name="build">  
  <antcall target="echoDemo"/>
 </target>
</project>
上面判斷一個檔案,如果存在的話 fileIsExists 就為true,echoDemo這個task在執行前會先判斷fileIsExists 是否為true如果不為true就不執行了。c盤下面有123.txt的話會列印hello ant 否則不會列印。

這裡面還有一個小陷阱,我習慣使用antcall,不喜歡使用depends,但是使用antcall的話就會有問題,例如我最開始這麼寫的,就不行。

<?xml version="1.0" encoding="GB2312"?>
<project name="weblogic ant task" default="build">
 <target name="detect.file">  
  <condition property="fileIsExists">  
  <and>  
   <available file="c:/123.txt"/>
  </and>  
  </condition>
 </target>
 <target name="echoDemo" if="fileIsExists">  
  <echo message="hello ant"/>
 </target>
 <target name="build">  
  <antcall target="detect.file"/>
  <antcall target="echoDemo"/>
 </target>
</project>

使用antcall的話在echoDemo這個task執行的時候fileIsExists這個屬性永遠不為true,即便在執行完detect.file後它已經為true了,但是它不會被傳遞到下一個task,沒用深入研究過ant,所以具體內部實現還不瞭解。

下面是ant的官方參考文檔

更複雜的可以參考

http://ant.apache.org/manual/CoreTasks/conditions.html

ConditionDescription

Sets a property if a certain condition holds true - this is a generalization of Available and Uptodate.

If the condition holds true, the property value is set to true by default; otherwise, the property is not set. You can set the value to something other than the default by specifying the value attribute.

Conditions are specified as nested elements, you must specify exactly one condition.

Parameters

Attribute Description Required
property The name of the property to set. Yes
value The value to set the property to. Defaults to "true". No
else The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Ant 1.6.3 No
Parameters specified as nested elements

All conditions to test are specified as nested elements, for a complete list see here.

Examples
  <condition property="javamail.complete">    <and>      <available classname="javax.activation.DataHandler"/>      <available classname="javax.mail.Transport"/>    </and>  </condition>

sets the property javamail.complete if both the JavaBeans Activation Framework and JavaMail are available in the classpath.

  <condition property="isMacOsButNotMacOsX">    <and>      <os family="mac"/>      <not>        <os family="unix"/>      </not>    </and>  </condition>

sets the property isMacOsButNotMacOsX if the current operating system is MacOS, but not MacOS X - which Ant considers to be in the Unix family as well.

  <condition property="isSunOSonSparc">    <os name="SunOS" arch="sparc"/>  </condition>

sets the property isSunOSonSparc if the current operating system is SunOS and if it is running on a sparc architecture.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.