ant 脚本 available 及条件判断功能
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 及条件判断功能的更多相关文章
- Linuxshell脚本之if条件判断
IF条件判断 .基本语法: if [ command ]; then 符合该条件执行的语句 fi .扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comman ...
- shell脚本编程之条件判断
条件测试类型: 整数测试 字符测试 文件测试 条件测试的表达式的三种方法: 1.[ expression ] 命令测试 2.[[ expression ]] 关键字测试 3.test expressi ...
- shell 脚本基础与条件判断
#!shell脚本格式决定专业性 #!/bin/bash #filename:脚本名 #author:作者 #date:时间 #脚本作用 脚本的执行方式 #脚本名为wk.sh 绝对路径 /root/ ...
- Linux shell脚本之 if条件判断 (转)
IF条件判断 1.基本语法: if [ command ]; then 符合该条件执行的语句 fi 2.扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comm ...
- shell脚本--分支、条件判断
在看选择判断结构之前,请务必先看一下数值比较与文件测试 if....else... #!/bin/bash #文件名:test.sh score=66 # //格式一 if [ $score -lt ...
- 5-4 bash脚本编程之三 条件判断及算术运算
1. 反引号是引用执行结果,并非是返回值 如下是错误的,结果是一行行记录,不是返回值 放大为: 练习 2. shell中如何进行算术运算 A=3 B=4 1. let算术运算表达式 2. $[算术运算 ...
- css3条件判断_@supports的用法 以及 Window.CSS.supports()的使用
为了判断浏览器是否支持css3的一些新属性样式,当不兼容该样式的时候,我们可以更优雅的降级处理.这就需要使用到css3的条件判断功能:在css中支持@supports标记.或者在js中使用CSS.su ...
- css3条件判断_@supports的用法/Window.CSS.supports()的使用
为了判断浏览器是否支持css3的一些新属性样式,当不兼容该样式的时候,我们可以更优雅的降级处理.这就需要使用到css3的条件判断功能:在css中支持@supports标记.或者在js中使用CSS.su ...
- Shell脚本IF条件判断和判断条件总结
转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结 ...
随机推荐
- This Product is covered by one or more of the folloWing patents
借用一下网络图片,作为描述: 原因: 启动方式使用了网络启动, 解决方案: 进入bios,修改启动方式,禁用网卡驱动,使用从硬盘启动或者从U盘启动即可.
- Dalvik 虚拟机和 Sun JVM 在架构和执行方面有什么本质区别?
目前我理解的是: 两者共同点: 都是解释执行 byte code 都是每个 OS 进程运行一个 VM,并执行一个单独的程序 在较新版本中(Froyo / Sun JDK 1.5)都实现了相当程度的 J ...
- 微信小程序 —— wepy 使用 Vant Weapp
一.下载 npm i @vant/weapp -S --production 下载完毕之后,就可以在 node_modules 文件夹里,看见下载的包了. 2.移动文件夹 把刚刚下载的包文件夹下的 l ...
- WinForm 实现主程序(exe)与其关联类库(*.dll)分开存放
WinForm 实现主程序(exe)与其关联类库(*.dll)分开存放 开发环境: Microsoft Windows 7 SP1 Microsoft.NET Framework 4.0 ( ...
- 【JZOJ4925】【GDOI2017模拟12.18】稻草人
题目描述 YLOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,YLOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下条 ...
- jQuery事件大全(真的很全)
DOM Attribute $("p").addClass(css中定义的样式类型); 给某个元素添加样式$("img").attr({src:"te ...
- 2019.11.12htmlhomework1
ex: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- jquery全屏图片滑动切换
在线演示 本地下载
- [ 浙江大学 数据库系统原理 ] [ MiniSQL ]课程设计
工作流程图如下.代码详见https://github.com/gstggsstt/minisql. 注:课程设计时间紧迫,编码基本没有性能考虑,仅限于实现功能,索引未使用B+tree.
- 通过JS操作CSS
动态效果如图所示: 第一种实现方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...