一、ant下载与安装
进入https://ant.apache.org/ ,选择Binary Distributions 并选择bin.zip结尾的文件,下载安装并配置环境变量
二、build.xml文件的编写,例:
<?xml version="1.0"?>
<project name="Lab_1160300406" default="test auot junit and report" basedir=".">
<!-- 定义工程依赖的jar包存放的位置 -->
<property name="lib.dir" value="lib"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<property name="lib.dir" value="lib"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!-- 定义其他文件的位置 -->
<property name="output folder" value="classes"/>
<property name="src folder" value="src"/>
<property name="test folder" value="test"/>
<property name="report folder" value="report"/>
<target name="clean">
<delete dir="report"/>
<echo>清除测试报告文件 成功!</echo>
</target>
<target name="compile init">
<mkdir dir="${output folder}"/>
<echo>创建编译文件夹 成功!</echo>
</target>
<target name="report init" depends="clean">
<mkdir dir="${report folder}"/>
<echo>创建测试报告文件夹 成功!</echo>
</target>
<target name="compile" depends="compile init">
<javac srcdir="${src folder}" destdir="${output folder}" encoding="ISO-8859-1" classpathref="classpath" includeAntRuntime="false"/>
<echo>项目源文件编译 成功!</echo>
</target>
<target name="test compile" depends="report init">
<javac srcdir="${test folder}" destdir="${output folder}" encoding="ISO-8859-1" classpathref="classpath" includeAntRuntime="false"/>
<echo>项目测试文件编译 成功!</echo>
</target>
<target name="all compile" depends="compile, test compile">
</target>
<target name="test auot junit and report" depends="all compile">
<junit printsummary="on" fork="true" showoutput="true">
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${output folder}"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${report folder}">
<fileset dir="${output folder}">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${report folder}">
<fileset dir="${report folder}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report folder}"/>
</junitreport>
</target>
</project>
<property name="output folder" value="classes"/>
<property name="src folder" value="src"/>
<property name="test folder" value="test"/>
<property name="report folder" value="report"/>
<target name="clean">
<delete dir="report"/>
<echo>清除测试报告文件 成功!</echo>
</target>
<target name="compile init">
<mkdir dir="${output folder}"/>
<echo>创建编译文件夹 成功!</echo>
</target>
<target name="report init" depends="clean">
<mkdir dir="${report folder}"/>
<echo>创建测试报告文件夹 成功!</echo>
</target>
<target name="compile" depends="compile init">
<javac srcdir="${src folder}" destdir="${output folder}" encoding="ISO-8859-1" classpathref="classpath" includeAntRuntime="false"/>
<echo>项目源文件编译 成功!</echo>
</target>
<target name="test compile" depends="report init">
<javac srcdir="${test folder}" destdir="${output folder}" encoding="ISO-8859-1" classpathref="classpath" includeAntRuntime="false"/>
<echo>项目测试文件编译 成功!</echo>
</target>
<target name="all compile" depends="compile, test compile">
</target>
<target name="test auot junit and report" depends="all compile">
<junit printsummary="on" fork="true" showoutput="true">
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${output folder}"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${report folder}">
<fileset dir="${output folder}">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${report folder}">
<fileset dir="${report folder}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report folder}"/>
</junitreport>
</target>
</project>
三、打开cmd,cd进入需要编译和运行的项目文件夹,键入 ant