【问题标题】:AntBuilder can't find package org.junitAntBuilder 找不到包 org.junit
【发布时间】:2020-06-23 19:59:15
【问题描述】:

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

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>

【问题讨论】:

    标签: java eclipse junit


    【解决方案1】:

    尝试将 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>
    

    【讨论】:

    • 我把它改成了
    【解决方案2】:

    尝试直接包含您的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""
     />
    

    希望这会有所帮助。

    【讨论】:

    • 恐怕不行。我开始怀疑我是否首先将它指向了错误的 junit.jar 文件。
    • 我已经提到过:) Also make sure that the JAR file is available at the location.
    【解决方案3】:

    创建一个路径元素并在 javac 中引用该 id 有所帮助。

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

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

    <target name="compile" depends="copy" description="compile all the source files in both src and test directories and should store all the class files in the bin directory.">
        <javac destdir="bin" includes="*.java" classpathref="lib.path.id.junit.jar" includeantruntime="true" debug="true" >
            <src path="src" /> 
            <src path="test" />
        </javac>
    
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2015-06-27
      • 2019-02-28
      • 1970-01-01
      相关资源
      最近更新 更多