<?xml version="1.0"?>
<project name="table" basedir="." default="help">
<!--指定了连接tomcat的一些属性,由外部文件导入-->
<property file="build.properties"/>
<!--指定源程序的作者-->
<property name="anthor" value="rikgong"/>
<!--指定源程序的版本-->
<property name="version" value="1.0"/>
<!--指定源程序的目录-->
<property name="src.dir" value="src"/>
<!--指定了整个工程的classes的目录-->
<property name="classes.dir" value="classes"/>
<!--指定了工程的基目录-->
<property name="web.dir" value="web"/>
<!--指定了目的目录-->
<property name="dist.dir" value="dist"/>
<!--指定了数据库操作的目录-->
<property name="data.dir" value="data"/>
<!--所有的编译后的classes存放的文件夹-->
<property name="build.dir" value="build"/>
<!--指定了测试文件编译后所在的文件夹-->
<property name="test.dir" value="test"/>
<!--指定了测试文件所在的目录-->
<property name="test.src" value="test"/>
<!--指定需要生成文档的包的名称!(需要修改)##############-->
<!--源文件包1-->
<property name="src_package" value="table"/>
<!--指定最后生成的文档所在的目录-->
<property name="doc.dir" value="doc"/>
<!--指定程序的名称,需要修改####################-->
<property name="webapp.name" value="table"/>
<!--指定所需要的外部环境参数-->
<property environment="env"/>
<property name="tomcat.home" value="${env.CATALINA_HOME}"/>
<!--设置好编译的类库路径-->
<path id="classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="t*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<!--帮助-->
<target name="help" >
<echo message=""/>
<echo message="${webapp.name} build file"/>
<echo message="-----------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="-------the project operation ----"/>
<echo message="compile --> Compile all Java files"/>
<echo message="javadoc ---->make the documents"/>
<echo message="deploy --> Deploy application as directory"/>
<echo message="deploywar --> Deploy application as a WAR file"/>
<echo message="clean ---->clean the project"/>
<echo message=""/>
<echo message=" ------the archive operation ------ "/>
<echo message="war --> Package as WAR file"/>
<echo message="zip --> Package as ZIP file(include the whole project)"/>
<echo message="jar --> Package as JAR file(include the classes files)"/>
<echo message=""/>
<echo message=" ------the Test operation ------ "/>
<echo message="test --> run the junit test"/>
<echo message="report --> report the test as html files"/>
<echo message=""/>
<echo message="------------the tomcat operation--------"/>
<echo message="install --> Install application in Tomcat"/>
<echo message="remove --> Remove application from Tomcat"/>
<echo message="reload --> Reload application in Tomcat"/>
<echo message="start --> Start Tomcat application"/>
<echo message="stop --> Stop Tomcat application"/>
<echo message="list --> List Tomcat applications"/>
<echo message=""/>
</target>
<!--###################以下是工程的常见操作###########################-->
<!--编译-->
<target name="compile" description="compiler the files ">
<mkdir dir="${build.dir}/classes">
</mkdir>
<javac destdir="${build.dir}/classes" srcdir="${src.dir}" debug="true"
deprecation="false" failοnerrοr="true" optimize="false">
<sourcepath path="${src.dir}">
</sourcepath>
<classpath refid="classpath">
</classpath>
</javac>
<mkdir dir="${build.dir}/${test.dir}/classes">
</mkdir>
<javac destdir="${build.dir}/${test.dir}/classes" deprecation="false" debug="true"
optimize="false" failοnerrοr="true"
>
<src path="${test.src}">
</src>
<sourcepath path="${test.dir}">
</sourcepath>
<classpath >
<path location="${build.dir}/classes">
</path>
<path refid="classpath">
</path>
</classpath>
</javac>
<copy todir="${build.dir}/">
<fileset dir="${web.dir}/WEB-INF/" includes="**.xml" excludes="web.xml">
</fileset>
<fileset dir="${web.dir}/WEB-INF/classes/" includes="**.xml" excludes="web.xml">
</fileset>
</copy>
<copy todir="${web.dir}/WEB-INF/classes/">
<fileset dir="${build.dir}/classes" includes="**">
</fileset>
</copy>
<copy todir="${web.dir}/WEB-INF/classes/">
<fileset dir="${build.dir}/${test.dir}/classes" includes="**">
</fileset>
</copy>
</target>
<!--make the document-->
<target name="javadoc" description="make the document">
<javadoc author="true"
bottom="<i>Copyright@ www.norteck.com Inc. All Rights Reserved----rikgong@163.com.</i>"
destdir="${doc.dir}"
doctitle="<font color=#ff00000>${appname} API</font>"
packagenames="${src_package}.*"
sourcepath="${src.dir}"
use="true"
version="true"
header="版本1.0"
windowtitle="${webapp.name}--API(By rikgong Email:rikgong@163.com)">
<classpath refid="classpath"/>
</javadoc>
</target>
<!--发布应用程序-->
<target name="deploy" depends="compile" description="deploy the application">
<copy todir="${tomcat.home}/webapps/${webapp.name}" preservelastmodified="true">
<fileset dir="${web.dir}">
<include name="**/*.*"> </include>
<exclude name="**/junit.jar"></exclude>
<exclude name="**/*mock.jar"> </exclude>
<exclude name="**/strutstestcase*.jar"></exclude>
</fileset>
</copy>
<copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes" preservelastmodified="true">
<fileset dir="${build.dir}/classes"></fileset>
</copy>
</target>
<!--发布war 包 -->
<target name="deploywar" depends="war" description="deoloy the war">
<copy todir="${tomcat.home}/webapps" preservelastmodified="true">
<fileset dir="${dist.dir}">
<include name="*.war">
</include>
</fileset>
</copy>
</target>
<!--clean tbe project-->
<target name="clean" description="clean the application">
<delete dir="${build.dir}"> </delete>
<delete dir="${dist.dir}"> </delete>
<delete dir="${doc.dir}"></delete>
<antcall target="SQL"> </antcall>
<delete dir="${data.dir}"> </delete>
</target>
<!--################ 以下是各种打包操作########################-->
<!--war 打包操作,用于部署至服务器-->
<target name="war" depends="compile" description="war the application">
<mkdir dir="${dist.dir}">
</mkdir>
<war destfile="${dist.dir}/${webapp.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<classes dir="${build.dir}/classes">
</classes>
<fileset dir="${web.dir}">
<include name="**/*.*">
</include>
<exclude name="**/web.xml">
</exclude>
<exclude name="**/junit.jar">
</exclude>
<exclude name="**/*mock.jar">
</exclude>
<exclude name="**/strutstestcase*.jar">
</exclude>
</fileset>
</war>
</target>
<!--jar 打包 用于备份文件-->
<target name="jar" depends="compile" description="jar the application,so you an backup the aplication">
<mkdir dir="${dist.dir}">
</mkdir>
<!--生成时间戳-->
<tstamp></tstamp>
<jar destfile="${dist.dir}/${webapp.name}_${DSTAMP}.jar" >
<fileset dir="${web.dir}/WEB-INF/" >
<include name="**/*.jar"> </include>
<include name="**/*.class"> </include>
</fileset>
<manifest>
<attribute name="author" value="${anthor}"/>
<attribute name="version" value="${version}"/>
<attribute name="time" value="${DSTAMP}"/>
</manifest>
</jar>
</target>
<!--ZIP打包(备份整个工程)-->
<target name="zip" description="zip the project as a whole">
<mkdir dir="${dist.dir}">
</mkdir>
<!--生成时间戳-->
<tstamp></tstamp>
<zip destfile="${dist.dir}/${webapp.name}_${DSTAMP}.zip" >
<zipfileset dir="${basedir}">
<include name="${test.dir}/*"></include>
<include name="${src.dir}/**"></include>
<include name="${web.dir}/**"></include>
<include name="*.*"> </include>
<include name="${web.dir}/WEB-INF/lib"></include>
<exclude name="${web.dir}/WEB-INF/lib/*.*"></exclude>
</zipfileset>
</zip>
</target>
<!--###############以下是测试常见操作 测试与发布 ####################-->
<target name="test" depends="compile">
<available classname="junit.framework.TestCase" property="junit.present">
</available>
<fail unless="junit.present" message="Please copy junit to ant's lib">
</fail>
<mkdir dir="${build.dir}/${test.dir}/data/xml">
</mkdir>
<junit printsummary="no" fork="yes" haltonfailure="yes" errorproperty="test.failed" >
<classpath>
<path refid="classpath">
</path>
<path location="${build.dir}/classes">
</path>
<path location="web/WEB-INF/classes">
</path>
<path location="web">
</path>
</classpath>
<formatter type="xml">
</formatter>
<formatter type="brief" usefile="false">
</formatter>
<batchtest todir="${build.dir}/${test.dir}/data/xml" if="testcase">
<fileset dir="${build.dir}/${test.dir}/classes">
<include name="**/*${testcase}*">
</include>
</fileset>
</batchtest>
<batchtest todir="${build.dir}/${test.dir}/data/xml" unless="testcase">
<fileset dir="${build.dir}/${test.dir}/classes">
<include name="**/*Test*.class*"/>
</fileset>
</batchtest>
</junit>
<fail if="test.failed">
Unit tests failed. For error messages, check the log files in
${test.dir}/data or run "ant test-reports"
to generate reports at ${test.dir}/reports.
</fail>
</target>
<!--生成报告 以html 格式-->
<target name="report" description="report the Test result as html">
<mkdir dir="${build.dir}/${test.dir}/reports">
</mkdir>
<junitreport >
<fileset dir="${build.dir}/${test.dir}/data/xml">
<include name="TEST-*.xml"> </include>
</fileset>
<report format="frames" todir="${build.dir}/${test.dir}/reports"></report>
</junitreport>
</target>
<!--###################以下是Tomcat Ant Tasks 无需修改,通用###########################-->
<taskdef file="tomcatTasks.properties">
<classpath>
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
</classpath>
</taskdef>
<!--install -->
<target name="install" description="Install application in Tomcat"
depends="war">
<deploy
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"
war="file:${dist.dir}/${webapp.name}.war"/>
</target>
<!--remove -->
<target name="remove" description="Remove application from Tomcat">
<undeploy
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<!--reload -->
<target name="reload" description="Reload application in Tomcat">
<reload
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<!--start -->
<target name="start" description="Start Tomcat application">
<start
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<!--stop -->
<target name="stop" description="Stop Tomcat application">
<stop
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"
path="/${webapp.name}"/>
</target>
<!--list -->
<target name="list" description="List Tomcat applications">
<list
url="${tomcat.manager.url}"
username="${tomcat.manager.username}"
password="${tomcat.manager.password}"/>
</target>
<!--###################以下是SQl 的常用操作 需要引入外部的sql 文件###########################-->
<target name="SQL">
<mkdir dir="${data.dir}">
</mkdir>
<copy file="SC.sql" todir="${data.dir}">
</copy>
<sql
driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SC"
userid="rikgong"
password="rikgong"
>
<classpath refid="classpath">
</classpath>
delete student from Student ;
delete classes from Classes ;
delete sc from SC;
insert into Student(StudentID,name,version) values(1,'student1',1);
insert into Classes values (1,'courses1',1);
insert into SC values(1,1);
</sql>
</target>
</project>
build.properties文件部分
# Ant properties for building equinoxwebapp.version=ch2
tomcat.manager.url=http://localhost:80/manager
tomcat.manager.username=admin
tomcat.manager.password=
tomcatTasks.properties文件部分
deploy=org.apache.catalina.ant.DeployTask
undeploy=org.apache.catalina.ant.UndeployTask
remove=org.apache.catalina.ant.RemoveTask
reload=org.apache.catalina.ant.ReloadTask
start=org.apache.catalina.ant.StartTask
stop=org.apache.catalina.ant.StopTask
list=org.apache.catalina.ant.ListTask