1. 通过<available property="属性名"  file | classname | resource = "被判定是否存在的东西"  value="给属性名显示指定一个值" ..... /> 存在性判断语句,如果判定的东西存在,则以默认值true/或指定的属性值设置指定的属性;若判定的东西不存在,则不设置该属性。

我们可以根据这个属性是否被设置(通过<isset property="属性名" />判断)、这个属性已被设置的值(<equals arg1="${属性名}" arg2="true|指定的属性值">),执行 if - then - else 判断逻辑。

 <project name="test" basedir="." default="copy">

     <!--
为了使用ant的 if [isset] - then - else 功能,定义任务,并将其引入urn:contrib-ant命名空间
--> <taskdef resource="net/sf/antcontrib/antcontrib.properties" uri="urn:contrib-ant">
<classpath>
<pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
</classpath>
</taskdef>
<!--
Sets a property if a resource is available at runtime.
在运行时,如果一个资源(文件、目录、类、JVM系统资源等)可以得到,
就设置一个属性,其属性值默认设为true,否则不设置
This resource can be a file, a directory, a class in the classpath, or a JVM system resource.
If the resource is present, the property value is set to true by default; otherwise, the property is not set.
You can set the value to something other than the default by specifying the value attribute.
除了默认值,你还能够通过指定value属性,设置为其他值 如果./test/target/test-1.0.jar存在,则设置属性test.exist,并让其取默认值true,否则不设置该属性,此处设测试值xxxxx
-->
<available property="test.exist" file="test-1.0.jar" filepath="./test/target" value="xxxxx"/> <target name="copy" description="Test Copy" xmlns:c="urn:contrib-ant">
<c:if>
<!--
如果当前上下文中存在test.exit属性,则返回true,则返回false <c:equals arg1="${test.exist}" arg2="xxxxx" /> 可完成相同判断功能
-->
<c:isset property="test.exist" /> <c:then>
<!-- 如果存在test.exit属性,则拷贝到test/libdb目录 -->
<copy todir="test/libdb" preservelastmodified="true">
<fileset dir="test/target">
<include name="test-1.0.jar" />
</fileset>
</copy>
<echo>属性test.exist的值为: ${test.exist}</echo>
</c:then> <c:else>
<!-- 如果不存在test.exit属性,则拷贝到test/libdb目录 -->
<echo>./test/target/test-1.0.jar文件不存在,无法进行拷贝</echo>
</c:else>
</c:if>
</target> <path id="runtime.path">
<fileset dir="../resources">
<include name="**/*.jar" />
</fileset>
</path> <target name="test-available" description="a test of the task available"> <!-- 如果在runtime.path引用的类路径中存在esb.chapter3.Person类,则设person.class.present属性为exist -->
<available classname="esb.chapter3.Person"
property="person.class.present"
classpathref="runtime.path" value="exist"/>
<echo>${person.class.present}</echo> <property name="workspace.home" value="D:/eclipse-luna-jee/workspace/z_servicemix" />
<available classname="esb.chapter3.Order" property="order.exist">
<classpath>
<path refid="runtime.path" />
<!--
pathelement
location属性,接收一个文件或目录
path属性,功能相当于一个内嵌的<path>元素,使用起来比较随意,接收一个分号分隔的位置列表
注: location和path属性一般可以通用,当涉及到一个分号分隔的位置列表时,只能用path属性
-->
<pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
<pathelement location="${workspace.home}/resources" />
<pathelement path="${workspace.home}/src:${workspace.home}/bin" />
</classpath>
</available>
<echo>${order.exist}</echo> <!-- 如果./lib/jaxp11/jaxp.jar文件存在,设jaxp.jar.present属性为true -->
<property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/>
<available file="${jaxp.jar}" property="jaxp.jar.present"/> <!-- 如果/usr/local/lib目录存在,设local.lib.present属性为true -->
<available file="/usr/local/lib" type="dir" property="local.lib.present"/> <!-- 如果xxx\lib\ant-contrib.jar包中存在net/sf/antcontrib/antcontrib.properties资源文件,设have.res属性为true -->
<available property="have.res" resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
</classpath>
</available>
<echo>have.extras = ${have.res}</echo>
</target>
</project>

2.  也可通往<condition property="属性名" />, <target name="target1"   if | unless ="属性名" /> 完成判断分支功能

 <project name="test" basedir="." default="copy">

     <target name="copy">
<condition property="test.exist">
<and>
<available file="test-1.0.jar" filepath="test/target" />
</and>
</condition>
<!-- 下面的2个任务都尝试执行,但只有测试条件通过的任务体才会被执行 -->
<antcall target="copy-target" />
<antcall target="echoUnexiting" />
</target> <target name="copy-target" if="test.exist" description="Test Copy">
<copy todir="test/libdb" preservelastmodified="true">
<fileset dir="test/target">
<include name="test-1.0.jar"/>
</fileset>
</copy>
</target> <target name="echoUnexiting" unless="test.exist">
<echo>./test/target/test-1.0.jar文件不存在,无法进行拷贝</echo>
</target>
</project>

