Ant和Jacoco:需要设置junit';s fork to;“真的”;但不包括';这是真的

Ant和Jacoco:需要设置junit';s fork to;“真的”;但不包括';这是真的,ant,junit,jacoco,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"> <juni

我正在使用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>
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:\>
如您所见,
${junit.fork}
属性设置为
true
,我在
中使用了该属性

但是,我没有使用该属性,而是简单地设置了
,它可以正常工作:


当我使用
${junit.fork}
属性时,我运行了
ant-d测试来验证Java JVM是否正在分叉

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

forkValue是从configurableWrapper读取的。属性映射值(如
${junit.fork}
)使用
RuntimeConfigurable.maybeConfigure
方法计算,该方法由
Task.maybeConfigure
调用。当您继续查看Jacoco源代码时:

...
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();
}
...
此方法(
mayBeConfigure
)在任务增强后调用(
enhancer.enhanceTask(task);
)。 我想这是你的问题和一个bug。。。我们应该向jacoco bug追踪器报告


谢谢。我不是一个真正的Java开发人员,所以我花了一段时间才明白你在说什么。仍然不是100%确定,但至少我知道我看到的不是我自己的问题,也不是预期的行为。我将报告错误,并链接到您的答案。
...
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);
}
...
...
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();
}
...