1

「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 ビルドはすべてのソース ファイル (.java) を選択し、ビルド フォルダーに基づくすべてのクラス ファイルを探して実行を開始すると推測しましたが、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フォルダーで「.class」を簡単に探すことができます。しかし、私の試行錯誤はどれも成功せず、同じ「classNotFoundException」になりました。

何がうまくいかなかったのかわかりません。誰か助けてください。

4

1 に答える 1

0

OK みんな、私があなたから得た小さな助けに感謝します。問題が見つかりました。

include タグの属性名を "name=/Test/Services/**/*.java"/>" に変更し、fileset タグの 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 ディレクトリの下に、ソース ".java" ファイルを含む Test/Services フォルダーがあります。

        </fileset>
</batchtest>

それは私の問題を解決しましたが、ファイルセットタグにある dir="${src}/Test/Services" として dir 属性を指定し、name 属性を保持したときに、ant ビルドがソースファイルを識別できなかった理由がわかりませんname="**/*.java" としてタグを含めます。私の理解では、fileset タグは、指定された dir path のパスを構築する必要があります。パスが構築されると、include タグは、言及されたソース ファイル、つまり ".java" をインクルードまたは検索します。

于 2013-05-11T02:10:41.713 に答える