Ant标签详解--基础操作
Ant的一些核心概念:
**build.xml:**构建文件是以XML 文件来描述的,默认构建文件名为build.xml。
**project:**每个构建文件包含一个工程。
<project name="HelloAnt" default="run" basedir=".">
</project>
**property:**属性,一个property 有一个名称和一个值,Property 可以用于task 的属性值,通过${}来调用,类似于EL表达式。
<property name="src" value="src" />
**depends:**每个工程包含若干个目标(target),目标可以依赖于其他的目标。
<target name="init" depends="clean">
<mkdir dir="${src}" />
</target>
**task:**目标包含一个或多个任务(task),一个task 是一段可执行的代码,"javac"就是一个典型的task。
<mkdir dir="${src}" />
下面是Ant一些常用标签的介绍,我用实例来讲解一下会更生动些:
1.<project>
<project name="HelloAnt" default="run" basedir=".">
<description>说明信息</description>
<property name="dest" value="classes" />
<target name="init" depends="clean">
...
</target>
</project>
<!--
default:表示默认的运行目标(target),这个属性是必须的
basedir:表示项目的基准目录
name:表示项目名
description:表示项目的描述,可在<project>之间添加
-->
每个构建文件都对应于一个项目,必须包含一个<project>元素,但是大型项目经常包含大量的子项目,每一个子项目都可以有自己的构建文件。
2.<target>
一个项目标签下可以有一个或多个target标签,target标签可以依赖于其他的target标签。例如,有一个target用于编译程序,另一个target用于生成可执行文件。在生成可执行文件之前必须先编译该文件,因此可执行文件的target依赖于编译程序的target。
<target name="init" depends="clean" description="初始化" id="init" if="xx" unless="xx">
Task...
</target>
<!--
name:表示名称,这个属性是必须的。
depends:表示依赖的目标。
id:id标识
description:表示目标的描述,也可在<target>之间添加<description></description>。
if:表示仅当属性设置满足时才执行。
unless:表示当属性没有设置时才执行。
-->
Ant的depends属性指定了target的执行顺序,Ant会依照depends属性中target出现顺序依次执行每个target。在执行之前,首先需要执行它所依赖的target。一个target只能被执行一次,即使有多个target依赖于它。if或unless属性用来判断是否执行该target。
3.<property>
<!-- 设置name,value键值对应属性 -->
<property name="dest" value="classes" />
<!-- 设置classpath -->
<property classpath=""/>
<!-- 设置classpath引用 -->
<property classpathref=""/>
<!-- 读取文件中的属性配置 -->
<property file="xx.properties"/>
<!-- 读取网络位置上属性文件中的属性配置 -->
<property url="http://url/xx.properties"/>
<!-- 读取属性文件中的属性配置 -->
<property resource="xx.properties"/>
<!-- 读取环境变量 -->
<property environment="env"/>
<!-- 设置路径结构 -->
<property location="/usr/local/"/>
<!-- 指向task -->
<property taskname="init"/>
property标签可以帮助我们设置一些经常用到的属性,类似于Java中的变量,这样我们可以在多个task中方便调用,下面是一个引用属性文件的实例。
假设项目根目录存在build.properties 属性文件,用于保存公共的属性,其内容为:
src = src
dest = classes
然后在build.xml 中引用此属性文件:
<property file="build.properties" />
<property name="src" value="${src} " />
<property name="dest" value="${dest} " />
我们可以通过<echo>${dest}</echo>输出相应信息测试是否载入成功。
<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloAnt" default="run" basedir=".">
<property file="build.properties" />
<property name="src" value="${src} " />
<property name="dest" value="${dest} " />
<target name="run">
<echo>${dest}</echo>
</target>
</project>
4.文件相关
(1)<mkdir>
该标签用于创建一个目录,它有一个属性dir用来指定所创建的目录名
<mkdir dir=”${dir}”/>
创建了一个目录,${dir}是前面已经声明的property属性
(2)<tempfile>
建立一个文件名称为temp.bak,后缀为.bak 的文件
<tempfile property="temp" destDir="build" suffix=".bak"/>
(3)<rename>
重命名文件/文件夹
<rename src="hello.jar" dest="helloAnt.jar"/>
将hello.jar重命名为helloAnt.jar
(4)<move>
移动文件/文件夹
<move todir="bin">
<fileset dir="${dest}">
<include name="**/*.class" />
<exclude name="**/*_temp.class" />
</fileset>
</move>
将${dest}目录下所有class文件移动到bin目录下,排除"_temp"结尾的class文件。
<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloAnt" default="run" basedir=".">
<property name="dest" value="classes" />
<target name="run">
<move todir="bin">
<fileset dir="${dest}">
<include name="**/*.class" />
<exclude name="**/*_temp.class" />
</fileset>
</move>
</target>
</project>
(5)<copy>
复制文件/文件夹
<copy file="file1" tofile="file2"/>
将file1复制为file2
<copy file="file" todir="dir"/>
将file复制到dir中
<copy todir="src2">
<fileset dir="src">
<include name="**/*.java"/>
<exclude name="**/*_tepm.java"/>
</fileset>
</copy>
或
<copy todir="src2">
<fileset dir="src" excludes="**/*_temp.java" includes="**/*.java"/>
</copy>
将src下的java文件复制到src2目录下,同时排除"_temp"结尾的java文件,两种写法均可。
<copyfile src="file1" dest="file2" forceoverwrite="true" />
<copydir src="${src}" dest="${dest}" includes="**/*.java" excludes="**/_temp.java" />
复制文件和文件夹,跟上面的功能是一样的。
<delete defaultexcludes="false">
<fileset dir="src" includes="**/*.svn"/>
</delete>
通常情况下,svn 文件默认是 excludes 的,所以这里要设置一下
<deltree dir="src"/>
删除src目录树,即所有子文件夹内容
(6)<delete>
删除一个文件或一组文件
<delete file="file1"/>
删除file1文件
<delete dir="dir1"/>
删除dir1目录
<delete>
<fileset dir="dir1" includes="**/*.bak"/>
</delete>
删除dir1下所有bak文件
<delete includeEmptyDirs="true">
<fileset dir="dir1"/>
</delete>
或
<delete includeEmptyDirs="true">
<fileset dir="dir1" includes="**/*"/>
</delete>
删除dir1下所有文件
(7)<touch>
主要是用于修改最后修改时间
<touch file="file1"/>
如果文件不存在,先创建文件,更改最后修改时间为当前系统时间
<touch file="myfile" datetime="11/11/2011 11:11 pm"/>
如果文件不存在,先创建文件,更改最后修改时间为11/11/2011 11:11 pm
<touch datetime="11/11/2011 11:11 pm">
<fileset dir="src" />
</touch>
更改文件夹最后修改时间,文件夹需存在
5.输出信息
(1)<echo>
用于打印/输出信息,类似于log4j的info方法。
<echo message="hello!">
<echo message="${msg}">
<echo>${msg}</echo>
<echo>hello!</echo>
以上四种方式均可以显示相应信息
<echo file="test.txt" message="hello Ant!"/>
将"hello Ant!"写入test.txt文件中
<echoxml file="test.xml">
<test></test>
</echoxml>
将"<test></test>"这段xml代码写入test.xml文件中
<echoproperties destfile="test.properties">
<propertyset>
<propertyref prefix="java." />
</propertyset>
</echoproperties>
将所有"java."开头的属性写入到test.properties属性文件中
<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloAnt" default="run" basedir=".">
<target name="run">
<echo file="test.txt" message="hello Ant!" />
<echoproperties destfile="test.properties">
<propertyset>
<propertyref prefix="java." />
</propertyset>
</echoproperties>
<echoxml file="test.xml">
<test>
</test>
</echoxml>
</target>
</project>
(2)<fail>
用于显示错误信息,类似于log4j的error方法。
<fail message="${err}" />
<fail message="this is an error" />
<fail>${err}</fail>
<fail>this is an error</fail>
与echo类似,fail提供四种基本的错误输出方式,当然fail也可以加入类似if,unless等业务逻辑进行判断是否显示。
6.流程,判断
(1)<condition>
包括<and>,<or>,<not>等标签。
<condition property="isMacOsButNotMacOsX">
<and>
<os family="mac"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>
(2)<available>
available 主要用于判断某个文件是否存在,并设置相应属性值。
1)如果类存在,则设置属性classExisted 为true。
<available classname="com.test.Myclass" property="classExisted"/>
2)如果文件存在则设置属性myJar 为true。
<property name="myJar" value="./lib/myJar.jar"/>
<available file="${myJar}" property="jarExisted"/>
3)如果目录存在,则设置属性为true。
<available file="/home/g21121" type="dir" property="g21121Existed"/>
4)如果在classpath 下发现文件则设置属性为true
<available property="have.extras" resource="extratasks.properties">
<classpath>
<pathelement location="/usr/local/ant/extra.jar" />
</classpath>
</available>
(3)if ... else
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="test" default="run">
<property name="db.type" value="oracle" />
<import file="properties.xml" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}" />
<target name="run">
<if>
<equals "${db.type}" arg2="mysql" />
<then>
<echo message="The db is mysql" />
</then>
<else>
<echo message="the db is oralce" />
</else>
</if>
</target>
</project>
7.其他标签
(1)<classPath>和<path>
比较好理解,设置classPath,引用path内容。
<classpath>
<pathelement path="${classPath}"/>
<pathelement location="lib/ant.jar"/>
</classpath>
<path id="classPath">
<pathelement location="${dest}"/>
</path>
(2)<replace>
替换指定内容,value1替换成value2。
<replace file="configure.sh" value="defaultvalue" propertyFile="source/name.properties">
<replacefilter token="value1"/>
<replacefilter token="value1" value="value2"/>
<replacefilter token="value1" property="property.key"/>
</replace>
(3)<exec>
执行系统程序,例如:cmd,svn,cvs等等。
<!-- 启动tomcat -->
<exec executable="./${tomcat.home}/bin/startup.sh" failonerror="false"></exec>
<!-- 打开cmd执行abc.bat -->
<exec executable="cmd">
<arg value="abc.bat"/>
</exec>
(4)执行SQL
<sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" userid="root" password="123456" encoding="utf-8" src="user.sql" >
<classpath refid="classpath" />
insert into user values(xxx,xxx,xxx,xxx);
</sql>
既可以在sql标签内容声明SQL语句,也可以通过src加载sql文件。
(5)发送电子邮件
不得不佩服Ant功能的强大,居然连电子邮件功能都集成了,这样在部署或部署失败之后可以向指定地址报告结果。
<mail mailhost="smtp.gmail.com" mailport="1025" subject="test">
<from address="g21121@gmail.com" />
<replyto address="g21121@gmail.com" />
<to address="g21121@gmail.com" />
<message>hello Ant! --from ant mail</message>
<attachments>
<fileset dir="dist">
<include name="**/*.zip" />
</fileset>
</attachments>
</mail>
(6)载入其他构件文件
<ant antfile="dir1/dir2/build.xml" />
<ant antfile="dir2/build.xml" dir="dir1" />
<ant antfile="build.xml" dir="dir1/dir2" />
调用指定文件中的制定target:
<ant antfile="dir/build.xml" target="compile"/>
(7)压缩,解压缩
压缩,解压缩包括jar,war,ear,zip,tar等等常见压缩包格式。
zip压缩:
<zip destfile="${dist}/test.zip" basedir="${dest}" includes="**/**/*.html" excludes="**/*_temp.html" />
zip解压缩:
<unzip src="${dist}/test.zip" dest="${dest}">
<patternset>
<include name="/lib/ant.jar" />
</patternset>
<mapper type="flatten" />
</unzip>
upnzip也可以解压缩war包。
tar压缩:
<tar destfile="${dist}/test.tar">
<tarfileset dir="${src}" fullpath="/usr/local/src/" preserveLeadingSlashes="true">
<include name="*.java" />
</tarfileset>
</tar>
tar解压缩:
<untar src="${dist}/test.tar" dest="${dest}" />
EAR压缩:
全称是EnterpriseArchiveFile,为J2EE应用程序创建的JAR文件是EAR文件(企业JAR文件)。
包含内容:Servlet、JSP页面、JSP标记库、JAR库文件、HTML/XML文档和其他公用资源文件,如图片、音频文件等;除了包含JAR、WAR以外,还包括EJB组件.
<ear destfile="${dist}/test.ear" appxml="resource/application.xml">
<fileset dir="${dist}" includes="*.jar,*.war" />
</ear>
(8)获取当前时间:
<!-- 获取当前时间 -->
<tstamp>
<format property="backupTime" pattern="yyyyMMddHHmmss" locale="zh"/>
</tstamp>
类似于Java的用法,这里我就不讲了。
Ant的命令还有很多,这里只是讲了一部分,Ant的知识还需要不断的实践去获取与验证。