【IT168 技术文档】自己做的一个试验项目的Ant文件。其中包含了编译Java文件,生成jar包,利用javadoc产生Java like的API说明文件,利用JUnit作自动测试并且产生JUnit的测试报表。
下面是源代码和一些注释。 以上是由具体的一个项目定制的一个build.xml。在看《JUnit in Action》的时候看到了“如何使用啊Ant作自动测试”的部分,所以利用JUnit生成了一些TestCase和TestSuite,效果总的来说还是不错的,实现了单元测试的自动化(自动测试)和可度量(报表包含了TestCase测试信息,包括测试Case数,通过率,Error个数,Failure个数,对于所有的错误异常都记录在案)。
我们有必要在开始一个新的编译前,清空以前产生的旧文件,这边我的做法是删除文件夹。
比如,docs目录是用来存放API文档的,build文件夹用来存放编译的class文件,现在删除,
可以在后面的Task产生。 -->
<target name="clean">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}" />
<delete dir="${dist}" />
<delete dir="${docs}" />
<delete dir="${rpts}" />
</target>
<!-- 初始化项目,注意depends这个关键字,意思是依赖于某个项目的完成,也就是项目的依赖关系。 -->
<target name="init" depends="clean" description="init the project">
<!-- Create the time stamp -->
<tstamp />
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}" />
</target>
<!-- 编译项目。并且指定源文件在src目录下,生成的class文件存放在build目录。 -->
<target name="compile" depends="init" description="compile the source code">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" />
</target>
<!-- 生成jar文件,并且指定了jar的文件名称和存放的目录 -->
<target name="dist" depends="compile" description="">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/library" />
<!-- Put everything in ${build} into the Demo-${DSTAMP}.jar -->
<jar jarfile="${dist}/library/Demo-${DSTAMP}.jar" basedir="${build}" />
</target>
<!-- 单元测试部分。使用JUnit作自动测试,include关键字指定了包含的java文件。 -->
<!-- Prepare for creating the report-->
<target name="test" depends="dist" description="">
<!-- Create the unit test report directory -->
<mkdir dir="${rpts}"/>
<property name="tests" value="*Test" />
<junit printsummary="yes" haltonerror="no" haltonfailure="no">
<formatter type="plain" usefile="false"/>
<formatter type="xml"/>
<batchtest todir="${rpts}">
<fileset dir="${src.test}">
<include name="**/${tests}.java"/>
</fileset>
</batchtest>
<classpath>
<pathelement location="${classes.test}"/>
</classpath>
</junit>
</target>
<!-- 生成单元测试报告。使用JUnit所带的生成测试报表功能,产生一套本次的测试报告。 -->
<!-- Create the report-->
<target name="report" depends="test" description="">
<junitreport todir="${rpts}">
<fileset dir="${rpts}">
<include name="TEST*.xml"/>
</fileset>
<report todir="${rpts}"/>
</junitreport>
</target>
<!—利用javadoc根据源代码中的javadoc注释生成API文档。 -->
<!-- Api document -->
<target name="docs" depends="report" description="create api doc">
<!-- Create the document directory -->
<mkdir dir="${docs}" />
<!-- Create the document -->
<javadoc sourcepath = "${src}"
destdir="${docs}"
author="true"
version="true"
use="true"
windowtitle="Demo API"
link="${javadoc}">
<!-- Set which name space should be used -->
<packageset dir="${src}" defaultexcludes="yes">
<!-- include name="cn.Siemens.SPSE.CNB.Demo.Calculaotr" / -->
<!-- include name="javadoc_test" /-->
<!-- include name="junit_test" /-->
</packageset>
<doctitle>
<![CDATA[<h1>Demo Project</h1>]]>
</doctitle>
<bottom>
<![CDATA[<i>Siemens PSE All Rights Reserved.</i>]]>
</bottom>
<tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>
</project>