[英]ANT build fails and gives classNotFoundExceeption in html report
在“ ANT標記如何工作?”上進行了許多試驗和錯誤之后,我決定為用Java編寫並與JUnit集成的測試用例編寫自己的自定義生成xml文件。 不幸的是,我的構建腳本失敗,出現“ ClassNotFoundException”。 而且我可以看到運行ant build腳本后看到的登錄生成的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)
一段時間后,我將目標的“運行”中的“包含”標記中的.java更改為.class。 這樣做的原因是,我認為ANT無法在源文件夾(src / Test / Services)中查找“ .java”文件,因此我更改為“ .class”,然后將“ fileset”標記中的dir屬性值修改為“ build”,因此它可以輕松地在BUILD文件夾中尋找“ .class”,我將在其中存儲編譯的文件。 但是我的嘗試和錯誤都沒有成功,並以相同的“ classNotFoundException”結束。
我不確定出了什么問題,可以有人幫我嗎?
好的,謝謝您給我的幫助。 我發現了問題。
我不得不將include標簽屬性名稱更改為“ name = / Test / Services / ** / *。java” />“,並將文件集標簽中的dir屬性修改為” $ {src}“;請參見以下內容:
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}" >
<exclude name="MyFile.java"/>
<include name="/Test/Services/**/*.java"/> //<----this where i had to modify.
注意:在src目錄下,我具有Test / Services文件夾,其中包含源“ .java”文件。
</fileset>
</batchtest>
它解決了我的問題,但是當我在文件集標記中將dir屬性指定為dir =“ $ {src} / Test / Services”並將其保留在name屬性中時,我不明白為什么螞蟻生成無法識別源文件包括標記為name =“ ** / *。java”。 我的理解是文件集標記應為給定的dir路徑構建路徑,一旦構建路徑,包含標記將包含或查找提到的源文件,即“ .java”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.