我的build.xml放到JunitTestuild,构造这个文件才可以使用ant命令来执行,包括编译、打包、测试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系统,在DOS窗口下进入你的工作目录,并到 ../JunitTest/build下,执行ant runtests命令,会看到如下信息
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