build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="test3" default="" basedir=".">
<!--${ant.project.name}-->
<!-- 链接库目录 -->
<property name="lib.dir" value="${basedir}/lib" />
<!--工程目录-->
<property name="src" value="${basedir}/src" />
<property name="test" value="${basedir}/test" />
<property name="function" value="${basedir}/function" />
<!-- 工程编译目录 -->
<property name="build.dir" value="${basedir}/build" />
<property name="desc.dir" value="${build.dir}/desc" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="function.dir" value="${build.dir}/function" />
<!-- Tomcat 目录 -->
<property name="tomcat.dir" value="/opt/javaworkspace/tomcat" />
<property name="tomcatlib.dir" value="${tomcat.dir}/lib" />
<!-- Selenium 目录-->
<property name="selenium.dir" value="/opt/java/selenium/selenium-server-1.0-beta-1" />
<!-- 测试编译目录 -->
<property name="test.dir" value="${build.dir}/test" />
<property name="function.dir" value="${build.dir}/function" />
<property name="report.dir" value="${build.dir}/Report" />
<property name="junitreport.dir" value="${report.dir}/JunitReport" />
<property name="finalreport.dir" value="${report.dir}/FinalReport" />
<!-- 数据库 -->
<property file="${src}/datasource.properties" />
<property name="changelog" value="${build.dir}/database.changelog.xml" />
<!-- 链接库(jar;class文件) -->
<path id="base.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${tomcatlib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${src}">
<include name="**/*.xml" />
</fileset>
</path>
<!-- ant-contrib.jar -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${tomcatlib.dir}/ant-contrib-0.6.jar" />
</classpath>
</taskdef>
<!-- Clean 编译的文件-需要修改-->
<target name="clean" depends="tomcat.stop">
<delete dir="${build.dir}" />
<delete dir="${basedir}/classes">
</delete>
<delete>
<fileset dir="${tomcat.dir}/webapps">
<include name="${ant.project.name}.war" />
</fileset>
</delete>
<delete dir="${tomcat.dir}/webapps/${ant.project.name}" />
<delete dir="${tomcat.dir}/work/Catalina/localhost/${ant.project.name}" />
</target>
<!-- 生成编译文件目录 --需要改-->
<target name="init" depends="clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${report.dir}" />
<mkdir dir="${test.dir}" />
<mkdir dir="${function.dir}" />
<mkdir dir="${desc.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${report.dir}" />
<mkdir dir="${junitreport.dir}" />
<mkdir dir="${finalreport.dir}" />
<copy todir="${desc.dir}">
<fileset dir="${basedir}/WebRoot" />
</copy>
<copy todir="${classes.dir}">
<fileset dir="${basedir}/src">
<include name="*.xml" />
<include name="*.properties" />
<exclude name="*.jar" />
</fileset>
</copy>
</target>
<!-- 编译src -->
<target name="compile-src" depends="init">
<javac srcdir="${src}" destdir="${classes.dir}">
<classpath refid="base.classpath" />
</javac>
<copy todir="${classes.dir}/com/goodhope/pojo">
<fileset dir="${basedir}/src/com/goodhope/pojo">
<include name="*.xml" />
</fileset>
</copy>
</target>
<!-- 编译test -->
<target name="compile-test" depends="compile-src">
<javac srcdir="${test}" destdir="${test.dir}">
<classpath path="${classes.dir}" />
<classpath refid="base.classpath" />
</javac>
</target>
<!-- 编译function -->
<target name="compile-function" depends="compile-src,compile-test">
<javac srcdir="${function}" destdir="${function.dir}">
<classpath path="${classes.dir}" />
<classpath refid="base.classpath" />
</javac>
</target>
<!-- 单元测试-生成单元测试报告 -->
<target name="junit-test" depends="compile-function">
<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
<classpath>
<pathelement location="${test.dir}" />
<pathelement location="${classes.dir}" />
<path refid="base.classpath" />
</classpath>
<formatter type="xml" />
<batchtest todir="${junitreport.dir}" fork="true">
<fileset dir="${test.dir}">
<include name="**/*Test.*" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${junitreport.dir}">
<fileset dir="${junitreport.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${junitreport.dir}" />
</junitreport>
<fail if="tests.failed">
</fail>
</target>
<!--生成 War 包 -->
<target name="war" depends="junit-test">
<war warfile="${build.dir}/${ant.project.name}.war" webxml="${desc.dir}/WEB-INF/web.xml">
<lib dir="${basedir}/lib" />
<classes dir="${classes.dir}" />
<fileset dir="${desc.dir}">
</fileset>
</war>
</target>
<!-- 部署 War 到 Tomcat -->
<target name="deploy" depends="war">
<copy todir="${tomcat.dir}/webapps">
<fileset dir="${build.dir}">
<include name="${ant.project.name}.war" />
</fileset>
</copy>
</target>
<!--启动 Tomcat -->
<target name="tomcat.start">
<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.start" inheritall="false" />
</target>
<!--停止 Tomcat -->
<target name="tomcat.stop">
<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.stop" inheritall="false" />
</target>
<!--启动 Selenium 服务器 -->
<target name="selenium.start">
<if>
<not>
<socket server="localhost" port="4444" />
</not>
<then>
<java jar="${selenium.dir}/selenium-server.jar" fork="true" spawn="true" />
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="4444" />
</and>
</waitfor>
</then>
</if>
</target>
<!-- 停止 Selenium 服务器 -->
<target name="selenium.stop">
<if>
<socket server="localhost" port="4444" />
<then>
<get taskname="selenium-shutdown" dest="${functionTest.report.dir}/selenium-result.txt" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" ignoreerrors="true" />
</then>
</if>
</target>
<!-- test-all -->
<target name="function-test" depends="deploy">
<!-- 并行容器节点,一边同时打开tomcat 和selenium server,一边等待两者打开后执行JUnit测试。
如果不使用并行节点,而是用spawn=yes属性后台启动tomcat,则屏幕里看不到tomcat信息,如果测试意外终止的话,也不能关闭tomcat -->
<parallel>
<antcall target="tomcat.start" />
<antcall target="selenium.start" />
<sequential>
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="8099" />
</and>
</waitfor >
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="4444" />
</and>
</waitfor >
<!-- Junit Test All -->
<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
<classpath>
<pathelement path="${function.dir}" />
<path refid="base.classpath" />
</classpath>
<formatter type="xml" />
...
<project name="test3" default="" basedir=".">
<!--${ant.project.name}-->
<!-- 链接库目录 -->
<property name="lib.dir" value="${basedir}/lib" />
<!--工程目录-->
<property name="src" value="${basedir}/src" />
<property name="test" value="${basedir}/test" />
<property name="function" value="${basedir}/function" />
<!-- 工程编译目录 -->
<property name="build.dir" value="${basedir}/build" />
<property name="desc.dir" value="${build.dir}/desc" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="function.dir" value="${build.dir}/function" />
<!-- Tomcat 目录 -->
<property name="tomcat.dir" value="/opt/javaworkspace/tomcat" />
<property name="tomcatlib.dir" value="${tomcat.dir}/lib" />
<!-- Selenium 目录-->
<property name="selenium.dir" value="/opt/java/selenium/selenium-server-1.0-beta-1" />
<!-- 测试编译目录 -->
<property name="test.dir" value="${build.dir}/test" />
<property name="function.dir" value="${build.dir}/function" />
<property name="report.dir" value="${build.dir}/Report" />
<property name="junitreport.dir" value="${report.dir}/JunitReport" />
<property name="finalreport.dir" value="${report.dir}/FinalReport" />
<!-- 数据库 -->
<property file="${src}/datasource.properties" />
<property name="changelog" value="${build.dir}/database.changelog.xml" />
<!-- 链接库(jar;class文件) -->
<path id="base.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${tomcatlib.dir}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${src}">
<include name="**/*.xml" />
</fileset>
</path>
<!-- ant-contrib.jar -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${tomcatlib.dir}/ant-contrib-0.6.jar" />
</classpath>
</taskdef>
<!-- Clean 编译的文件-需要修改-->
<target name="clean" depends="tomcat.stop">
<delete dir="${build.dir}" />
<delete dir="${basedir}/classes">
</delete>
<delete>
<fileset dir="${tomcat.dir}/webapps">
<include name="${ant.project.name}.war" />
</fileset>
</delete>
<delete dir="${tomcat.dir}/webapps/${ant.project.name}" />
<delete dir="${tomcat.dir}/work/Catalina/localhost/${ant.project.name}" />
</target>
<!-- 生成编译文件目录 --需要改-->
<target name="init" depends="clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${report.dir}" />
<mkdir dir="${test.dir}" />
<mkdir dir="${function.dir}" />
<mkdir dir="${desc.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${report.dir}" />
<mkdir dir="${junitreport.dir}" />
<mkdir dir="${finalreport.dir}" />
<copy todir="${desc.dir}">
<fileset dir="${basedir}/WebRoot" />
</copy>
<copy todir="${classes.dir}">
<fileset dir="${basedir}/src">
<include name="*.xml" />
<include name="*.properties" />
<exclude name="*.jar" />
</fileset>
</copy>
</target>
<!-- 编译src -->
<target name="compile-src" depends="init">
<javac srcdir="${src}" destdir="${classes.dir}">
<classpath refid="base.classpath" />
</javac>
<copy todir="${classes.dir}/com/goodhope/pojo">
<fileset dir="${basedir}/src/com/goodhope/pojo">
<include name="*.xml" />
</fileset>
</copy>
</target>
<!-- 编译test -->
<target name="compile-test" depends="compile-src">
<javac srcdir="${test}" destdir="${test.dir}">
<classpath path="${classes.dir}" />
<classpath refid="base.classpath" />
</javac>
</target>
<!-- 编译function -->
<target name="compile-function" depends="compile-src,compile-test">
<javac srcdir="${function}" destdir="${function.dir}">
<classpath path="${classes.dir}" />
<classpath refid="base.classpath" />
</javac>
</target>
<!-- 单元测试-生成单元测试报告 -->
<target name="junit-test" depends="compile-function">
<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
<classpath>
<pathelement location="${test.dir}" />
<pathelement location="${classes.dir}" />
<path refid="base.classpath" />
</classpath>
<formatter type="xml" />
<batchtest todir="${junitreport.dir}" fork="true">
<fileset dir="${test.dir}">
<include name="**/*Test.*" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${junitreport.dir}">
<fileset dir="${junitreport.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${junitreport.dir}" />
</junitreport>
<fail if="tests.failed">
</fail>
</target>
<!--生成 War 包 -->
<target name="war" depends="junit-test">
<war warfile="${build.dir}/${ant.project.name}.war" webxml="${desc.dir}/WEB-INF/web.xml">
<lib dir="${basedir}/lib" />
<classes dir="${classes.dir}" />
<fileset dir="${desc.dir}">
</fileset>
</war>
</target>
<!-- 部署 War 到 Tomcat -->
<target name="deploy" depends="war">
<copy todir="${tomcat.dir}/webapps">
<fileset dir="${build.dir}">
<include name="${ant.project.name}.war" />
</fileset>
</copy>
</target>
<!--启动 Tomcat -->
<target name="tomcat.start">
<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.start" inheritall="false" />
</target>
<!--停止 Tomcat -->
<target name="tomcat.stop">
<ant antfile="${tomcat.dir}/tomcat-build.xml" target="tomcat.stop" inheritall="false" />
</target>
<!--启动 Selenium 服务器 -->
<target name="selenium.start">
<if>
<not>
<socket server="localhost" port="4444" />
</not>
<then>
<java jar="${selenium.dir}/selenium-server.jar" fork="true" spawn="true" />
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="4444" />
</and>
</waitfor>
</then>
</if>
</target>
<!-- 停止 Selenium 服务器 -->
<target name="selenium.stop">
<if>
<socket server="localhost" port="4444" />
<then>
<get taskname="selenium-shutdown" dest="${functionTest.report.dir}/selenium-result.txt" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" ignoreerrors="true" />
</then>
</if>
</target>
<!-- test-all -->
<target name="function-test" depends="deploy">
<!-- 并行容器节点,一边同时打开tomcat 和selenium server,一边等待两者打开后执行JUnit测试。
如果不使用并行节点,而是用spawn=yes属性后台启动tomcat,则屏幕里看不到tomcat信息,如果测试意外终止的话,也不能关闭tomcat -->
<parallel>
<antcall target="tomcat.start" />
<antcall target="selenium.start" />
<sequential>
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="8099" />
</and>
</waitfor >
<waitfor maxwait="5" maxwaitunit="minute" checkevery="3" checkeveryunit="second">
<and>
<socket server="localhost" port="4444" />
</and>
</waitfor >
<!-- Junit Test All -->
<junit printsummary="on" fork="true" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
<classpath>
<pathelement path="${function.dir}" />
<path refid="base.classpath" />
</classpath>
<formatter type="xml" />
...