Ant和Jacoco:需要将junit的fork设置为"true",而不是将属性设置为true

Dav*_* W. 5 ant junit jacoco

我正在使用Jacoco Junit任务并尝试以下方法:

    <!-- run the junit tests -->
    <echo>DEBUG: $${junit.fork} = "${junit.fork}"</echo>
    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="${junit.fork}" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>
Run Code Online (Sandbox Code Playgroud)

得到以下内容:

test:
     [echo] DEBUG: ${junit.fork} = "true"
[jacoco:coverage] Enhancing junit with coverage

BUILD FAILED
D:\build.xml:233: Coverage can only be applied on a forked VM

Total time: 6 seconds

D:\>
Run Code Online (Sandbox Code Playgroud)

如您所见,${junit.fork}属性设置为true,我使用了该属性<junit fork="${junit.fork}"/>.

但是,我只是设置,而不是使用该属性<junit fork="true">,它工作正常:

    <jacoco:coverage
        destfile="${target.dir}/jacoco.exec"
        append="false">
        <junit fork="true" includeAntRuntime="true">
            <classpath>
                <pathelement path="${main.destdir}"/>
                <pathelement path="${test.destdir}"/>
            </classpath>
            <classpath refid="test.classpath"/>
            <formatter type="${junit.formatter.type}"/>
            <batchtest      todir="${junit.batchtest.todir}">
                <fileset dir="${test.destdir}" />
            </batchtest>
        </junit>
    </jacoco:coverage>
Run Code Online (Sandbox Code Playgroud)

我已经运行ant -d test以验证Java JVM在使用该${junit.fork}属性时是否正在分叉.

为什么JaCoCo坚持认为JUnit测试没有被分叉,除非我将fork参数设置为字符串true而不是属于等于的属性true

gon*_*ard 3

根据Jacoco覆盖任务源代码

...
public void enhanceTask(final Task task) {
    final RuntimeConfigurable configurableWrapper = task.getRuntimeConfigurableWrapper();

    final String forkValue = (String) configurableWrapper.getAttributeMap().get("fork");
    if (!Project.toBoolean(forkValue)) {
        throw new BuildException("Coverage can only be applied on a forked VM", getLocation());
    }
    addJvmArgs(task);
}
...
Run Code Online (Sandbox Code Playgroud)

forkValue 是从configurableWrapper 中读取的。属性映射值(如)使用调用的${junit.fork}方法进行评估。当你继续查看 Jacoco 源代码时: RuntimeConfigurable.maybeConfigureTask.mayBeConfigure

...
public void addTask(final Task task) {
    if (childTask != null) {
        throw new BuildException("Only one child task can be supplied to the coverge task", getLocation());
    }
    this.childTask = task;
    final String subTaskTypeName = task.getTaskType();
    final TaskEnhancer enhancer = findEnhancerForTask(subTaskTypeName);
    if (enhancer == null) {
        throw new BuildException(format("%s is not a valid child of the coverage task", subTaskTypeName), getLocation());
}
    if (isEnabled()) {
        log(format("Enhancing %s with coverage", childTask.getTaskName()));
        enhancer.enhanceTask(task);
    }
    task.maybeConfigure();
}
...
Run Code Online (Sandbox Code Playgroud)

该方法 ( mayBeConfigure) 在任务增强 ( enhancer.enhanceTask(task);) 后调用。我想这是你的问题和错误......我们应该将其报告给 jacoco 错误跟踪器。

雅可可问题