码迷,mamicode.com
首页 > 其他好文 > 详细

ant 脚本 available 及条件判断功能

时间:2015-01-13 23:06:55      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

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

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

 

 1 <project name="test" basedir="." default="copy">
 2     
 3     <!--
 4     为了使用ant的 if [isset] - then - else 功能,定义任务,并将其引入urn:contrib-ant命名空间
 5      -->
 6     
 7     <taskdef resource="net/sf/antcontrib/antcontrib.properties" uri="urn:contrib-ant">
 8         <classpath>
 9             <pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
10         </classpath>
11     </taskdef>
12     <!--  
13     Sets a property if a resource is available at runtime. 
14     在运行时,如果一个资源(文件、目录、类、JVM系统资源等)可以得到,
15     就设置一个属性,其属性值默认设为true,否则不设置
16     This resource can be a file, a directory, a class in the classpath, or a JVM system resource.
17     If the resource is present, the property value is set to true by default; otherwise, the property is not set. 
18     You can set the value to something other than the default by specifying the value attribute.
19     除了默认值,你还能够通过指定value属性,设置为其他值
20         
21     如果./test/target/test-1.0.jar存在,则设置属性test.exist,并让其取默认值true,否则不设置该属性,此处设测试值xxxxx
22      -->
23     <available property="test.exist" file="test-1.0.jar" filepath="./test/target" value="xxxxx"/>
24     
25     
26     <target name="copy" description="Test Copy" xmlns:c="urn:contrib-ant">
27         <c:if>
28             <!-- 
29             如果当前上下文中存在test.exit属性,则返回true,则返回false 
30             
31             <c:equals arg1="${test.exist}" arg2="xxxxx" /> 可完成相同判断功能
32             -->
33             <c:isset property="test.exist" />
34             
35             <c:then>
36             <!-- 如果存在test.exit属性,则拷贝到test/libdb目录 -->
37                 <copy todir="test/libdb" preservelastmodified="true">
38                     <fileset dir="test/target">
39                         <include name="test-1.0.jar" />
40                     </fileset>
41                 </copy>
42                 <echo>属性test.exist的值为: ${test.exist}</echo>
43             </c:then>
44             
45             <c:else>
46             <!-- 如果不存在test.exit属性,则拷贝到test/libdb目录 -->
47                 <echo>./test/target/test-1.0.jar文件不存在,无法进行拷贝</echo>
48             </c:else>
49         </c:if>
50     </target>
51 
52     <path id="runtime.path">
53         <fileset dir="../resources">
54             <include name="**/*.jar" />
55         </fileset>
56     </path>
57     
58     <target name="test-available" description="a test of the task available">
59         
60         <!-- 如果在runtime.path引用的类路径中存在esb.chapter3.Person类,则设person.class.present属性为exist -->
61         <available classname="esb.chapter3.Person" 
62                    property="person.class.present"
63                    classpathref="runtime.path" value="exist"/>
64         <echo>${person.class.present}</echo>
65         
66         <property name="workspace.home" value="D:/eclipse-luna-jee/workspace/z_servicemix" />
67         <available classname="esb.chapter3.Order" property="order.exist">
68             <classpath>
69                 <path refid="runtime.path" />
70                 <!--  
71                 pathelement
72                     location属性,接收一个文件或目录
73                     path属性,功能相当于一个内嵌的<path>元素,使用起来比较随意,接收一个分号分隔的位置列表
74                     注: location和path属性一般可以通用,当涉及到一个分号分隔的位置列表时,只能用path属性
75                  -->
76                 <pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
77                 <pathelement location="${workspace.home}/resources" />
78                 <pathelement path="${workspace.home}/src:${workspace.home}/bin" />
79             </classpath>
80         </available>
81         <echo>${order.exist}</echo>
82         
83         <!-- 如果./lib/jaxp11/jaxp.jar文件存在,设jaxp.jar.present属性为true -->
84         <property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/>
85         <available file="${jaxp.jar}" property="jaxp.jar.present"/>
86         
87         <!-- 如果/usr/local/lib目录存在,设local.lib.present属性为true -->
88         <available file="/usr/local/lib" type="dir" property="local.lib.present"/>
89         
90         <!-- 如果xxx\lib\ant-contrib.jar包中存在net/sf/antcontrib/antcontrib.properties资源文件,设have.res属性为true -->
91         <available property="have.res" resource="net/sf/antcontrib/antcontrib.properties">
92           <classpath>
93             <pathelement location="D:\osesbinaction\libraries\ant-contrib\lib\ant-contrib.jar" />
94           </classpath>
95         </available>
96         <echo>have.extras = ${have.res}</echo>
97     </target>
98 </project>

 

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

 1 <project name="test" basedir="." default="copy">
 2 
 3     <target name="copy">
 4         <condition property="test.exist">
 5             <and>
 6                 <available file="test-1.0.jar" filepath="test/target" />
 7             </and>
 8         </condition>
 9         <!-- 下面的2个任务都尝试执行,但只有测试条件通过的任务体才会被执行 -->
10         <antcall target="copy-target" />
11         <antcall target="echoUnexiting" />
12     </target>
13 
14     <target name="copy-target" if="test.exist" description="Test Copy">
15         <copy todir="test/libdb" preservelastmodified="true">
16             <fileset dir="test/target">
17                 <include name="test-1.0.jar"/>
18             </fileset>
19         </copy>
20     </target>
21     
22     <target name="echoUnexiting" unless="test.exist">
23         <echo>./test/target/test-1.0.jar文件不存在,无法进行拷贝</echo>
24     </target>
25 </project>

 

ant 脚本 available 及条件判断功能

标签:

原文地址:http://www.cnblogs.com/asnjudy/p/4222569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!