近来虽然Maven构建的项目越来越盛行,但是依然还有很多遗留(暂且称之为遗留吧)项目依然是由ANT编译的,今天老夫就整理一下自己目前公司项目用到的ANT模板,既作为老夫的学习笔记,以供将来查看,也分享出来供需要的参考,因为比较简单易懂,就不多说了,直接上代码


<?xml version="1.0" encoding="UTF-8"?>  
<!&#8211;  
Ant工程build模板  
ant build file

Example: ant -Dprofile=dev deploy  
test IDC测试环境  
dev 本地开发环境 (default)  
&#8211;>  
<project name="antProject" basedir="." default="prompt">

<property name="javac.version" value="1.6" />  
<property name="project.name" value="projectname" />  
<property name="source.encoding" value="UTF-8" />  
<property name="deploy.dir" value="/tmp/${project.name}" />

<!&#8211; project var set &#8211;>  
<property name="target.path" value="build" />  
<property name="target.root.path" value="${target.path}/ROOT" />  
<property name="target.root.classes" value="${target.root.path}/WEB-INF/classes" />

<property name="webroot.path" value="WebRoot" />  
<property name="compileSrc" value="src" />  
<property name="compileDest" value="${target.root.classes}" />  
<property name="env.dir" value="env" />  
<property name="env.cfg" value="${env.dir}/${profile}" />  
<tstamp>  
<format property="now" pattern="yyyyMMddHHmmss" />  
</tstamp>  
<path id="compileClassPath">  
<fileset dir="${webroot.path}/WEB-INF/lib">  
<include name="*.jar" />  
</fileset>  
</path>  
<target name="prompt" description="show prompt information">  
<echo>Please choose task: clean, compile, war, deploy. ${line.separator}  
Example: ${line.separator}  
ant compile ${line.separator}  
ant -Dprofile=test deploy  
</echo>  
</target>  
<target name="clean">  
<delete dir="${target.path}" />  
<mkdir dir="${target.path}" />  
<delete dir="${compileDest}" />  
<mkdir dir="${compileDest}" />  
<delete dir="${target.root.classes}" />  
<mkdir dir="${target.root.classes}" />  
<echo>cleaned for ${compileDest}, ${target.root.path},${target.path}/*.war</echo>  
</target>

<target name="compile" depends="clean,testIfProfileSet">  
<echo>starting compile the source from ${compileSrc}</echo>  
<javac srcdir="${compileSrc}" includeantruntime="false" fork="true"  
memoryinitialsize="256m" memorymaximumsize="512m" destdir="${compileDest}"  
classpathref="compileClassPath" encoding="${source.encoding}"  
source="${javac.version}" target="${javac.version}"  
debug="true" verbose="false" optimize="true" deprecation="false">  
</javac>  
<copy todir="${compileDest}">  
<fileset dir="${compileSrc}" excludes="*\*/\*.svn,*\*/\*.java" />  
</copy>  
<echo>compiled classes copied to ${compileDest}</echo>  
<copy todir="${target.root.path}">  
<fileset dir="${webroot.path}" excludes="*\*/\*.svn,*\*/\*.java" />  
</copy>  
<echo>copied to ${webroot.path} to ${compileDest}</echo>  
<echo>compile finished</echo>  
</target>  
<target name="testIfProfileSet">  
<echo>ant param profile: ${profile}</echo>  
<condition property="isProfileSet">  
<!&#8211; <isset property="${profile}" /> &#8211;>  
<or>  
<equals arg1="${profile}" arg2="dev" />  
<equals arg1="${profile}" arg2="test" />

</or>  
</condition>  
<antcall target="profileProvided">  
</antcall>  
<antcall target="noProfile">  
</antcall>  
</target>

<target name="profileProvided" if="isProfileSet">  
<copy todir="${target.root.classes}">  
<fileset dir="${env.cfg}" />  
</copy>  
<echo>copied config files ${env.cfg} to ${target.root.classes}</echo>  
</target>  
<target name="noProfile" if="isProfileSet">  
<echo>ant param profile is none, use default config files</echo>  
</target>

<target name="war" depends="compile">  
<jar jarfile="${target.path}/${project.name}-${profile}${now}.war">  
<fileset dir="${target.root.path}">  
<include name="*\*/\*" />  
<exclude name="*\*/\*.fla" />  
<exclude name="*\*/\*.mp3" />  
<exclude name="*\*/\*.as" />  
<exclude name="*\*/\*.bak" />  
<exclude name="*\*/\*.swd" />  
<exclude name="*\*/\*.db" />  
</fileset>  
</jar>  
</target>  
<target name="deploy" depends="compile">  
<delete dir="${deploy.dir}" />  
<mkdir dir="${deploy.dir}" />  
<copy todir="${deploy.dir}">  
<fileset dir="${target.root.path}">  
</fileset>  
</copy>  
<echo>deploy ${target.root.path} to ${deploy.dir}</echo>  
</target>  
</project>