Ant 使用 Manifestclasspath 到 运行 junit ClassNotFoundException

Ant using Manifestclasspath to run junit ClassNotFoundException

我正在尝试在由 ant 任务启动的 junit 测试中将 codecoverage 与 jacoco 集成。 jacoco 迫使我分叉我的 junit 的事实给了我一些问题,因为我的类路径很长并且分叉崩溃了。 我正在使用 manifestclasspath 在 jar 文件中添加我的类路径,并将我的新 jar 作为参数发送给 VM,但它不起作用。我的测试 运行 但它们都 return 一个 ClassNotFoundException。 这是我如何配置 ant 进程的一部分。

<path refid="bin.classpath"/>

bin.classpath 包含我需要放入 .jar 文件的所有路径。

<target name="run-unit-tests" depends="init" description="Runs all the unit tests">
  <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
    <classpath path="jacocoant.jar" />
  </taskdef>

  <manifestclasspath property="binjar" jarfile="binManifest.jar">
    <classpath refid="bin.classpath" />
  </manifestclasspath>

  <jar destfile="manifestJars/binManifest.jar">
    <manifest>
      <attribute name="Class-Path" value="${binjar}" />
    </manifest>
  </jar>

  <jacoco:coverage destfile="results/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
    <junit fork="true" reloading="false" showoutput="true">
      <classpath>
        <pathelement path="${projects.dir}/AntProject/manifestJars/binManifest.jar" />
      </classpath>

      <batchtest todir="${test.data.dir}" fork="true">
        <fileset dir="${projects.dir}/ProjectFolder1/src" />
        <fileset dir="${projects.dir}/ProjectFolder2/src" />
      </batchtest>
    </junit>
  </jacoco:coverage>
</target>

如果我在 运行 执行 ant-task 时查看控制台日志,我可以看到我的新 .jar 文件是如何发送的:

-classpath''C:\Users\XXX\Project\AntProject\manifestJars\binManifest.jar;

如果我打开创建的 binManifest.jar,我会发现一个 MANIFEST.MF 文件,我的所有路径都在 Class-Path 属性 中,格式为:../.. /Class1/bin../../Class2/bin../../ClassN/bin。但是由于某种原因,我的所有测试都失败了,因为找不到我的 类。我错过了什么?谢谢

让我们从没有 JaCoCo 的情况开始 - 下面是使用 junit fork="true"manifestclasspathcomplete and verifiable example

main/Example.java:

class Example {
  public static void sayHello() {
    System.out.println();
  }
}

test/ExampleTest.java:

public class ExampleTest {
  @org.junit.Test
  public void test() {
    Example.sayHello();
  }
}

build.xml:

<project xmlns:jacoco="antlib:org.jacoco.ant" default="build">
  <target name="build">
    <delete dir="bin" />
    <mkdir dir="bin/main-classes" />
    <mkdir dir="bin/test-classes" />

    <javac target="1.5" debug="true" destdir="bin/main-classes">
      <src path="main" />
    </javac>

    <javac target="1.5" debug="true" destdir="bin/test-classes">
      <src path="test" />
      <classpath>
        <pathelement path="bin/main-classes"/>
        <pathelement path="lib/junit-4.12.jar"/>
        <!-- otherwise "java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" -->
        <pathelement path="lib/hamcrest-core-1.3.jar"/>
      </classpath>
    </javac>

    <manifestclasspath property="binjar" jarfile="bin/manifest.jar">
      <classpath>
        <pathelement path="bin/main-classes"/>
        <pathelement path="bin/test-classes"/>

        <pathelement path="lib/junit-4.12.jar"/>
        <!-- otherwise "java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" -->
        <pathelement path="lib/hamcrest-core-1.3.jar"/>
      </classpath>
    </manifestclasspath>

    <jar destfile="bin/manifest.jar">
      <manifest>
        <attribute name="Class-Path" value="${binjar}" />
      </manifest>
    </jar>

    <junit fork="true" showoutput="true">
      <formatter type="brief" usefile="false" />
      <classpath>
        <pathelement path="bin/manifest.jar" />
      </classpath>
      <batchtest>
        <fileset dir="test" includes="**/*Test.java" />
      </batchtest>
    </junit>
  </target>
</project>

请注意,根据 Junit 的要求,除 junit-4.12.jar 外还存在 lib/hamcrest-core-1.3.jar - 请参阅 https://github.com/junit-team/junit4/wiki/Download-and-Install

让我们通过执行 ant.

来确认它无一例外地工作

在添加 JaCoCo 之后

<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
  <classpath path="jacocoant.jar" />
</taskdef>

到头,

<jacoco:coverage destfile="bin/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
...
</jacoco:coverage>

大约 junit,最后

<jacoco:report>
  <executiondata>
    <file file="bin/jacoco.exec"/>
  </executiondata>
  <structure name="JaCoCo Ant Example">
    <classfiles>
      <fileset dir="bin/main-classes"/>
    </classfiles>
    <sourcefiles encoding="UTF-8">
      <fileset dir="main"/>
    </sourcefiles>
  </structure>
  <html destdir="bin/report"/>
</jacoco:report>

到最后没什么大不了的。执行 ant 将在目录 bin/report.

中生成报告