Ant 属性学习

1:http://bbs.csdn.net/topics/320124396

想问一下关于ant变成中taskdef的resource属性有什么作用,这个属性的设置是随意的,还是根据自己系统上安装的ant来的?
例如下面一段代码:
<taskdef resource="net/sf/antcontrib/antlib.properties">
<classpath>
<pathelement location="D:\ant\lib\ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>

终于知道了,net/sf/antcontrib/antlib.properties,是在ant-contrib-1.0b3.jar中的~~

 

2   http://blog.163.com/yang_jianli/blog/static/161990006201272455531446/

Ant 利用第三方的task  

 

一、如何使用第三方任务

 

  Ant可以使用第三方任务,在使用第三方任务之前,需告知Ant,这就是<taskdef>任务的用意。

 

  Ant中声明第三方任务有一下几种方法:

 

  1、声明中指定任务类名,比如:

 

    <taskdef name="document"

 

      classname="xdoclet.doc.DocumentDocletTask"

 

      classpath="${xdoclet.jar}"

 

    />

 

   2、比较常见的做法是,定义个properties文件,在该文件中将任务的名称和类名相对应,在一个文件中可以定义多个任务。在构建文件(build.xml)中,只需要指定该文件和类所在的路径,就可以一次定义多个任务了。比如:

 

      task.properties文件中:

 

     document=xdoclet.doc.DocumentDocletTask

 

     xdoclet=xdoclet.DocletTask

 

     ....

 

      在构建文件中,

 

      <taskdef file="task.properties"

 

               classpath="${xdoclet.jar}"

 

      />

 

   3、一般来说,如果第三方提供自己的任务,都会事先写好properties供他人使用。该properties文件一般存在jar包中,此时在构建文件中声明任务:
    
      <taskdef source="task.properties"
               classpath="${xdoclet.jar}"
      />
二、第三方任务ant-contrib
   要使用第三方任务ant-contrib,首先需在其网站获得ant-contrib.jar。
  ant-contrib主要有下面几个任务:
  1、C++编译和连接任务
  2、propertycopy--允许特性动态展开为解引用的特性。见学习笔记3--特性解引用
  3、osfamily--设置表明操作系统的特性,比如mac,windows,unix。
   <target name="testosfamily">
 <osfamily property="MyOs"/>
 <echo message="MyOs is ${MyOs}"/>
</target>
   将返回:
   [echo] MyOs is windows
  4、逻辑任务if,switch,foreach,trycatch等。
     if逻辑结构,例子: 
    <target name="testif">
  <if><not><isset property="testif1.prop"/></not>
    <then><echo message="testif1 is not set"/>
          <if><isset property="testif2.prop"/>
            <then><echo message="testif2 is  set"/></then>
            <else><echo message="testif2 is not set"/></else>
          </if>
    </then>
    <elseif><isset property="testif2.prop"/>
            <then><echo message="testif1 is set"/>
                  <echo message="testif2 is  set"/>
            </then>
    </elseif>
    <else>
         <echo message="testif1 is  set"/>
         <echo message="testif2 is not set"/>
    </else>
  </if>
</target>
 
   switch的逻辑结构,例子:
    <target name="testswitch">
  <switch value="${testswitch}">
    <case value="branch1">
     <echo message="enter branch1..."/>
    </case>
    <case value="branch2">
     <echo message="enter branch2..."/>
    </case>
    <default>
     <echo message="enter default branch..."/>
    </default>
  </switch>
</target>
 
   使用显示循环foreach,例子:
    foreach有两个要循环的列表,一个用list指定,另一个可用fileset指定。两个选其一就可以了,也可以两个同时存在。target和param必须存在。
    <target name="testforeach">
  <foreach list="1,2,3" target="loop" param="var"
  />
</target>
<target name="loop">
  <echo message="var = ${var}"/>
</target>
 <target name="testforeach">
  <foreach target="loop" param="var">
    <fileset dir="src"/>
   </foreach>
</target>
<target name="loop">
  <echo message="var = ${var}"/>
</target>
 
   捕获任务异常,例子:
 <target name="testtrycatch">
  <trycatch property="exception.prop"
            reference="exception.ref">
       <try>
         <fail>TestException!</fail>
       </try>
       <catch>
         <echo message="exception is caught here"/>
       </catch>
       <finally>
          <echo message="finally"/>
       </finally>
  </trycatch>
  <property name="exref" refid="exception.ref"/>
  <echo message="exception.prop=${exception.prop}, exception.ref=${exref}"/>
</target>
 
   输出结果为:
   [trycatch] Caught exception: TestException!
     [echo] exception is caught here
     [echo] finally
     [echo] exception.prop=TestException!, exception.ref=D:\personal\java\ant\bu
ild.xml:267: TestException!

3

 

 

 

posted @ 2013-03-08 11:44  IamThat  阅读(399)  评论(0编辑  收藏  举报