评注: 用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 转载请保留此声明, 谢谢!! ]

转: ant condition使用的更多相关文章

  1. ant的condition任务

    1.istrue isfalse:断言 真 假 <project name="testCondition"> <target name="test&qu ...

  2. jenkins / ant / jmeter 持续集成接口自动化

    1. 将 jmeter 脚本放在/var/lib/jenkins/workspace/Jmeter_auto/jmxpath路径下 2. 点击http://jk.facebank.net.cn/job ...

  3. 关于 ant 不同渠道自动打包的笔记

    必要的java.android.ant文件及循环打包用到的ant的jar 下载Ant(这里的Ant不是eclipse和android SDk里面自带的ant)      官方下载地址:http://a ...

  4. Ant 命令行编译Android项目

    首先把android sdk下的tools目录加到系统path环境变量里, 要么就得直接指定android.bat的绝对路径 对于一个新项目, 可以用这个命令创建需要的ant编译环境(可以看到andr ...

  5. windows下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件

    原创文章,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4535459.html android 程序打包成apk,如果在是命令行方式,一般都要经过如下步骤: 1.用a ...

  6. 在Android开发中使用Ant 二:进行一次完整的打包

    一次完整的Android打包要进行以下的几步:编译.代码混淆.打包apk.签名apk.apk优化. 为了能包涵使用NDK的情况,在这里使用一个有native代码的工程TestJni. 在工程根目录下新 ...

  7. 在Android开发中使用Ant 一:环境的搭建及入门

    配置Ant环境 下载Ant:http://ant.apache.org/bindownload.cgi 在windows上应该选择zip压缩包,将zip压缩包解压到一个目录. 打开系统环境变量,在系统 ...

  8. ant exec

    http://ant.apache.org/manual/Tasks/exec.html Exec Description Executes a system command. When the os ...

  9. Ant Tasks 使用总结

    xmlproperty http://ant.apache.org/manual/Tasks/xmlproperty.html Ant的xmlproperty的Task能直接读取一个xml文件以生成相 ...

随机推荐

  1. JedisPool使用原理和源代码

    1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...

  2. jquery 更换皮肤

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Xcode 6 模拟器路径

    原文地址:http://leancodingnow.com/xcode-6-simulator-folder/ 本文主要介绍一下Xcode 6的iOS模拟器的应用目录的变化. Xcode 5的iOS模 ...

  4. Animated Scroll to Top

    Due to a number of requests, I'm writing a detail tutorial on how to create an animated scroll to to ...

  5. C# 多线程处理相关说明: WaitHandle,waitCallback, ThreadPool.QueueUserWorkItem

    class TestThread { static void Main() { //使用WaitHandle静态方法阻止一个线程,直到一个或多个同步对象接收到信号 WaitHandle[] waitH ...

  6. 【WebForm】Js调用后台C#方法

    因业务的需要,有这么个需求,需要前台的JS传参调用C#后台的方法.现在有这么个方法可以解决,整理如下. 首先,先说一下基本实现,前台用Jquery的ajax将其中的URL后加方法,然后在Data中传递 ...

  7. js设置控件的隐藏与显示的两种方法

    js设置控件的隐藏与显示,设置控件style的display和visibility属性就可以了,下面有个示例,需要的朋友可以参考下用JavaScript隐藏控件的方法有两种,分别是通过设置控件的sty ...

  8. SAP Connector 3.0 .Net 开发

    在VS2010中使用控制台应用程序使用SAP Connector 3.0开发时,当程序运行到实例化RfcConfigParameters时报错 (RfcConfigParametersrefcon = ...

  9. Codeforces Round #342 (Div. 2) A - Guest From the Past 数学

    A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...

  10. cdoj 题目简单分类

    如有错误请联系我,下面题的题解,除了傻逼题均可以在我blog中查找 1 傻逼题 3 傻逼题 4 傻逼题 15 dfs 24 傻逼题 25傻逼题 26 傻逼题 30 flyod 31 01背包 42 k ...