使用包含的 jar 文件从 Apache Ant 运行 JUnit 导致“未找到 JUnitTask" [英] Running JUnit from Apache Ant with included jar file results in "JUnitTask was not found"

查看:20
本文介绍了使用包含的 jar 文件从 Apache Ant 运行 JUnit 导致“未找到 JUnitTask"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache Ant 编译和运行 JUnit 测试.目前,它正在将 ant JUnit 库作为 Ant 的一部分安装的环境中工作,但不在其他环境中.我想解决这个问题.我试过包括需要的 jars(junit-4.12.jarhamcrest-core-1.3.jar,如果我理解正确的话)但它不起作用.我的build.xml相关部分如下:

I am using Apache Ant to compile and run JUnit tests. At the moment, it is working on environments where the ant JUnit library is installed as a part of Ant, but not on other environments. I want to fix this. I have tried including the jars needed(junit-4.12.jar and hamcrest-core-1.3.jar, if I understand it correctly) but it is not working. The relevant part of my build.xml is as follows:

  <property name="build.path" location="build/src"/>
  <property name="build.test.path" location="build/test"/>
  <property name="lib.path.rel" value="lib"/>
  <property name="junit.file" value="${lib.path.rel}/junit/junit-4.12.jar"/>
  <property name="hamcrest.file" value="${lib.path.rel}/junit/hamcrest-core-1.3.jar"/>

  <path id="junit.class.path">
    <pathelement location="${junit.file}"/>
    <pathelement location="${hamcrest.file}"/>
    <pathelement location="${build.path}"/>
  </path>

  <target name="test-compile" depends="test-clean">
    <mkdir dir="${build.test.path}"/>

    <javac srcdir="${test.path}"
           destdir="${build.test.path}"
           includeantruntime="false">
      <classpath refid="junit.class.path" />
    </javac>
  </target>

  <target name="test" depends="test-compile">
    <junit>
      <classpath>
        <path refid="junit.class.path"/>
        <pathelement location="${build.test.path}"/>
      </classpath>
      <batchtest>
        <fileset dir="${build.test.path}">
          <include name="**/*Test*"/>
        </fileset>
      </batchtest>
      <formatter type="brief" usefile="false"/>
    </junit>
  </target>

任务 test-compile 成功构建了测试.
但是,任务 test 会导致以下错误:
问题:无法创建任务或输入junit原因:找不到类 org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.

我正在使用 CentOS 7 和 Ant 1.9.2,虽然我认为要求用户将 Ant 更新到可用的最新版本是合理的,但我无法控制他们安装的操作系统和库.因此,我不想通过 yum install something 或诸如此类的方式来解决问题,而是通过找到一种正确使用 JUnit jar 文件的方法(如果可能的话).然后,当他们拉取 git 存储库并运行任务时,它将在任何操作系统或环境中运行(因为他们将使用他们刚刚拉取的 jar 文件).显然,这也意味着将 jars 放在 Ant lib 目录中"将不是一个可行的解决方案.
谢谢.

The task test-compile successfully builds the tests.
The task test, however, results in the following error:
Problem: failed to create task or type junit Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.

I am using CentOS 7 with Ant 1.9.2, and though I think it is reasonable to request users to update Ant to the latest version available, I have no control over what OS and libraries they have installed. As such, I want to solve the problem not by yum install something or whatnot, but by finding a way to properly use the JUnit jar files(if that is possible). Then, when they pull the git repository and run the task, it will work on any OS or environment(since they will be using a jar file they just pulled). Obviously, this also means that "just put the jars in the Ant lib dir" will not be a viable solution.
Thank you.

推荐答案

要修复此错误,您必须将定义 JUnitTask 的 jar 传递给 ant.这个 jar 叫做 ant-junit.jar,你可以(来自 JUnit 任务文档) :

To fix this error you have to pass to ant the jar that is defining the JUnitTask. This jar is called ant-junit.jar and you can either (from JUnit task documentation) :

  1. 将 junit.jar 和 ant-junit.jar 都放在 ANT_HOME/lib 中.
  2. 不要将它们放在 ANT_HOME/lib 中,而是将它们的位置包含在 CLASSPATH 环境变量中.
  3. 使用 -lib 将这两个 JAR 文件添加到您的类路径中.
  4. 在构建文件的 中使用 元素指定两个 JAR 的位置.
  5. 将 ant-junit.jar 保留在 ANT_HOME/lib 中的默认位置,但在传递给 .(从 Ant 1.7 开始)
  1. Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
  2. Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
  3. Add both JARs to your classpath using -lib.
  4. Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file.
  5. Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the passed to . (since Ant 1.7)

对于选项 4,您只需在 ant build 中添加如下声明:

For option 4, you just have to add to your ant build a declaration like:

<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
  <classpath>
    <pathelement location="${lib}/ant-junit.jar"/>
    <pathelement location="${lib}/ant-junit4.jar"/>
  </classpath>
</taskdef>

这篇关于使用包含的 jar 文件从 Apache Ant 运行 JUnit 导致“未找到 JUnitTask"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