2013-04-26 51 views
3

我有我的應用程序如下設置:自動運行JUnit的情況下,建立依賴關係

enter image description here

project TestAll目前包含運行在我的所有其他項目的TestAll javafiles的Java文件並按預期工作。我面臨的問題是我想要這個TestAll.java從一個ant腳本運行,並讓它在一個報告文件中記錄結果。這個javafile依賴於我的應用程序中的所有其他項目。

這是我到目前爲止有:

<?xml version="1.0"?> 
<project name="Ant-Test" default="main" basedir="."> 
    <!-- Sets variables which can later be used. --> 
    <property name="src.dir" location="" /> 
    <property name="build.dir" location="../bin" /> 
    <property name="dist.dir" location="../dist" /> 
    <property name="lib.dir" location="../lib" /> 
    <property name="test.dir" location="../src" /> 
    <property name="test.report.dir" location="../testreport" /> 


    <target name="clean"> 
    <delete dir="${build.dir}" /> 
    <delete dir="${dist.dir}" /> 
    </target> 

     <!-- Define the classpath which includes the junit.jar and the classes after compiling--> 
     <path id="junit.class.path"> 
     <pathelement location="${lib.dir}/junit.jar" /> 
     <pathelement location="${build.dir}" /> 
     </path> 

    <target name="makedir"> 
    <mkdir dir="${build.dir}" /> 
    <mkdir dir="${dist.dir}" /> 
    <mkdir dir="${test.report.dir}" /> 
    </target> 

    <!-- Compiles the java code (including the usage of library for JUnit --> 
     <target name="compile" depends="clean, makedir"> 
     <javac srcdir="${src.dir}" destdir="${build.dir}"> 
      <classpath refid="junit.class.path" /> 
     </javac> 
    </target> 

     <target name="junit" depends="compile"> 
     <junit printsummary="on" fork="true" haltonfailure="yes"> 
      <classpath refid="junit.class.path" /> 
      <formatter type="xml" /> 
      <batchtest todir="${test.report.dir}"> 
      <fileset dir="${src.dir}"> 
       <include name="**/*Test*.java" /> 
      </fileset> 
      </batchtest> 
     </junit> 
     </target> 

    <!--Creates the deployable jar file --> 
    <target name="jar" depends="compile"> 
    <jar destfile="${dist.dir}\se.testall.jar" basedir="${build.dir}"> 
     <manifest> 
     <attribute name="Main-Class" value="test.Main" /> 
     </manifest> 
    </jar> 
    </target> 

    <target name="main" depends="compile, jar, junit"> 
    <description>Main target</description> 
    </target> 

</project> 

而且錯誤即時得到的是:

[javac] Compiling 1 source file to C:\Repositories\MyProject\TestAllProjects\bin 
[javac] C:\Repositories\MyProject\TestAllProjects\src\se\testall\src\TestAllClass.java:6: error: package se.tlv.AProject.testall does not exist 
[javac] import se.tlv.AnotherProject.testall.*; 
[javac]^
[javac] C:\Repositories\TestAllProjects\src\se\src\TestAllClass.java:7: error: package se.AnotherProject.testall does not exist 
TestAll project

..和等所有內部進口這是最類似於類路徑錯誤的地方,ANT無法找到它需要的文件,但我不知道如何解決它,並已嘗試幾乎整整一天。任何幫助表示讚賞

+0

+1表現良好的問題。 – 2013-04-26 13:46:43

回答

1

提供給javac任務的classpath是:junit jar,build目錄和當前目錄。

除非當前目錄(build.xml所在位置)爲se,否則javac任務將無法找到任何java文件來編譯它們。

鑑於此,javac任務的類路徑將需要包含每個項目中每個se目錄的路徑。

編輯:

注意:除非你在與其他代碼封裝測試計劃,你應該有建立一個build目錄和測試版本目錄中的兩個javac的任務,然後提供一個路徑到junit的每一個人,以便它可以運行測試,但只提供jar任務的構建目錄路徑。