ant 脚本 available 及条件判断功能的更多相关文章

  1. Linuxshell脚本之if条件判断

    IF条件判断 .基本语法: if [ command ]; then 符合该条件执行的语句 fi .扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comman ...

  2. shell脚本编程之条件判断

    条件测试类型: 整数测试 字符测试 文件测试 条件测试的表达式的三种方法: 1.[ expression ] 命令测试 2.[[ expression ]] 关键字测试 3.test expressi ...

  3. shell 脚本基础与条件判断

    #!shell脚本格式决定专业性 #!/bin/bash #filename:脚本名 #author:作者 #date:时间 #脚本作用 脚本的执行方式  #脚本名为wk.sh 绝对路径 /root/ ...

  4. Linux shell脚本之 if条件判断 (转)

    IF条件判断 1.基本语法: if [ command ]; then 符合该条件执行的语句 fi 2.扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comm ...

  5. shell脚本--分支、条件判断

    在看选择判断结构之前,请务必先看一下数值比较与文件测试 if....else... #!/bin/bash #文件名:test.sh score=66 # //格式一 if [ $score -lt ...

  6. 5-4 bash脚本编程之三 条件判断及算术运算

    1. 反引号是引用执行结果,并非是返回值 如下是错误的,结果是一行行记录,不是返回值 放大为: 练习 2. shell中如何进行算术运算 A=3 B=4 1. let算术运算表达式 2. $[算术运算 ...

  7. css3条件判断_@supports的用法 以及 Window.CSS.supports()的使用

    为了判断浏览器是否支持css3的一些新属性样式,当不兼容该样式的时候,我们可以更优雅的降级处理.这就需要使用到css3的条件判断功能:在css中支持@supports标记.或者在js中使用CSS.su ...

  8. css3条件判断_@supports的用法/Window.CSS.supports()的使用

    为了判断浏览器是否支持css3的一些新属性样式,当不兼容该样式的时候,我们可以更优雅的降级处理.这就需要使用到css3的条件判断功能:在css中支持@supports标记.或者在js中使用CSS.su ...

  9. Shell脚本IF条件判断和判断条件总结

    转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结 ...

随机推荐

  1. php安装oci8和pdo_oci扩展实现连接oracle数据库

    PHP一般跟MySQL数据库搭配使用,但最近遇到一个需求需要实现PHP连接Oracle,了解到PHP可以通过pdo_oci和oci8扩展来连接Oracle,这里将安装的过程记录下来. 安装环境:PHP ...

  2. 【JZOJ4772】【NOIP2016提高A组模拟9.9】运输妹子

    题目描述 小轩轩是一位非同一般的的大农(lao)场(si)主(ji),他有一大片非同一般的农田,并且坐落在一条公路旁(可以认为是数轴),在他的农田里种的东西也非同一般--不是什么水稻小麦,而是妹子. ...

  3. Nginx教程(一) Nginx入门教程 (转)

    1 Nginx入门教程 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行.由俄罗斯的程序设计师IgorSysoev所开 ...

  4. WPF/Silverlight深度解决方案:(七)HLSL自定义渲染特效之完美攻略(中)

    原文:WPF/Silverlight深度解决方案:(七)HLSL自定义渲染特效之完美攻略(中) 通过上一节的解说,大家是否已经对HLSL有了较深刻的认识和理解,HLSL的渲染不仅仅局限于静态处理,通过 ...

  5. 【水滴石穿】react-native-template-app

    这个也是一个基础项目 地址如下https://github.com/ndlonghi/react-native-template-app 点击登陆跳转到首页 分析代码如 react-native-te ...

  6. jQuery 五角星评分

    五角星打分 我用的是搜狗输入法上带的特殊符号打出来的  空五角星:☆  实五角星:★ 1.html <ul class="comment"> <li>☆&l ...

  7. CSS3摆动动画效果

    效果图:红包在左右摇晃 代码如下: @keyframes upAnimation { 0 % { transform: rotate(0 deg);transition - timing - func ...

  8. maven 从svn导入项目遇到的问题 No marketplace entries found to handle yuicompressor maven plugin:1.3.0:compile

    版权声明:本文为博主原创文章,未经博主同意不得转载.安金龙 的博客. https://blog.csdn.net/smile0198/article/details/25463825 RT.使用ecl ...

  9. Python 变量赋值

  10. 【datagrid】动态加载列 2016-01-03 16:32 2013人阅读 评论(19) 收藏

    之前我们的项目在前台显示只需要把数据从数据库读出来进行显示就可以,datagrid的表头字段都是写死的,把数据往表里一扔,就基本没什么事儿了,结果客户前几天要求,其中一个字段不能是死的,应该是有多少项 ...