细说phing iftask中的conditions

iftask测试一堆条件,根据其真伪执行相应的动作。其本身很好理解。
实际在使用这个task的时候发现最麻烦的是测试条件该怎么写,手册将我引向了第五章 Conditons一节,但本节写的实在简单,又没有具体的例子。

下面结合手册中的说明,详细说一下这些条件的使用,并附加实例。

not
<not>元素需要另外一个条件嵌入其中,对条件的结果取反。它没有任何属性值。

and
<and> 元素无任何属性,可以接受多个条件。如果所包含的条件全部为真,则结果为真。

or
<or> 元素无任何属性,可以接受多个条件。如果所包含的条件中有一个为真,则结果为真。条件测试的顺序与其出现在构建文件中的顺序一致。

解说:
如果你了解任何一门编程语言,只要将其当成if语句中的&&(与),||(或),!(非)来理解就可以了。
它们用于将条件取反或连接一组条件。下面将结合euqals元素给出使用示例。

equals
测试两个给定的字串是否相同。
属性
含意
是否必须
arg1
待测字串1
arg2
待测  字串 2
casesensitive
是否大小写敏感,默认true
trim
去掉参数两侧的空白字符,默认false

例:test.xml
<project name="test" default="test">
	<property name="a" value="1"/>
	<property name="b" value="22"/>
	<target name="test">
		<!--example 1-->
		<if>
			<and>
				<equals arg1="${a}" arg2="1"/>
				<equals arg1="${b}" arg2="22"/>
			</and>
			<then>
				<echo msg="it's and test.result is true"/>
			</then>
			<else>
				<echo msg="it's and test.result is false"/>
			</else>
		</if>

		<!--example 2-->
		<if>
			<not>
				<or>
					<equals arg1="${a}" arg2="a"/>
					<equals arg1="${b}" arg2="22"/>
				</or>
			</not>
			<then>
				<echo msg="it's not or test.result is true"/>
			</then>
			<else>
				<echo msg="it's not or test.result is false"/>
			</else>
		</if>
	</target>
</project>
执行phing -f test.xml ,结果为:
test > test:
     [echo] it's and test.result is true
     [echo] it's not or test.result is false
BUILD FINISHED
Total time: 0.2075 seconds
example1, 相当于判定 if ($a == "1" && $b == "22")。
example2,相当于判定 if(!($a == "a" || $b == "22"))

os
测试当前操作系统是否为给定值
属性
含意
是否必须
family
期望的操作系统名
family的可选值有以下三种:
windows (所有的微软windows版本)
mac (所有的苹果系统)
unix(所有Unix及类似系统)

例:
project name="test" default="test">
	<target name="test">
		<if>
			<os family="unix" />
			<then>
				<echo msg="it's unix"/>
			</then>
			<else>
				<echo msg="it's nothing"/>
			</else>
		</if>
	</target>
</project>

isset
测试给定的property值是否被设定
属性
含意
是否必须
property
待测试的property名

contains
测试一个字串是否包含了另一个子串
属性
含意
是否必须
string
主串
substring
子串
casesensitive
是否大小写敏感,默认true
例子:
<project name="test" default="test">
	<target name="test">
		<if>
			<contains string="my name is ball" substring="ball"/>
			<then>
				<echo msg="get it"/>
			</then>
		</if>
	</target>
</project>

istrue
测试一个字串是否为true
属性
含意
是否必须
value
待测值
说明:
空,false,0会得到false,其它字串都相当于true。
比如<istrue value="123"/>,结果为true。

isfalse
测试字串是否非真。
属性
含意
是否必须
value
待测值
例:
<isfalse value="${someproperty}"/>
<isfalse value="false"/>

referenceexists
测试某引用(reference)是否存在
属性
含意
是否必须
ref
待测试的引用
例:
<referenceexists ref="${someid}"/>

available
为个条件就是available task,请参见 core task中的availableTask
例:测试文件是否存在
<if>
    <available file="${releaseDir}/WEB-INF/entry.php-dist" type="file"/>
    <then>
        <move file="${releaseDir}/WEB-INF/entry.php-dist" tofile="${releaseDir}/WEB-INF/entry.php" overwrite="true" />
     </then>
</if>

你可能感兴趣的:(conditions,phing,iftask)