一、compile:
<path id="compile-classpath">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"></fileset>
<fileset dir="${test.lib.dir}" includes="*.jar"></fileset>
<fileset dir="${cobertura.home}">
<include name="*.jar" />
<include name="lib/*.jar" />
</fileset>
<pathelement location="${build.test.classes}"/>
</path>
<taskdef classpathref="compile-classpath" resource="tasks.properties" />
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="${src.dir}" encoding="ISO-8859-1"
classpathref="compile-classpath"
includeantruntime="true" failοnerrοr="true" debug="true">
</javac>
<copy todir="${build.classes}">
<fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java"></fileset>
</copy>
<copy todir="${build.classes}">
<fileset dir="${conf.dir}"></fileset>
</copy>
</target>
二、test: //cobertura测代码覆盖率【要自己安装】
用到的jar:https://download.csdn.net/download/qq_37704364/10604690(不包含cobertura)
<path id="run-test-classpath">
<path refid="compile-test-classpath"/>
<pathelement location="${build.test.classes}"/>
</path>
<path id="compile-test-classpath">
<path refid="compile-classpath"></path>
<pathelement location="${build.classes}"/>
</path>
<target name="compile-test" depends="compile">
<javac destdir="${build.test.classes}" srcdir="${test.src.dir}" encoding="ISO-8859-1"
includeantruntime="true" failοnerrοr="true"
classpathref="compile-test-classpath">
</javac>
</target>
<target name="run-test" depends="compile-test">
<delete file="cobertura.ser"/>
<cobertura-instrument todir="${cobertura.class.dir}">
<fileset dir="${build.classes}">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
<junit fork="true" haltonfailure="false" failureproperty="junit.fail" >
<classpath location="${cobertura.class.dir}" />
<classpath location="${build.classes}" />
<classpath location="${build.test.classes}" />
<classpath refid="compile-classpath"></classpath>
<formatter type="xml"/>
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${build.test.report}" unless="testcase">
<fileset dir="${test.src.dir}" includes="**/*Test.java" ></fileset>
</batchtest>
</junit>
<junitreport>
<fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset>
<report format="frames" todir="${build.test.report}/html"/>
</junitreport>
<cobertura-report srcdir="${src.dir}" destdir="${cobertura.dir}" format="html" />
<delete file="cobertura.ser"/>
<fail if="${junit.fail}" message="JUnit failed, ${build.test.report}"/>
</target>
<target name="doc" depends="run-test">
<javadoc sourcepath="${src.dir}" use="true" packagenames="com.*"
charset="UTF-8" encoding="UTF-8" docencoding="UTF-8"
destdir="${build.doc.dir}">
<classpath refid="compile-classpath"></classpath>
</javadoc>
</target>
三、war:
<target name="unWar" depends="compile-test">
<copy todir="${build.war.dir}">
<fileset dir="${web.dir}"></fileset>
</copy>
<copy todir="${build.war.dir}/WEB-INF/classes">
<fileset dir="${build.classes}">
</fileset>
</copy>
</target>
<target name="war" depends="unWar">
<delete file="${build.dir}/${ant.project.name}.war"></delete>
<war destfile="${build.dir}/${ant.project.name}.war" webxml="${build.war.dir}/WEB-INF/web.xml">
<fileset dir="${build.war.dir}" />
</war>
</target>
<target name="deploy" depends="war">
<exec executable="${env.CATALINA_HOME}/bin/shutdown.bat"/>
<delete file="${env.CATALINA_HOME}/webapps/${ant.project.name}.war"></delete>
<delete dir="${env.CATALINA_HOME}/webapps/${ant.project.name}"></delete>
<copy todir="${env.CATALINA_HOME}/webapps" file="${build.dir}/${ant.project.name}.war"/>
<exec executable="${env.CATALINA_HOME}/bin/startup.bat"/>
</target>
四、doc:
<target name="doc" depends="run-test">
<javadoc sourcepath="${src.dir}" use="true" packagenames="com.*"
charset="UTF-8" encoding="UTF-8" docencoding="UTF-8"
destdir="${build.doc.dir}">
<classpath refid="compile-classpath"></classpath>
</javadoc>
</target>
五、完整的build.xml:【要配置Tomcat的路径】
<?xml version="1.0" encoding="UTF-8"?>
<project name="online-exam" default="run-test" basedir=".">
<property environment="env"></property>
<property name="src.dir" location="src"></property>
<property name="test.src.dir" location="test"></property>
<property name="conf.dir" location="conf"></property>
<property name="web.dir" location="WebContent"></property>
<property name="lib.dir" location="${web.dir}/WEB-INF/lib"></property>
<property name="cobertura.home" location="D:\cobertura-2.0.3"></property>
<property name="cobertura.lib.dir" location="${cobertura.home}/lib"></property>
<property name="build.dir" location="build"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.test.dir" location="${build.dir}/test"></property>
<property name="build.test.classes" location="${build.test.dir}/classes"></property>
<property name="build.test.report" location="${build.test.dir}/report"></property>
<property name="build.doc.dir" location="${build.dir}/doc"></property>
<property name="build.war.dir" location="${build.dir}/war"></property>
<property name="test.lib.dir" location="lib"></property>
<property name="cobertura.dir" location="${build.dir}/cobertura/"></property>
<property name="cobertura.html.dir" location="${cobertura.dir}/html"></property>
<property name="cobertura.class.dir" location="${cobertura.dir}/class"></property>
<path id="compile-classpath">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"></fileset>
<fileset dir="${test.lib.dir}" includes="*.jar"></fileset>
<fileset dir="${cobertura.home}">
<include name="*.jar" />
<include name="lib/*.jar" />
</fileset>
<pathelement location="${build.test.classes}"/>
</path>
<taskdef classpathref="compile-classpath" resource="tasks.properties" />
<path id="compile-test-classpath">
<path refid="compile-classpath"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-classpath">
<path refid="compile-test-classpath"/>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
<mkdir dir="${build.doc.dir}"/>
<mkdir dir="${build.war.dir}"/>
<mkdir dir="${build.war.dir}/WEB-INF"/>
<mkdir dir="${build.war.dir}/WEB-INF/class"/>
<mkdir dir="${cobertura.dir}"/>
<mkdir dir="${cobertura.html.dir}"/>
<mkdir dir="${cobertura.class.dir}"/>
</target>
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="${src.dir}" encoding="ISO-8859-1"
classpathref="compile-classpath"
includeantruntime="true" failοnerrοr="true" debug="true">
</javac>
<copy todir="${build.classes}">
<fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java"></fileset>
</copy>
<copy todir="${build.classes}">
<fileset dir="${conf.dir}"></fileset>
</copy>
</target>
<target name="compile-test" depends="compile">
<javac destdir="${build.test.classes}" srcdir="${test.src.dir}" encoding="ISO-8859-1"
includeantruntime="true" failοnerrοr="true"
classpathref="compile-test-classpath">
</javac>
</target>
<target name="run-test" depends="compile-test">
<delete file="cobertura.ser"/>
<cobertura-instrument todir="${cobertura.class.dir}">
<fileset dir="${build.classes}">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
<junit fork="true" haltonfailure="false" failureproperty="junit.fail" >
<classpath location="${cobertura.class.dir}" />
<classpath location="${build.classes}" />
<classpath location="${build.test.classes}" />
<classpath refid="compile-classpath"></classpath>
<formatter type="xml"/>
<test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
<batchtest todir="${build.test.report}" unless="testcase">
<fileset dir="${test.src.dir}" includes="**/*Test.java" ></fileset>
</batchtest>
</junit>
<junitreport>
<fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset>
<report format="frames" todir="${build.test.report}/html"/>
</junitreport>
<cobertura-report srcdir="${src.dir}" destdir="${cobertura.dir}" format="html" />
<delete file="cobertura.ser"/>
<fail if="${junit.fail}" message="JUnit failed, ${build.test.report}"/>
</target>
<target name="doc" depends="run-test">
<javadoc sourcepath="${src.dir}" use="true" packagenames="com.*"
charset="UTF-8" encoding="UTF-8" docencoding="UTF-8"
destdir="${build.doc.dir}">
<classpath refid="compile-classpath"></classpath>
</javadoc>
</target>
<target name="unWar" depends="compile-test">
<copy todir="${build.war.dir}">
<fileset dir="${web.dir}"></fileset>
</copy>
<copy todir="${build.war.dir}/WEB-INF/classes">
<fileset dir="${build.classes}">
</fileset>
</copy>
</target>
<target name="war" depends="unWar">
<delete file="${build.dir}/${ant.project.name}.war"></delete>
<war destfile="${build.dir}/${ant.project.name}.war" webxml="${build.war.dir}/WEB-INF/web.xml">
<fileset dir="${build.war.dir}" />
</war>
</target>
<target name="deploy" depends="war">
<exec executable="${env.CATALINA_HOME}/bin/shutdown.bat"/>
<delete file="${env.CATALINA_HOME}/webapps/${ant.project.name}.war"></delete>
<delete dir="${env.CATALINA_HOME}/webapps/${ant.project.name}"></delete>
<copy todir="${env.CATALINA_HOME}/webapps" file="${build.dir}/${ant.project.name}.war"/>
<exec executable="${env.CATALINA_HOME}/bin/startup.bat"/>
</target>
</project>