转: ant condition使用
評注: 用c語言的方式來,比喻ant...比較好理解
轉(zhuǎn):?http://www.smithfox.com/?e=176
[備忘] Apache Ant中的邏輯判斷
?
[原創(chuàng)鏈接:?http://www.smithfox.com/?e=176 轉(zhuǎn)載請保留此聲明, 謝謝!!?]
在寫Ant時(shí)有時(shí)免不了要簡單的邏輯,?本文并沒有創(chuàng)造什么新的好辦法, 只是著眼于將一些 "似懂非懂" 的概念理清一下.
相信第一次遇到這樣的問題時(shí), 你一定能搜索到很多的內(nèi)容, 零散的concept進(jìn)入了你的腦中: condition, if, else, else if, then, unless, avaliable, ant-contrib.
先不管這些, 看一段 ?程序員都能看懂的代碼:
function test():void {if( (a!=null && b=="hello") || ( fileExist("/good.txt") ) ) {printf("11111");} else {printf("33333");} }很顯然上面這段代碼很難直接體現(xiàn)在 Ant這樣以XML為載體的描述式腳本中, 再改造一下:
function test():void {var flag:Boolean = conditaion( or( and(a!=null,b=="hello"), fileExist("/good.txt") ) );if( flag ) {printf("11111");} else {printf("33333");} }為什么要這樣改造, 因?yàn)閷?yīng)的Ant是這樣寫的:
<?xml version="1.0" encoding="UTF-8"?><project name="anttest" default="printf11111"><!-- 這個(gè)Ant Project的默認(rèn)target是printf11111, 為了使Ant能自動調(diào)用 printf33333將 printf33333 放到它的 depends --><target name="printf1111" depends="getflag, printf33333" if="flag"><echo message="11111"/></target><target name="printf33333" depends="getflag" unless="flag"><echo message="33333"/></target><target name="getflag"><condition property="flag"><or><and><isset property="a"/><equals arg1="${b}" arg2="hello" /></and><available file="/good.txt" type="file"/></or></condition></target> </project>你肯定會有兩點(diǎn)感受: 一是,覺得這個(gè)真的很啰嗦, 二是, 這么多的新出來的字眼, 我到哪去找呀?
好吧, 先解決第二個(gè)問題, 給幾個(gè)鏈接:
http://ant.apache.org/manual/Tasks/conditions.html
http://ant.apache.org/manual/targets.html
再看第一個(gè)問題, 在啰嗦中找點(diǎn)規(guī)律:
1. Ant的邏輯分支的粒度是 target,?因?yàn)?if 和 unless(作用相當(dāng)于else) 是 target的屬性
2. Ant的邏輯體現(xiàn)在 property(相當(dāng)于變量)上, 因?yàn)?if 和 unless 只接受 property
3. condition這個(gè)task, 是邏輯組合器, 它的作用相當(dāng)于: var flag:Boolean = (xxx);
你會發(fā)現(xiàn)寫一個(gè)這么簡單的東東, 都要搞好幾個(gè)target, 主要還是因?yàn)? "Ant的邏輯分支持粒度是 target", 在Ant中比target小的粒度是 task, 那有沒有task級別的 邏輯分支呢? 這時(shí)候 ant-contrib 就華麗登場了.
其實(shí)ant-contrib 重用了 Ant的conditions(不是condition task), 而廢棄了 condition 這個(gè) task, 代之以 if, else, elseif再加then 這樣的task.
用 ant-conrib的例子如下:
<?xml version="1.0" encoding="UTF-8"?><project name="anttest" default="print"><property name="a" value="somevalue"/><property name="b" value="hello"/><taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${basedir}/ant-contrib-1.0b3.jar" /><target name="print"><if><or><and><isset property="a"/><equals arg1="${b}" arg2="hello" /></and><available file="/good.txt" type="file"/></or><then><echo message="11111" /></then><else><echo message="33333" /></else></if></target></project>你會發(fā)現(xiàn)用ant-contrib比直接用 ant內(nèi)置的簡潔多了, 而且可讀性也增強(qiáng)了. 這主要是因?yàn)? if, else 這樣的邏輯分支已經(jīng)是?ant task 級別了.
[原創(chuàng)鏈接:?http://www.smithfox.com/?e=176 轉(zhuǎn)載請保留此聲明, 謝謝!!?]
轉(zhuǎn)載于:https://www.cnblogs.com/jhj117/p/5626109.html
總結(jié)
以上是生活随笔為你收集整理的转: ant condition使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到砍甘蔗代表什么预兆
- 下一篇: 博客地址迁移