<!--
=================================================================================================================
1、Touch的使用
①若文件不存在,则创建文件;如果存在,则更改最后访问时间为当前系统时间
<touch file="myfile"/>
②若文件不存在,则创建文件,并更改最后访问时间为10/15/2010 2:02 pm
<touch file="myfile" datetime="10/15/2010 2:02 pm"/>
③更改目录下所有文件的最后访问时间为10/15/2010 2:02 pm
<touch datetime="10/15/2010 2:02 pm">
<fileset dir="src_dir"/>
</touch>
=================================================================================================================
2、Condition的使用
有<and>、<or>、<not>等tag
<condition property="isMacOsButNotMacOsX">
<and>
<os family="mac"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>
=================================================================================================================
3、替换replace
<replace file="configure.sh" value="defaultvalue" propertyFile="source/name.properties">
<replacefilter token="@token1@"/>
<replacefilter token="@token2@" value="value2"/>
<replacefilter token="@token3@" property="property.key"/>
</replace>
=================================================================================================================
4、调用chmod
<chmod perm="go-rwx" type="file">
<fileset dir="/web">
<include name="**/*.cgi"/>
<include name="**/*.old"/>
</fileset>
<dirset dir="/web">
<include name="**/private_*"/>
</dirset>
</chmod>
=================================================================================================================
-->
<!--
=================================================================================================================
5、checksum MD5运算
①md5文件,然后把值放入foo.bar.MD5属性<checksum file="foo.bar"/>
②md5文件,然后把值放入foobarMD5属性 <checksum file="foo.bar" property="foobarMD5"/>
③Md5目录下的所有文件,然后建立一个.md5文件,把所有的md5值放入
<checksum>
<fileset dir=".">
<include name="foo*"/>
</fileset>
</checksum>
=================================================================================================================
6、Available的使用
①如果类存在,则设置属性Myclass.present为true,如果没有就false
<available classname="org.whatever.Myclass" property="Myclass.present"/>
②若文件存在,则设置属性jaxp.jar.presen为true,否则为false
<property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/>
<available file="${jaxp.jar}" property="jaxp.jar.present"/>
③若目录存在,则设置属性local.lib.present为true,否则为false
<available file="/usr/local/lib" type="dir" property="local.lib.present"/>
=================================================================================================================
7、设置property
①设置属性<property name="foo.dist" value="dist"/>
②读取属性文件中的属性配置<property file="foo.properties"/>
③读取网络中的属性文件<property url="http://www.mysite.com/bla/props/foo.properties"/>
④读取文件中的属性配置<property resource="foo.properties"/>
⑤读取环境变量<property environment="env"/>
⑥读取属性文件中的属性,并作为全局引用<property file="/Users/antoine/ant-global.properties"/>
=================================================================================================================
8、显示错误
①方式一<fail>Something wrong here.</fail>
②方式二<fail message="${属性}"/>
③如果这个属性不存在显示错误<fail unless="failCondition" message="unless Condition"/>
④如果这个属性存在显示错误<fail if="failCondition" message="if Condition"/>
⑤如果符合条件显示错误,这里的条件是(没有设置属性)
<fail message="tag condition">
<condition>
<not>
<isset property=" failConditon "/>
</not>
</condition>
</fail>
=================================================================================================================
-->
<!--
=================================================================================================================
9、时间戳
①在生成环境中使用当前时间和日期,以某种方式标记某个生成任务的输出,以便记录它是何时生成的,这经常是可取的
这可能涉及编辑一个文件,以便插入一个字符串来指定日期和时间,或将这个信息合并到JAR或zip文件的文件名中
这种需要是通过简单但是非常有用的tstamp任务来解决的
这个任务通常在某次生成过程开始时调用,比如在一个init目标中。这个任务不需要属性,许多情况下只需<tstamp/>就足够了
tstamp不产生任何输出;相反,它根据当前系统时间和日期设置Ant属性
②下面是tstamp设置的一些属性、对每个属性的说明,以及这些属性可被设置到的值的例子
DSTAMP==设置为当前日期,默认格式为yyyymmdd==如20031217
TSTAMP==设置为当前时间,默认格式为hhmm======如1603
TODAY===设置为当前日期,带完整的月份========如2010年10月15日
③比如我们按该方式创建了一个JAR文件:<jar destfile="package.jar" basedir="classes"/>
在调用tstamp任务之后,我们能够根据日期命名该JAR文件:<jar destfile="package-${DSTAMP}.jar" basedir="classes"/>
因此,如果这个任务在2010年10月15日调用,则该JAR文件将被命名为package-20101015.jar
④另外,还可以配置tstamp任务来设置不同的属性,应用一个当前时间之前或之后的时间偏移,或以不同的方式格式化该字符串
所有这些都是使用一个嵌套的format元素来完成的,如下所示:
<tstamp>
<format property="OFFSET_TIME" pattern="HH:mm:ss" offset="10" unit="minute"/>
</tstamp>
上面的清单将OFFSET_TIME属性设置为距离当前时间10分钟之后的小时数、分钟数和秒数
用于定义格式字符串的字符与java.text.SimpleDateFormat类所定义的那些格式字符相同
=================================================================================================================
10、通过JDBC执行SQL语句
<sql driver="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/mydb"
userid="root"
password="root"
src="data.sql" mce_src="data.sql"/>
=================================================================================================================
11、使用SMTP服务器发送邮件
<mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build">
<from address="me@myisp.com"/>
<to address="all@xyz.com"/>
<message>The ${buildname} nightly build has completed</message>
<fileset dir="dist">
<includes name="**/*.zip"/>
</fileset>
</mail>
mailhost==SMTP服务器地址
mailport==服务器端口
subject===主题
from======发送人地址
to========接收人地址
message===发送的消息
fileset===设置附件
=================================================================================================================
-->
<!--
=================================================================================================================
12、配合JUnit实现单元测试
①把junit.jar拷入ANT_HOME/lib下,并确认在这个目录下有optional.jar,因为JUnit是Ant的扩展任务,需要引用这个扩展包
②在build.xml中加入JUnit的任务
<target name="run" depends="client">
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<classpath>
<pathelement location="client.jar" />
</classpath>
<formatter type="plain" />
<test name="com.ascenttech.HelloWorldTest" />
</junit>
</target>
=================================================================================================================
-->
<!--
=================================================================================================================
13、使用Ant调用native2ascii转换中文
在src下建立一个名为build.xml的文件,代码如下
<project>
<target name="I18N">
<native2ascii dest="com//jadyer//test"
src="com//jadyer//temp" mce_src="com//jadyer//temp"
includes="*.properties"
encoding="UTF-8">
</native2ascii>
</target>
</project>
src表示所要转换的资源文件的路径
dest表示目标路径。路径若为空则代表当前根目录
=================================================================================================================
-->