Ant + Junit在第一次运行时起作用,而且从未再次运行

时间:2009-11-24 20:41:41

标签: java ant junit

嘿伙计们,我一整天都在尝试获取一个ant文件来自动构建我的项目。该文件(附后)在我找到的网页上,非常彻底。

我的问题是,只要我没有运行“干净”的目标,它就会起作用。一旦我运行“清洁”目标,“测试”目标就会停止工作。我在所有测试类上都得到NoClassFound错误。虽然它早先工作了。

我正在使用最新版本的eclipse开发Macbook(10.5.8)。从eclipse和使用Ant版本1.7.1的终端运行文件时会发生此错误

我稍微修改了文件以使其适应我的文件结构,如下所示:

src/
   packageA.packageB/
                     ClassA.java
                     ...
                     ClassN.java

unittests/
          packageA.packageB/
                            AllClassTests.java
                            ClassATest.java
                            ...
                            ClassNTest.java

lib/
    junit-4.7.jar

ant文件是:

<project name="SampleJUnitTests" default="dist" basedir=".">
<description>
    DataTypes Build File
</description>
<!-- set global properties for this build -->

<property name="project_name" value="DataTypes"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist"  location="dist"/>
<property name="lib"  location="lib"/>
<property name="reports" location="reports"/>
<property name="tests" location="unittests"/>
<property name="tmp" location="tmp_file"/>

<!-- the names of various distributable files -->
<property name="jar_name" value="${project_name}.jar"/>
<property name="war_name" value="${project_name}.war"/>

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

<target name="dist" depends="compile" description="generate the distributable files " >
    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/${jar_name}" basedir="${build}"/>
</target>

<target name="clean" description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
    <delete dir="${reports}"/>
    <delete dir="${tmp}"/>
</target>

<target name="run-tests" depends="compile" description="run your test suite" >
    <junit printsummary="yes" haltonfailure="no" showoutput="yes" tempdir="${tmp}">
        <classpath>
            <pathelement path="${build}"/>
            <fileset dir="lib">
                <include name="**/*.jar"/>
            </fileset>
        </classpath>
      <batchtest fork="yes" todir="${reports}/raw/">
        <formatter type="xml"/>
        <fileset dir="${tests}">
          <include name="**/*.java"/>
          <exclude name="**/All*Tests.java"/>
        </fileset>
      </batchtest>
    </junit>
</target>

<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 ="run" depends="" description="if this project can be run, run it" >
</target>

<!-- supporting targets -->

<target name="init" description="initialize the build environment" >
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create directory structures -->
    <mkdir dir="${build}"/>
    <mkdir dir="${lib}"/>
    <mkdir dir="${dist}/lib"/>
    <mkdir dir="${reports}"/>
    <mkdir dir="${reports}/raw/"/>
    <mkdir dir="${reports}/html/"/>
    <mkdir dir="${tmp}"/>
</target>

<target name="all" depends="clean, test">
</target>

我放弃了,希望有人可以解决我的问题。 非常感谢!

1 个答案:

答案 0 :(得分:2)

您没有构建测试类的任何Ant目标。如果您愿意,可以将此作为compile目标的一部分执行:

<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>
  <javac srcdir="${tests}" destdir="${build}">
    <classpath>
      <pathelement path="${build}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
    </classpath>
  </javac>
</target>