Ant 使用小结
以前的项目中都没有用到过ant,都是手动打war,复制copy。最近的项目中ant 配置一堆 一堆的,在做的时候只是处于能看懂的状况。昨天写了个小的例子,mark一下。
完成的功能:编译,打jar包,打war包,连同开发jdk,tomcat 部署
项目目录

bat脚本:
@echo off
setlocal
set PROJECT_HOME=%~dp0%..
set ANT_HOME=%PROJECT_HOME%\lib\ant
set JAVA_HOME=%PROJECT_HOME%\lib\sun\jdk1.6.0_35
set args=%*
set PATH=%ANT_HOME%\bin;%PATH%
call %ANT_HOME%\bin\ant.bat %args%
exit /b %ERRORLEVEL%
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="init" name="Project testApp" basedir=".">
<echo message="Using Java ${java.version}" />
<property name="codeProject" location="../testApp" />
<property name="lib" location="../lib" />
<property name="deploy" location="../deploy" />
<property name="srcDir" location="${codeProject}/src" />
<property name="buildDir" location="../work/build" />
<property name="distDir" location="../work/dist" />
<property name="projectName" value="TestApp" />
<path id="project.classpath">
<fileset dir="${lib}/sun/jdk1.6.0_35/jre/lib">
<include name="/*.jar" />
</fileset>
<fileset dir="${lib}/tomcat/lib">
<include name="/*.jar" />
</fileset>
<fileset dir="${codeProject}/WebContent/WEB-INF/lib">
<include name="/*.jar" />
</fileset>
</path>
<target name="init" description="Initializ">
<tstamp />
<mkdir dir="${buildDir}" />
<mkdir dir="${distDir}" />
<mkdir dir="${deploy}" />
<echo message="init" />
</target>
<target name="compile" depends="init">
<javac srcdir="${srcDir}" destdir="${buildDir}"
includeantruntime="false">
<classpath refid="project.classpath" />
</javac>
<echo message="compile" />
</target>
<target name="dist" depends="compile">
<jar destfile="${distDir}/${projectName}-${DSTAMP}.jar" basedir="${buildDir}">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Main-Class" value="package.Main" />
</manifest>
</jar>
<jar destfile="${distDir}/${projectName}-src-${DSTAMP}.jar"
basedir="${srcDir}" />
</target>
<target name="build" depends="dist">
<copy todir="${deploy}/sun">
<fileset dir="${lib}/sun" includes="**/*" />
</copy>
<copy todir="${deploy}/tomcat">
<fileset dir="${lib}/tomcat" includes="**/*" />
</copy>
<copy todir="${distDir}">
<fileset dir="${codeProject}/WebContent/WEB-INF/lib" includes="**/*" />
</copy>
<war warfile="${deploy}/tomcat/webapps/${projectName}.war" webxml="${codeProject}/WebContent/WEB-INF/web.xml">
<lib dir="${distDir}"/>
<fileset dir="${codeProject}/WebContent"/>
</war>
<copy file="./run.bat" tofile="${deploy}/run.bat"/>
</target>
<target name="clean">
<echo message="clean" />
<delete dir="${buildDir}" />
<delete dir="${distDir}" />
<delete dir="${deploy}" />
</target>
</project>
run tomcat bat
set DEPLOY_HOME=%~dp0%
set JAVA_HOME=%DEPLOY_HOME%\sun\jdk1.6.0_35
set PATH=%JAVA_HOME%\bin;%PATH%
set TOMCAT_HOME=%DEPLOY_HOME%\tomcat
set CATALINA_HOME=%TOMCAT_HOME%
call %TOMCAT_HOME%\bin\startup.bat
exit /b %ERRORLEVEL%