<?xml version="1.0" encoding="UTF-8"?>
<project name="javaTest" default="run" basedir=".">
<!-- 定义变量-->
<property name="base.dir" value="${basedir}"/>
<property name="testng.output.dir" value="${base.dir}/test-output"/>
<property name="lib.dir" value="${base.dir}/lib"/>
<echo message="import lib" />
<!-- 导入lib下的包-->
<path id="classes">
<fileset dir="${lib.dir}" includes="*jar"/>
<fileset dir="${lib.dir}" includes="*zip"/>
<!-- 定义class存放的位置,run时用到-->
<pathelement location="build"/>
</path>
<!-- 定义testng包的路径,run时用到-->
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.11.jar" />
<!-- 执行clean动作,删除class存放文件夹-->
<target name="clean">
<delete dir="build"/>
</target>
<!-- 执行编译动作,新建class存放文件夹,编译srcdir下的文件-->
<target name="compile" depends="clean">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" classpathref="classes" includeantruntime="off">
</javac>
</target>
<!-- 执行run动作,对xmlfileset下的文件进行执行,并把报告放入outputDir中-->
<target name="run" depends="compile">
<testng classpathref="classes" outputDir="${testng.output.dir}">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
<jvmarg value="-ea" />
</testng>
</target>
</project>
注:complie中和run中必须要用classpathref=”classes“,complie中需要找到lib下的jar包进行编译,run中需要找到path下的存放class文件的位置进行执行。
1,首先需要把第三方类库的jar文件都copy到一个目录下。如/lib
2,其次在Ant script中加入以下语句。此时path的作用就很清楚了,其实就是告诉ant应该去哪里找jar文件。和classpath的作用一样。
<property name="lib.dir" value="lib"/>
<path id="project.classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
3,最后定义要使用的第三方类库,注意classpathref的值就是我们刚刚定义的path id。
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="project.classpath"/>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpathref="project.classpath"/>
<taskdef uri="jwaresoftware.antxtras" resource="org/jwaresoftware/antxtras/install/antlib.xml" classpathref="project.classpath" />
4,然后在ant script中就可以方便的使用除了ant的core-task以外的其他tasks了。
需要注意的是:如果在ant中运行一个程序,例如自己写的helloworld,其中使用了log4j类库,则需要将log4j的jar加入到/lib下才能运行。