Java_Ant 使用逻辑判断Condition

在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了。

下面是ant的官方参考文档

更复杂的可以参考

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

Condition
Description
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.


1、istrue isfalse:断言 真 假
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <istrue value="true"/>                   
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

  2、逻辑运算
    2.1、not 逻辑非
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <not>
                <istrue value="true"/>                   
            </not>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>
    2.2、and 逻辑与
<project name="testCondition">
    <target name="test">
        <condition property="scondition">
            <and>
                <istrue value="true"/>
                <istrue value="false"/>                   
            </and>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>
    2.3、or 逻辑或 xor异或 (语法上与and类似)
4、isset 指定属性是否存在
<project name="testCondition">
    <!--属性也可以通过ant参数-D来设置-->
    <property name="name" value="this is name"/>   
    <target name="test">
        <condition property="scondition">
            <!--如果属性name不存在则返回false-->
            <isset property="name"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>
    5、equals 是否相等
<project name="testCondition">
    <!--属性也可以通过ant参数-D来设置-->
    <property name="name" value="this is name"/>   
    <target name="test">
        <condition property="scondition">
            <!--如果arg1的值与arg2的值相等返回true,否则为false-->
            <equals arg1="${name}" arg2="this is name"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>   
    6、filesmatch 比较文件
<project name="testCondition">       
    <target name="test">
        <condition property="scondition">
            <!--如果file1所代表的文件与file2所代表的文件相等返回true,否则为false-->
            <filesmatch file1="testfile1.txt" file2="testfile2.txt"/>
        </condition>
        <antcall target="isTrue"></antcall>
        <antcall target="isFalse"></antcall>       
    </target>
    <target name="isTrue" if="scondition">
        <echo>is ture</echo>
    </target>
    <target name="isFalse" unless="scondition">
        <echo>is false</echo>
    </target>
</project>

猜你喜欢

转载自dreamforday.iteye.com/blog/1299722