一、ant 调用另一个构建文件中的任务。
如果不指定antfile,则缺省为build.xml。
其格式一般为:
<target name="testant">
<ant antfile="buildInternal.xml"
dir="builddir"
inheritall="true"
inheritrefs="true"
target="testant"
>
<reference refid="filter.set" torefid="NewFilter"/>
</ant>
</target>
antfile表示子项目的构建文件。
dir表示构建文件所再的目录,缺省为当前目录。
inheritall表示父项目的所有属性在子项目中都可使用,并覆盖子项目中的同名属性。缺省为true。
inheritrefs表示父项目中的所有引用在子项目中都可以使用,并且不覆盖子项目中的同名引用。缺省为false。
如果在ant任务中显示的定义引用,如上例<reference refid="filter.set">则该引用将会覆盖子项目中的同名引用。
target表示所要运行的子项目中的target,如果不写则为缺省target。
二、antcall调用当前构建文件的另一个任务。
注意,不一定是同一个项目。也就是说,同一个构建文件可能包含多个项目,其余的项目可以通过import构建文件来获得。
其使用和ant有些类似。
格式为:
<antcall target="testant2" inheritrefs="true">
<param name="newprop" value="newvalue"/>
<reference refid="filter.set" torefid="NewFilter"/>
</antcall>
param相当于传给新target的property。
inheritrefs表示父项目中的所有引用在子项目中都可以使用,并且不覆盖子项目中的同名引用。缺省为false。如果在ant任务中显示的定义引用,如上例<reference
refid="filter.set">则该引用将会覆盖子项目中的同名引用。
三、antstructure为当前的构建文件产生DTD,需指定output文件。但是产生的DTD文件并不完整。
四、apply继承自exec,接受文件集作为其输入。
五、apt注释处理器
六、Available判断某个类,或某个文件,或某个路径。如果存在,则设置某个property。
其格式如下:
判断某个类是否存在
<available property ="resource.exists" classname ="package1.test1" classpath ="dist/project1.jar"/>
判断某个文件是否存在
<available property ="resource.exists1" file = "src/test.txt" type= "file"/>
判断某个资源是否存在
<available property ="resource.exists2" resource="package1/test2.class" classpath="dist/project1.jar"/>
七、basename相当于定义了一个property,其值为一个文件的名称。
如果文件是在某个路径中的,则只会读取文件的名字,同时也可以通过suffix来去掉文件的后缀。
如果文件是一个目录,则会读取目录的最后一项。例如d:/program
files/mydirectory.则只会截取mydirectory。
例子:
<target name="testbasename">
<basename property="basefile" file="d:/personal/java/ant/testfile.txt" suffix="txt"/>
<if><isset property="basefile"/>
<then>
<echo>the basename is set${basefile}</echo>
</then>
<else>
<echo>the basename is notset</echo>
</else>
</if>
</target>
实际的输出将是:[echo] the basename is set testfile
实际上只是一个字符串处理过程,文件不存在也同样会设置property。
八、buildnumber只有一个属性,file。
其功能是,向指定文件中写入一个属性build.number,初始化的时候build.number=1。
如果文件中已经有
build.number=...
则读取build.number,并在原来的数值的基础上加1,并写回文件。
最后,build.number的值必须为整形。
九、GUnzip/BUnzip2,Gzip/Bzip2
.gz以及.bz2文件的解压缩任务。
将文件打包成.gz
,.bz2文件
十、chmod--unix环境下改变文件的访问权限。
十一、concat--把一组文件合并成一个文件。这个任务支持嵌套文本,如果有的话附加在文件后面。例子:
<concat destfile="NOTES.txt"
append="false">
<filelist
dir="notes" files="note1.txt,note2.txt"/>
<fileset
dir="notes2" includes="**/note*.txt"/>
</concat>
十二、condition--有条件的设置属性。
例子:
<condition property="osiswindows">
<and>
<os
family="windows"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>
十三、copy
复制一个文件或目录
十四、delete
删除一个文件或目录,或删除根据fileset定义的文件集合
十五、dependset
检查和删除过时的目的文件。
比较srcfilelist,srcfileset中的文件和targetfilelist,
targetfileset中的所有文件,如果在源目录中有文件比目标目录中的文件新,则删除目标目录中的所有文件。
十六、dirname
和basename正好相反,获取文件或目录所在的路径。
例子:
<target
name="testdirname">
<dirname file="src/package1/test1.java"
property="testdir"/>
<echo>${testdir}</echo>
</target>
将返回:
D:\personal\java\ant\src\package1
十七、ear
创建ear文件,例子:
<ear
destfile="build/myapp.ear"
appxml="src/metadata/application.xml">
<fileset dir="build" includes="*.jar,*.war"/>
</ear>
十八、echo
<echo
message="Deleting drive C:" level="debug"/>
只有debug模式的时候这条message才会显示
十九、exec执行本地程序。
二十、fail--退出当前构建,例子:
<fail>
<condition>
<not>
<isset property="thisdoesnotexist"/>
</not>
</condition>
</fail>
二十一、filter--创建一个全局的filter,要谨慎定义filter。
例子:
<filter token="year" value="2000"/>
<copy todir="${dest.dir}"
filtering="true">
<fileset
dir="src"/>
</copy>
所有的目标文件都会采用到上面定义的filter
二十二、FixCRLF
转换文本文件为本地约定的格式,同时修复因“错误配置的编辑器或者文件转换程序”而损坏的文本文件。
二十三、genkey
二十四、get
从url获取文件。例子:
二十五、import
导入新的构建文件
二十六、input
接受用户控制台输入,例子:
<input
message="All
data is going to be deleted from DB continue (y/n)?"
validargs="y,n"
addproperty="do.delete"
/>
<condition property="do.abort">
<equals
arg1="n" arg2="${do.delete}"/>
</condition>
<fail if="do.abort">Build aborted by
user.</fail>
validargs表示用户只能输入y或者n,用户如果输入n,则do.delete="n"
二十七、jar--文件打包成jar文件。
二十八、java--运行java程序
二十九、javac--编译java文件。
三十、Javadoc/Javadoc2
产生html文档
例子:
<javadoc packagenames="package1.*"
sourcepath="src"
destdir="docs/api"
author="true"
version="true"
use="true"
windowtitle="Test API"/>
三十一、length
例子:
<length string="foo" property="length.foo" />
三十二、LoadFile--将全部的文本文件载入到单个特性,xml文件或txt文件
例子:
<loadfile srcfile="NOTES.txt"
property="load.file"/>
<echo>${load.file}</echo>
输出 将会是文本文件的所有内容
三十三、loadproperties
load property文件。
例子:
<loadproperties srcFile="file.properties">
<filterchain>
<linecontains>
<contains value="import."/>
</linecontains>
</filterchain>
</loadproperties>
<loadproperties>
<gzipresource>
<url
url="http://example.org/url.properties.gz"/>
</gzipresource>
</loadproperties>
insert
一、macrodef
和任务定义差不多,相当于定义了一个函数,函数的参数用attribute来指定,函数的名字用element来指定。
当调用该任务的时候,象调用函数一样,需传入参数。
例子:
<macrodef name="test">
<attribute name="one"/>
<attribute name="two" default="@{one}"/>
<sequential>
<echo>one=@{one} two=@{two}</echo>
</sequential>
</macrodef>
<test ne="test"/>
三十五、mail--A task to send SMTP email. 发送SMTPemail
例子:
<mail
mailhost="somehost@xyz.com" mailport="25" subject="Test
build" charset="utf-8">
<from address="me@myist.com"/>
<to address="all@xyz.com"/>
<message>some international
text:${line2}</message>
</mail>
三十六、Manifest
建立一个清单文件,他将放入某个jar,作为jar文件的说明书。其中,在清单文件可以指定jar文件的main-class,jar文件将可以直接运行。例子:
<manifest >
<attribute name="Built-By"
value="LANLAN"/>
<attribute
name="Main-Class" value="package1.test2"/>
<section
name="common">
<attribute name="Specification-Title" value="Example"/>
<attribute name="Specification-Version" value="1.0"/>
<attribute name="Specification-Vendor" value="Example
Organization"/>
<attribute name="Implementation-Title" value="common"/>
<attribute name="Implementation-Version" value="1.0 December 13
2006"/>
<attribute name="Implementation-Vendor" value="Example
Corp."/>
</section>
</manifest>
三十七、Mkdir
创建目录,例子:
<mkdir
dir="${dist}/lib"/>
三十八、move
将文件移动到另一个目录,或将文件集移动到另一个目录。例子:
<move todir="new/dir/to/move/to">
<fileset
dir="src/dir"/>
</move>
三十九、parallel
在分开的线程中执行所包含的任务,全部完成后再继续运行。在这个任务中可以包含任何ant任务。
四十、patch--通过一个diff文件对某个文件打补丁;要求补丁位于执行路径上。
例子:
<patch patchfile="module.1.0-1.1.patch"/>
四十一、pathconvert
把路径和classpath信息转换成指定的目标的os形式。
例子:
<pathconvert targetos="unix" property="jboss_unix.path"
refid="jboss_classpath">
<map from="c:" to="/home"/>
</pathconvert>
<echo
message="${jboss_unix.path}" />
四十二、Property
四十三、record
向当前的构建过程添加一个监听者,把输出记录到文件
例子:
<record name="log.txt" action="start"/>
<javac ...
<record name="log.txt" action="stop"/>
四十四、replace
用给定值替换指定文件中所有出现一个或者多个token的地方
例子:
<replace
file="index.xml" token="@@@" value="wombat"/>
四十五、rmic
编译远程rmi文件,产生stub以及skeleton文件。
四十六、依次序执行所有嵌套任务的容器任务,在parallel中非常有用
四十七、signjar
对jar文件进行数字签名。首先需要用genkey产生keystore例子
<genkey alias="key1" storepass="secret" keystore="myfirstkey">
<dname>
<param name="CN" value="ant test"/>
<param name="OU" value="aaa"/>
<param name="O" value="bbb"/>
<param name="C" value="CN"/>
</dname>
</genkey>
<signjar
alias="key1"
keystore="myfirstkey"
storepass="secret"
>
<fileset
dir="dist" includes="**/*.jar" />
</signjar>
四十八、sleep
休息或暂停一段时间,例子:
<sleep
milliseconds="10"/>
四十九、sql
对使用jdbc的数据库执行一系列的SQL语句。可以采用文本文件包含sql命令
<sql
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@ipaddress:1521:aliasname"
userid="user"
password="pwd"
onerror="continue">
<fileset
dir="dist">
<include
name="a.sql"
/>
</fileset>
<classpath location="dist/ojdbc.jar"/>
</sql>
五十、tar
创建tar归档文件
<tar destfile="dist/mytar.tar"
basedir="src"
/>
五十一、taskdef
增加任务。任务是继承了org.apache.tools.ant.Task 的java类
和typedef的功能类似
五十二、tempfile
将一个临时文件的名字赋值给一个property
例子
<tempfile property="temp.file" prefix="temp"/>
此时文件名为temp*的文件将赋值给property
五十三、touch--相当于unix的touch命令
五十四、tstamp
设置特性为当前时间,或者与当前时间的偏移量
五十五、unjar,unwar,unzip解压缩文件
五十六、uptodate
如果指定的目标文件比源文件有更新的时间戳,设置给定的特性。
例子:
<uptodate
property="xmlBuild.notRequired">
<srcfiles
dir= "${src}/xml" includes="**/*.dtd"/>
<mapper
type="merge" to="${deploy}\xmlClasses.jar"/>
</uptodate>
五十七、waitfor等待所包含的条件变为有效。
例子:
<waitfor maxwait="30" maxwaitunit="second">
<available file="errors.log"/>
</waitfor>
等待30妙,知道errors。log文件产生
五十八、war打包成war,需指定web.xml
五十九、xmlproperty从一个合法的xml文件中载入特性值,根据这个文件的元素和属性名称生成特性名称。