ant 条件判断 condition(转)
本文转自:http://www.51testing.com/?uid-350678-action-viewspace-itemid-809501
用ant写个build.xml,用到condition,作笔记如下:
basicelements:istrueisfalsenotandorxoravailableissetequalsfilesmatch
1、istrueisfalse:断言真假
<projectname="testCondition">
<targetname="test">
<conditionproperty="scondition">
<istruevalue="true"/>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
2、逻辑运算
2.1、not逻辑非
<projectname="testCondition">
<targetname="test">
<conditionproperty="scondition">
<not>
<istruevalue="true"/>
</not>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
2.2、and逻辑与
<projectname="testCondition">
<targetname="test">
<conditionproperty="scondition">
<and>
<istruevalue="true"/>
<istruevalue="false"/>
</and>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
2.3、or逻辑或xor异或(语法上与and类似)
3、available是否可用
<projectname="testCondition">
<pathid="all.test.classes">
<pathelementlocation="bin"/>
</path>
<targetname="test">
<conditionproperty="scondition">
<!--在指定的classpath路径下是否存在资源TestTest.class-->
<availableresource="TestTest.class">
<classpathrefid="all.test.classes"/>
</available>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
4、isset指定属性是否存在
<projectname="testCondition">
<!--属性也可以通过ant参数-D来设置-->
<propertyname="name"value="thisisname"/>
<targetname="test">
<conditionproperty="scondition">
<!--如果属性name不存在则返回false-->
<issetproperty="name"/>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
5、equals是否相等
<projectname="testCondition">
<!--属性也可以通过ant参数-D来设置-->
<propertyname="name"value="thisisname"/>
<targetname="test">
<conditionproperty="scondition">
<!--如果arg1的值与arg2的值相等返回true,否则为false-->
<equalsarg1="${name}"arg2="thisisname"/>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>
6、filesmatch比较文件
<projectname="testCondition">
<targetname="test">
<conditionproperty="scondition">
<!--如果file1所代表的文件与file2所代表的文件相等返回true,否则为false-->
<filesmatchfile1="testfile1.txt"file2="testfile2.txt"/>
</condition>
<antcalltarget="isTrue"></antcall>
<antcalltarget="isFalse"></antcall>
</target>
<targetname="isTrue"if="scondition">
<echo>isture</echo>
</target>
<targetname="isFalse"unless="scondition">
<echo>isfalse</echo>
</target>
</project>