AntBuilder找不到包org.junit

时间:2012-10-11 21:19:58

标签: java eclipse junit

我在Eclipse的项目中使用“JUnit测试用例”新文件对话框将一些单元测试添加到测试目录(与我的src目录并行)。我有两个构建器,默认的Java Builder和我添加的AntBuilder。 Java Builder继续工作,但AntBuilder在Eclipse中失败。当我选择Project - >全部构建,它显示:

Buildfile: C:\source\machine-paint\eclipse\machine-paint\src\build.xml

clean:
      [delete] Deleting directory C:\source\machine-paint\eclipse\machine-paint\build

compile:
       [mkdir] Created dir: C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] Compiling 33 source files to C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] C:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.java:3: error: package org.junit does not exist
       [javac] import static org.junit.Assert.*;
       [javac]                        ^

然后在一些类似的错误之后,这个:

 [javac] C:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.java:8: error: incompatible types
       [javac]  @Test
       [javac]   ^
       [javac]   required: Annotation
       [javac]   found:    Test

这是我的构建文件。此时我真的在黑暗中开火,比如将这个类路径标记添加到javac命令。

<?xml version="1.0" encoding="UTF-8"?>
<project name="Builder" default="jar" basedir=".">
  <target name="clean">
    <delete dir="build"/>
  </target>

  <target name="compile" depends="clean">
    <mkdir dir="build/classes" />
    <javac srcdir="." destdir="build/classes" debug="true" includeantruntime="true">
      <classpath>
        <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" />
      </classpath>
    </javac>
  </target>

  <target name="jar" depends="compile">
    <jar destfile="../../stencil.jar" basedir="build/classes">
      <manifest>
        <attribute name="Main-Class" value="stencil.Main" />
      </manifest>
    </jar>
    <copy file="../../stencil.jar" tofile="../../../robotsdoart/stencils/stencil.jar" />
  </target>

  <target name="run">
    <java jar="build/jar/stencil.jar" fork="true" />
  </target>
</project>

3 个答案:

答案 0 :(得分:2)

尝试将srcdir更改为您的实际源目录,因此它不包含测试。

类似的东西:

<javac srcdir="src/" destdir="build/classes" debug="true" includeantruntime="true">
  <classpath>
    <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" /> <!-- not sure if you need this -->
  </classpath>
</javac>

答案 1 :(得分:0)

尝试直接包含您的junit jar文件。还要确保该位置可以使用JAR文件。

<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpath="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar"
 />

或者将path元素定义为:

 <path id="lib.path.id">
    <fileset dir="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib" 
             includes="junit.jar"/>
 </path>

并在javac

中使用相同的内容
<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpathref="lib.path.id""
 />

希望这有帮助。

答案 2 :(得分:0)

创建路径元素并在javac中引用该ID很有帮助。

class SpaceCreateView(CreateView):
    model = Space
    fields = ['title', 'space_image']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

然后我使用classpathref在javac中引用了它

<path id="lib.path.id.junit.jar">
    <fileset dir="${project-path}\lib" includes="*.jar" />
</path>