安装Ant:
假设安装在d盘:D:\apache-ant-1.8.4 ,直接解压就行。
设置ANT_HOME系统变量和PATH系统变量:
路径设置代码
- set ANT_HOME= D:\apache-ant-1.8.4
- set PATH=%ANT_HOME%\bin
调整ant的运行时最大占用内存:
在ant.bat文件的REM only runs on WinNT下,加上一句:
set ANT_OPTS=-Xmx1024m
指定执行ant的文件
ant -f build.xml
指定target
<?xml version="1.0" encoding="UTF-8"?> <project name='blizzard' default="main" basedir="."> <property environment="env"/> <property name="subproject.target" value="main"/> <target name="main" depends="game-common,gate,game" description="mini build"> <tstamp> <format property="build.datetime" pattern="MM/dd/yyyy hh:mm:ss aa" /> </tstamp> <echo>ant main target completed on ${build.datetime} </echo> </target> <target name="game-common" description="build the game-common"> <ant inheritAll="false" antfile="${basedir}/game-common/build.xml" target="${subproject.target}"/> </target> <target name="game" description="build the game"> <ant inheritAll="false" antfile="${basedir}/game/build.xml" target="${subproject.target}"/> </target> <target name="gate" description="build the gate"> <ant inheritAll="false" antfile="${basedir}/gate/build.xml" target="${subproject.target}"/> </target> <target name="start" description="start all the services"> <ant inheritAll="false" antfile="${basedir}/gate/build.xml" target="start" /> <ant inheritAll="false" antfile="${basedir}/game/build.xml" target="start" /> </target> <target name="stop" description="stop all the services"> <ant inheritAll="false" antfile="${basedir}/gate/build.xml" target="stop" /> <ant inheritAll="false" antfile="${basedir}/game/build.xml" target="stop" /> </target> </project>
ant game
ant game-common game start
编译web工程时,会提示找不到三个包,最好这些JAR放到ant的lib目录下
apache-ant-1.9.2\lib
jsp-api.jar"
servlet-api.jar
el-api.jar"
这些在tomcat的lib里能找到
如何提取版本信息?
<target name="version" description="Use SVN to get the current revision" unless="disable-svnCheck"> <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <property file="${basedir}/version.properties" /> <tstamp> <format property="versionTime" pattern="yyyyMMdd" /> </tstamp> <if> <equals arg1="${date}" arg2="${versionTime}"/> <then> <math result="packageTimes" operand1="${release}" operation="+" operand2="1" datatype="int"/> <propertyfile file="${basedir}/version.properties" comment="version properties file"> <entry key="date" type="date" value="now" pattern="yyyyMMdd"/> <entry key="release" type="int" value="1" operation="+"/> </propertyfile> </then> <else> <property name="packageTimes" value="1" /> <propertyfile file="${basedir}/version.properties" comment="version properties file"> <entry key="date" type="date" value="now" pattern="yyyyMMdd"/> <entry key="release" type="int" value="1" operation="="/> </propertyfile> </else> </if> <exec executable="svn" logerror="true" outputproperty="svn.exec.result" failonerror="true" failifexecutionfails="true"> <arg line="up" /> </exec> <propertyregex property="svn.revision" input="${svn.exec.result}" regexp="(\d+)" select="\1" casesensitive="false" /> <propertyregex property="branch" input="${basedir}" regexp="([^\${file.separator}]+$)" select="\1" casesensitive="false" /> <echo level="info" message="svn.revision=${svn.revision},branch=${branch},version=S${versionTime}R${packageTimes}" /> </target>
如何使用复杂的逻辑和运算
Ant-contrib 为 Ant 提供了与通常所使用的编程语言功能相同的 <if> 、 <for>、<switch>等逻辑判断任务,支持对字符串的排序<sortlist>任务,甚至还支持常见的数学运算,如加、减、乘、除、求余等功能。在构建过程中灵活运用这两个扩展包,将大大增强 Ant 的可编程性,这其实就是一种基于 XML 脚本的编程。