在对“ANT 标签如何工作?”进行了如此多的试验和错误之后,我决定为用 Java 编写并与 JUnit 集成的测试用例编写自己的自定义构建 xml 文件。不幸的是,我的构建脚本因“ClassNotFoundException”而失败。我可以在运行 ant 构建脚本后看到生成的 HTML 文件的日志。
请看下面
<project name="WebServices integrated with JUnit and generating report with ANT" default="test" basedir="." >
<description> REST Services integration with JUnit </description>
<!-- set global properties for this build -->
<property name="project_name" value="junit"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="${user.home}/My Documents/Mr/jars"/>
<property name="reports" location="reports"/>
<!-- the names of various distributable files. NOTE: Generating distribution file "target" is not used here-->
<!-- Delete the ${build} and ${dist} directory trees -->
<target name="clean" description="clean up">
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>
<!-- Top level targets -->
<target name="compile" depends="init" description="compile the source code">
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- run your tests -->
<target name="run-tests" depends="compile" description="run your test suite">
<junit printsummary="yes" haltonfailure="no" showoutput="yes">
<classpath>
<pathelement path="${build}"/>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}/Test/Services" >
<exclude name="MyFile.java"/>
<include name="**/*.java"/> // <------ IMP***: Here I am saying include .java files that are based at "${src}/Test/Services".
</fileset>
</batchtest>
</junit>
</target>
<!-- generate report on tests -->
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}/html/"/>
</junitreport>
</target>
<target name="init" depends="clean" description="initialize the build envrionment">
<!--create the time stamp -->
<tstamp/>
<!-- Create directory structure -->
<mkdir dir="${build}"/> //<----dir for class files
<mkdir dir="${lib}"/> //<----dir for all my libraries
<mkdir dir="${dist}/lib"/> //<----not used
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/> //<---- it will have output reports
</target>
<target name="all" depends="clean,test">
</target>
我猜想 ANT build 会选择所有源文件(.java),然后它会查找所有基于 build 文件夹的类文件并开始运行它们,但后来我在 HTML 报告中看到“classNotFoundException”。请参阅以下日志:
类:“getlieninfo”
> java.lang.ClassNotFoundException: getlieninfo
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
过了一会儿,我在名为“run-tests”的目标的“include”标签中将 .java 更改为 .class。这样做的原因是,我认为 ANT 无法在源文件夹(src/Test/Services)中查看“.java”文件,因此我更改为“.class”,然后将“fileset”标签中的 dir 属性值修改为“build”,所以它可以很容易地在 BUILD 文件夹中查找“.class”,我将在其中存储编译文件。但是我的试验和错误都没有成功,并以相同的“classNotFoundException”告终。
我不确定出了什么问题,有人可以帮我吗?