說明:本實例主要是通過ant工具發佈web項目到tomcat,生成junit測試報告,將項目打包成war包以及生成javadoc文檔
使用說明:1)實例中生成的目錄結構基本是ant推薦的方式,所以如果你是使用myeclipse開發web項目,可以直接將下面代碼用到你的項目中
2)在測試的時候需要新建test文件,用於存放測試用例
3)你需要下載junit的jar包以及數據庫的驅動包放到WEB-INF的lib目錄下
4)你使用的環境是jdk1.7+ant1.8,會出現無法生成測試報告錯誤,解決此問題的辦法是下載一個xalan包,將xalan解壓後將裏面的xalan.jar和serializer.jar
複製到jdk安裝目錄下的ext目錄中:例如C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext, 然後在運行build.xml腳本
1.準備工作
1)在windows下安裝tomcat服務器,並在系統變量下配置tomcat的安裝路徑
例如:CATALINA_HOME=D:\Program Files\Apache Software Foundation\Tomcat 7.0
2.在myeclipse下新建一個web項目
3.在web項目下新建兩個文件build.xml和build.properties,注意這兩個文件在項目的根目錄下
1)build.xml文件中的代碼如下
<?xml version="1.0"?>
<project name="util" basedir="." default="echo-test">
<!--introduction of the build.properties-->
<property file="build.properties">
</property>
<property name="build.debug" value="true">
</property>
<!--Creating a build timestamp-->
<tstamp>
<format property="buildtime" pattern="yyyy-MM-dd'T'HH:mm:ss" />
</tstamp>
<!--Output build timestamp-->
<echo message="BuildTile=${buildtime}">
</echo>
<!--Loading environment variables-->
<property environment="env">
</property>
<!--Library dependences-->
<path id="compile.classpath">
<fileset dir="${web.lib}" includes="*.jar">
</fileset>
<fileset dir="${env.CATALINA_HOME7}/lib" includes="*.jar">
</fileset>
</path>
<!--Deletes the output directories-->
<target name="clean">
<delete dir="${build.dir}">
</delete>
<delete dir="${dist.dir}">
</delete>
</target>
<!--Creates the output directories-->
<target name="init" depends="clean">
<mkdir dir="${dist.dir}" />
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${test.dir}" />
<mkdir dir="${test.classes.dir}" />
<mkdir dir="${test.data.dir}" />
<mkdir dir="${test.reports.dir}" />
<mkdir dir="${dist.doc.dir}" />
</target>
<!--Compile the source code-->
<target name="compile" depends="init">
<javac srcdir="src" destdir="${build.classes.dir}" classpathref="compile.classpath" includeantruntime="true" debug="${build.debug}">
</javac>
<echo>compilation complete!</echo>
</target>
<!--Generating documentation-->
<target name="javadoc" depends="compile">
<javadoc destdir="${dist.doc.dir}" sourcepath="src" packagenames="com.*">
</javadoc>
</target>
<target name="test-compile" depends="javadoc">
<javac srcdir="test" destdir="${test.classes.dir}" includeantruntime="true">
<classpath>
<pathelement location="${junitJar.dir}" />
<pathelement location="${build.classes.dir}" />
<pathelement path="" />
</classpath>
</javac>
</target>
<!--Create the war file-->
<target name="war" depends="test-compile">
<war destfile="${dist.dir}/${project.name}.war" basedir="${build.classes.dir}">
<fileset dir="${webRoot}" includes="**/*.*">
</fileset>
<lib dir="${web.lib}">
</lib>
<webinf dir="${web.WEB-INF}">
</webinf>
<classes dir="${build.classes.dir}">
</classes>
</war>
</target>
<target name="unit-test" depends="war">
<echo message="start run junit test" />
<junit>
<classpath>
<pathelement location="${build.classes.dir}" />
<!--bixuede-->
<pathelement location="${test.classes.dir}" />
<pathelement location="${junitJar.dir}" />
</classpath>
<formatter type="xml" />
<batchtest haltonfailure="no" todir="${test.data.dir}">
<fileset dir="test">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<!--Generating html test reports-->
<junitreport todir="${test.reports.dir}">
<fileset dir="${test.data.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${test.reports.dir}" />
</junitreport>
<echo message="end running junit test" />
</target>
<!--deploy project to tomcat -->
<target name="deploy" depends="unit-test">
<copy todir="${env.CATALINA_HOME7}/webapps">
<fileset dir="${dist.dir}" includes="${project.name}.war">
</fileset>
</copy>
</target>
<!--Create table of the project-->
<target name="create-table" depends="deploy">
<sql userid="${database.userid}" password="${database.password}" url="${database.url}" driver="${database.driver}">
<classpath refid="compile.classpath">
</classpath>
<transaction src="${sql.dir}">
</transaction>
</sql>
</target>
<!--output environment informations-->
<target name="echo-test" depends="create-table">
<property environment="env">
</property>
<echo>${env.CATALINA_HOME7}</echo>
<echo>${env.OS}</echo>
<echo>${env.ANT_HOME}</echo>
<echo>${env.JAVA_HOME}</echo>
</target>
</project>
2)build.properties中的代碼如下
dist.dir=dist
dist.doc.dir=${dist.dir}/doc
build.dir=build
build.classes.dir=${build.dir}/classes
test.dir=${build.dir}/test
test.data.dir=${test.dir}/data
test.classes.dir=${test.dir}/classes
test.reports.dir=${test.dir}/reports
war.dir=${build.dir}/war
project.name=userapp
webRoot=WebRoot
web.WEB-INF=${webRoot}/WEB-INF
web.lib=${web.WEB-INF}/lib
junitJar.dir=${web.lib}/junit4.4.jar
database.driver=com.mysql.jdbc.Driver
database.url=jdbc\:mysql\://localhost\:3306/test
database.userid=root
database.password=root
sql.dir=WebRoot/SQL/mysql.sql
build.properties的代碼說明
dist.dir=dist //存放war包的目錄
dist.doc.dir=${dist.dir}/doc //存放生javadoc的目錄
build.dir=build //構建目錄
build.classes.dir=${build.dir}/classes //存放編譯的源代碼目錄
test.dir=${build.dir}/test //存放構建測試
test.data.dir=${test.dir}/data //存放測試數據的目錄
test.classes.dir=${test.dir}/classes //存放編譯的測試代碼
test.reports.dir=${test.dir}/reports //存放生成的html報告的目錄
war.dir=${build.dir}/war //存放生的war包目錄
project.name=userapp //打包的項目名稱,可更改爲自己的項目名
webRoot=WebRoot //web項目的webroot目錄
web.WEB-INF=${webRoot}/WEB-INF
web.lib=${web.WEB-INF}/lib
junitJar.dir=${web.lib}/junit4.4.jar //編譯所需的junit jar包
database.driver=com.mysql.jdbc.Driver //數據庫驅動
database.url=jdbc\:mysql\://localhost\:3306/test //數據庫url
database.userid=root //數據庫登錄用戶id
database.password=root //數據庫密碼
sql.dir=WebRoot/SQL/mysql.sql //web項目的數據庫腳本,可以自定義修改,如果不修改則需要在webroot下新建一個SQL目錄,並在SQL下寫sql腳本