Java 使用包含的jar文件从ApacheAnt运行JUnit会导致;“未找到JUnitTask”;

Java 使用包含的jar文件从ApacheAnt运行JUnit会导致;“未找到JUnitTask”;,java,junit,ant,build.xml,Java,Junit,Ant,Build.xml,我正在使用ApacheAnt编译和运行JUnit测试。目前,它是在ant JUnit库作为ant的一部分安装的环境中工作的,而不是在其他环境中。我想解决这个问题。我已经尝试过包含所需的jar(junit-4.12.jar和hamcrest-core-1.3.jar,如果我理解正确的话),但它不起作用。我的build.xml的相关部分如下所示: <property name="build.path" location="build/src"/> <property nam

我正在使用ApacheAnt编译和运行JUnit测试。目前,它是在ant JUnit库作为ant的一部分安装的环境中工作的,而不是在其他环境中。我想解决这个问题。我已经尝试过包含所需的jar(
junit-4.12.jar
hamcrest-core-1.3.jar
,如果我理解正确的话),但它不起作用。我的
build.xml
的相关部分如下所示:

  <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>

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


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

谢谢。

要修复此错误,必须将定义JUnitTask的jar传递给ant。这个jar被称为
antjunit.jar
,您可以(从):

  • 将junit.jar和ant-junit.jar放在ant_HOME/lib中
  • 不要将它们放在ANT_HOME/lib中,而是将它们的位置包含在CLASSPATH环境变量中
  • 使用-lib将这两个JAR添加到类路径中
  • 在构建文件的
    中使用
    元素指定两个jar的位置
  • 将ant-junit.jar保留在ant_HOME/lib中的默认位置,但将junit.jar包含在传递给的文件中。(自Ant 1.7起)
  • 对于选项4,您只需在ant build中添加一个声明,如:

    <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>
    

    要修复此错误,必须将定义JUnitTask的jar传递给ant。这个jar被称为
    antjunit.jar
    ,您可以(从):

  • 将junit.jar和ant-junit.jar放在ant_HOME/lib中
  • 不要将它们放在ANT_HOME/lib中,而是将它们的位置包含在CLASSPATH环境变量中
  • 使用-lib将这两个JAR添加到类路径中
  • 在构建文件的
    中使用
    元素指定两个jar的位置
  • 将ant-junit.jar保留在ant_HOME/lib中的默认位置,但将junit.jar包含在传递给的文件中。(自Ant 1.7起)
  • 对于选项4,您只需在ant build中添加一个声明,如:

    <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>
    
    
    
    您是否考虑过使用Maven下载必要的jar?如果没有,这可以帮助您:您是否考虑过使用Maven下载必要的jar?如果不是,这可能会帮助您:也许我误解了您的意思,但您的解决方案似乎将问题推向了正确的方向,但并没有解决问题:我得到的结果是,如果我将ant junit用于junit 4:
    taskdef类org.apache.tools.ant.taskdefs.optional.junit.JUnitTask无法找到
    ,对于4之前的版本,测试实际运行,但出现错误
    junit.framework.AssertionFailedError:未找到测试
    。我的测试是为JUnit4编写的(没有
    扩展TestCase
    ,使用
    @Test
    注释),我使用的是JUnit4库,因此如果您对如何使JUnit4工作有更多建议,我们将不胜感激。@showagy您是对的:对于JUnit4测试,您还需要将
    ant-junit4.jar
    添加到taskdef的类路径中(请参见我编辑的答案)。我还从该类路径中删除了
    junit-4.12.jar
    hamcrest-core-1.3.jar
    ,因为taskdef声明不需要它们,并且您已经将它们添加到构建中的
    junit
    任务类路径中。希望它能解决你的问题我刚做到了,现在它工作得很好。非常感谢。也许我误解了你的意思,但是你的解决方案似乎朝着正确的方向推进了问题,但没有解决它:我得到的结果是,如果我在junit 4中使用ant junit:
    taskdef类org.apache.tools.ant.taskdefs.optional.junit.JUnitTask找不到
    ,测试实际运行,但出现错误
    junit.framework.AssertionFailedError:未找到测试
    。我的测试是为JUnit4编写的(没有
    扩展TestCase
    ,使用
    @Test
    注释),我使用的是JUnit4库,因此如果您对如何使JUnit4工作有更多建议,我们将不胜感激。@showagy您是对的:对于JUnit4测试,您还需要将
    ant-junit4.jar
    添加到taskdef的类路径中(请参见我编辑的答案)。我还从该类路径中删除了
    junit-4.12.jar
    hamcrest-core-1.3.jar
    ,因为taskdef声明不需要它们,并且您已经将它们添加到构建中的
    junit
    任务类路径中。希望它能解决你的问题我刚做到了,现在它工作得很好。非常感谢。