转: ant condition使用
評注: 用c語言的方式來,比喻ant...比較好理解
轉:?http://www.smithfox.com/?e=176
[備忘] Apache Ant中的邏輯判斷
?
[原創鏈接:?http://www.smithfox.com/?e=176 轉載請保留此聲明, 謝謝!!?]
在寫Ant時有時免不了要簡單的邏輯,?本文并沒有創造什么新的好辦法, 只是著眼于將一些 "似懂非懂" 的概念理清一下.
相信第一次遇到這樣的問題時, 你一定能搜索到很多的內容, 零散的concept進入了你的腦中: 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");} }很顯然上面這段代碼很難直接體現在 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");} }為什么要這樣改造, 因為對應的Ant是這樣寫的:
<?xml version="1.0" encoding="UTF-8"?><project name="anttest" default="printf11111"><!-- 這個Ant Project的默認target是printf11111, 為了使Ant能自動調用 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>你肯定會有兩點感受: 一是,覺得這個真的很啰嗦, 二是, 這么多的新出來的字眼, 我到哪去找呀?
好吧, 先解決第二個問題, 給幾個鏈接:
http://ant.apache.org/manual/Tasks/conditions.html
http://ant.apache.org/manual/targets.html
再看第一個問題, 在啰嗦中找點規律:
1. Ant的邏輯分支的粒度是 target,?因為 if 和 unless(作用相當于else) 是 target的屬性
2. Ant的邏輯體現在 property(相當于變量)上, 因為 if 和 unless 只接受 property
3. condition這個task, 是邏輯組合器, 它的作用相當于: var flag:Boolean = (xxx);
你會發現寫一個這么簡單的東東, 都要搞好幾個target, 主要還是因為: "Ant的邏輯分支持粒度是 target", 在Ant中比target小的粒度是 task, 那有沒有task級別的 邏輯分支呢? 這時候 ant-contrib 就華麗登場了.
其實ant-contrib 重用了 Ant的conditions(不是condition task), 而廢棄了 condition 這個 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>你會發現用ant-contrib比直接用 ant內置的簡潔多了, 而且可讀性也增強了. 這主要是因為, if, else 這樣的邏輯分支已經是?ant task 級別了.
[原創鏈接:?http://www.smithfox.com/?e=176 轉載請保留此聲明, 謝謝!!?]
轉載于:https://www.cnblogs.com/jhj117/p/5626109.html
總結
以上是生活随笔為你收集整理的转: ant condition使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到砍甘蔗代表什么预兆
- 下一篇: 博客地址迁移