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>

最新文章

  1. Object类和常用方法
  2. VBA实例收集
  3. @Autowired
  4. C# GDI+ 处理文本的两个小技巧
  5. log4net的配置与使用
  6. 执行大量的Redis命令,担心效率问题?用Pipelining试试吧~
  7. [css]input text ie6/7 border兼容问题
  8. grunt压缩多个js文件和css文件
  9. CentOS 7快速搭建Nodejs开发环境
  10. Eclipse的SVN插件安装
  11. poj 2246 (zoj 1094)
  12. [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
  13. MySQL数据库MyISAM和InnoDB存储引擎的比较【转载】
  14. 201521123029《Java程序设计》第三周学习总结
  15. freemarker报错之十五
  16. 【开源分享】微信营销系统(第三方微信平台)github 开源
  17. List集合联系
  18. dentry path_lookat dput
  19. JS操作Cookies
  20. CSS——background-size实现图片自适应

热门文章

  1. 一个不错的插件(软件).NET开发
  2. hackerrank--- challenges/fp-update-list
  3. iOS 9 学习系列:UIStack View
  4. oracle-ORA-00942错误
  5. ACK容器服务虚拟节点使用阿里云日志服务来收集业务容器日志
  6. js cookies 的写入、读取、删除
  7. PHP验证码文件类
  8. kendo grid 使用小结
  9. 【C++】判断一个图是否有环 无向图 有向图(转载)
  10. IO流之字符流-1