简体   繁体   中英

ANT build fails and gives classNotFoundExceeption in html report

After doing so many trial and errors on "How ANT tag works ?", I decided to write my own custom build xml file for the testcases that was written in Java and integrated with JUnit. Unfortunately my build script failing with "ClassNotFoundException". And i can see the log in generated HTML file that you get to see after running ant build script.

Please see below

<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>

And I guessed ANT build will pick all the source files (.java) and then it will look for all the class files that are based in build folder and started running them, but then I see "classNotFoundException" in HTML report. please see below log :

CLASS : "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)

After a while I changed .java to .class in "include" tag of my target called "run-tests" . The reason for doing this is, I thought ANT is not able to look ".java" files in source folder (src/Test/Services) hence I changed to ".class" and then modified dir attribute value in "fileset" tag to "build" so it may easily look for ".class" in BUILD folder where I will have compiled files stored. But none of my trial and error succeeded and ended up with same "classNotFoundException".

I am not sure what went wrong , can someone help me, please?

OK guys, Thanks for what ever little help I got from you. I found the problem.

I had to change include tag attribute name to "name=/Test/Services/**/*.java"/>" and modify dir attribute in fileset tag to "${src}" ; see below :

<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.

NOTE: Under src directory I have Test/Services folder which has source ".java" files in it.

        </fileset>
</batchtest>

It solved my problem, but then i don't understand why ant build was not able to identify source files when i gave dir attribute as dir="${src}/Test/Services" that is in fileset tag and kept name attribute in include tag as name="**/*.java" . My understanding is fileset tag should build the path for for the given dir path , once the path is build include tag will include or look for mentioned source files ie ".java" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM