使用包含的jar文件从Apache Ant运行JUnit,结果为“找不到JUnitTask". [英] Running JUnit from Apache Ant with included jar file results in "JUnitTask was not found"

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

问题描述

我正在使用Apache Ant来编译和运行JUnit测试.目前,它正在将Ant JUnit库作为Ant的一部分安装的环境上工作,但在其他环境上则不行.我想解决这个问题.我试过包括所需的jar(如果我正确理解,则为 junit-4.12.jar hamcrest-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安装某些东西或通过其他方式,而是通过找到一种正确使用JUnit jar文件的方法来解决(如果可能的话).然后,当他们提取git存储库并运行任务时,它将在任何OS或环境上运行(因为他们将使用刚提取的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. 使用构建文件中< taskdef> 中的< classpath> 元素指定两个JAR的位置.
  5. 将ant-junit.jar保留在ANT_HOME/lib的默认位置,但将junit.jar包含在传递给的地方.(自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,您只需向蚂蚁添加一个声明,如:

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天全站免登陆