Ant 无法识别其 lib 目录中的 JUnit jar?

标签 ant jar junit javac

我在 Windows XP 下使用 Cygwin,并且尝试让 Ant 使用 JUnit,而无需为其编写任何类路径条目。 JUnit 的文档说我可以:

Put both junit.jar and ant-junit.jar in ANT_HOME/lib.

我的 JUnit in Action 实用副本说:

You may have noticed that you don't explicitly add the JUnit JAR to the classpath. Remember that when you installed Ant, you copied the JUnit JAR file to the ${ANT_HOME}/lib directory in order to use the junit Ant task. Because junit.jar is already on your classpath, you don't need to specify it in the javac task to compile your tests.

$ANT_HOME 设置为 Ant 安装目录。在lib子目录中,我看到ant-junit.jar(与Ant一起安装)和junit-4.9.jar(由我复制)。看起来不错,对吧?但测试编译步骤失败:

[javac] Compiling 1 source file to E:\tools\ctg\bin\test
[javac] E:\tools\ctg\src\test\TestAll.java:3: package org.junit does not exist
[javac] import org.junit.*;

等等。我尝试将 junit-4.9.jar 重命名为 junit.jar。没有喜悦。我查看了 jar 文件,发现该包确实存在于其中。事实上,如果我在 Ant 类路径中显式包含该 jar 文件,则编译工作正常。

哎呀。对于可能出什么问题有什么想法吗?谢谢。

编辑:根据请求,这是 javac 目标:

<path id="classpath">
   <pathelement location="${app-bin}"/>
   <pathelement location="${test-bin}"/>
   <!--pathelement location="${junit-bin}"/-->
</path>

<target name="compile-test" depends="compile-app">
   <javac srcdir="${test-src}"
          destdir="${test-bin}"
          source="${java}"
          target="${java}"
          debug="on"
          includeantruntime="false">
      <compilerarg value="-Xlint"/>
      <classpath refid="classpath"/>
   </javac>
</target>

请注意,JUnit 二进制文件已被注释掉。我的目标是删除它并进行编译工作。

最佳答案

includeantruntime="false" 告诉 ant to not include the libs在 lib 目录中进行编译。

您必须将 jar 添加到类路径中,并将其提供给 javac 目标。或者将该属性设置为 true。

关于Ant 无法识别其 lib 目录中的 JUnit jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391777/

相关文章:

xml - 使用 ant 查找和使用最新修改的文​​件夹/目录

java - 从未包含在类路径中的外部 jar 读取 xml

Java 9 : how to add further jar files to classpath dynamically

java - 在 Saucelabs 中并行运行测试最终会变慢

java - 为什么 Mockito 模拟在没有注释的情况下在我的测试中工作

unit-testing - PHPUnit 本身的错误

java - 从可运行的 jar 创建 Windows .exe(最好在 Linux 上)

java - 无法找到与 ANT (mac) 关联的文件

java - 在 Servlet 中使用 JAR 文件不起作用?

java - 如何避免重复的 JUnit 测试