我的build.xml放到JunitTestuild,構(gòu)造這個(gè)文件才可以使用ant命令來執(zhí)行,包括編譯、打包、測(cè)試junit用例
<?xml version="1.0" encoding="UTF-8" ?>
<project name="anita" default="main" basedir="../">
<property name="app.name" value="sayhello" />
<property name="build.dir" value="build/classes" />
<target name="JUNIT">
<available property="junit.present" classname="junit.framework.TestCase" />
</target>
<target name="compile" depends="JUNIT">
<mkdir dir="${build.dir}"/>
<javac srcdir="src/" destdir="${build.dir}" >
<include name="**/*.java"/>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="build/lib"/>
<jar jarfile="build/lib/${app.name}.jar"
basedir="${build.dir}" includes="com/**"/>
</target>
<target name="compiletests" depends="jar">
<mkdir dir="build/testcases"/>
<javac srcdir="src/test" destdir="build/testcases">
<classpath>
<pathelement location="build/lib/${app.name}.jar" />
<pathelement path="" />
</classpath>
<include name="**/*.java"/>
</javac>
</target>
<target name="runtests" depends="compiletests" if="junit.present">
<java fork="yes" classname="junit.textui.TestRunner"
taskname="junit" failonerror="true">
<arg value="test.com.company.AllJunitTests"/>
<classpath>
<pathelement location="build/lib/${app.name}.jar" />
<pathelement location="build/testcases" />
<pathelement path="" />
<pathelement path="${java.class.path}" />
</classpath>
</java>
</target>
</project>
如果是win系統(tǒng),在DOS窗口下進(jìn)入你的工作目錄,并到 ../JunitTest/build下,執(zhí)行ant runtests命令,會(huì)看到如下信息
D:TEMPJunitTestTestCaseuild>ant runtests
Buildfile: build.xml
JUNIT:
compile:
[mkdir] Created dir: D:TEMPJunitTestTestCaseuildclasses
[javac] Compiling 3 source files to D:TEMPJunitTestTestCaseuildclasses
jar:
[mkdir] Created dir: D:TEMPJunitTestTestCaseuildlib
[jar] Building jar: D:TEMPJunitTestTestCaseuildlibsayhello.jar
compiletests:
[mkdir] Created dir: D:TEMPJunitTestTestCaseuild estcases
[javac] Compiling 2 source files to D:TEMPJunitTestTestCaseuild estcas
es
runtests:
[junit] .
[junit] Time: 0
[junit] OK (1 test)
BUILD SUCCESSFUL
Total time: 7 seconds