Ant and Jacoco: you need to set the junit plug to "true", but not the true property

I am using the Jacoco Junit task and have tried the following:

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

And got the following:

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

As you can see, the property ${junit.fork}was set to true, and I used this property in <junit fork="${junit.fork}"/>.

However, instead of using this property, I just set <junit fork="true">it to work fine:       

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

I performed ant -d testto make sure the Java JVM is deployed when I use the property ${junit.fork}.

Why does JaCoCo insist that the JUnit test does not fork out unless I set the parameter forkto a string trueand not to a property equal to true?

+5
source share
1 answer

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);
}
...

ForkValue . (, ${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);). , ... jacoco tracker.

Jacoco

+3

All Articles