说明:本实例主要是通过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脚本