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. Python 经典正则表达式语法实例

  2. phpcms 允许英文目录有空格

    大家都用过phpcm添加栏目吧,在添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文 ...

  3. settings.gradle与build.gradle有什么区别

    参考回答 settings.gradle文件是gradle项目的总体配置文件,一般会把子项目中通用的一些配置放在这个文件中,有点雷士与maven的parent pom 文件. build.gradle ...

  4. svn钩子(hooks)自动部署代码到web目录

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/li956732806/article/details/71158869   web目录:/hoe ...

  5. 2019-8-31-C#-循环的判断会进来几次

    title author date CreateTime categories C# 循环的判断会进来几次 lindexi 2019-08-31 16:55:58 +0800 2018-06-14 0 ...

  6. 通过在__init__.py中定义__all__变量,来简化from*import*的书写

    下图是一个带被引入使用的包的结构,包名比较长,给书写from*import*带来很多麻烦 为了解决麻烦,在__init__.py编写了如下内容 from .httputil import HTTPUt ...

  7. 使用virtualenv使得Python2和Python3并存

    1:下载python3源码并安装 wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz .tgz cd Python-.tgz . ...

  8. Resource Management in View Controllers

    UIViewController生命周期 UIViewControl是IOS程序中的一个重要组成部分,扮演者一个大管家的身份,管理着程序中的众多视图,今天看看了官方文档并做了如下一些简单的记录: 何时 ...

  9. 小爬爬4:selenium操作

    1.selenium是什么? selenium: - 概念:是一个基于浏览器自动化的模块. - 和爬虫之间的关联? - 帮我我们便捷的爬取到页面中动态加载出来的数据 - 实现模拟登陆 - 基本使用流程 ...

  10. MUI - 为textarea添加语音输入和清除的功能

    为textarea添加语音输入和清除的功能 mui支持input输入框语音输入和清除的功能,只需要添加相关css类即可. http://www.cnblogs.com/phillyx/ 代码如下 &l ...