Jacoco unit test code coverage

I am converting ANT construct to Maven. I am not using Sonar.

In Maven, Jacoco doesn't seem to report the scope of the unit tests themselves, but ANT. I tried to get this for my Maven build, but I couldn't find anything.

It seems like I should add the target <include>

to the target prepare-agent

, but I'm not sure what to include. I've tried src/test/java/*

all sorts of variations of this theme as well, but none works.

How can I configure Jacoco in Maven to report unit test code coverage?


source to share

1 answer

It turns out the only way to do it is to use maven-antrun-plugin


There is no need to add <include>

to the target prepare-agent

because all information is present in the file jacoco.exec

it generates, including the unit test code.

The target report

does not include it, and it also cannot be configured to use it. You will need to specifically set the classfiles

and properties sourcefiles

, and the Maven Jacoco plugin will not let you do this.

Hence, you need the Maven Antrun plugin, and configure and call it from there.


                        <taskdef name="report" classname="org.jacoco.ant.ReportTask" classpathref="maven.plugin.classpath" />
                                <file file="${project.build.directory}/jacoco.exec" />
                            <structure name="Coverage">
                                    <fileset dir="${project.build.directory}/classes"/>
                                    <fileset dir="${project.build.directory}/test-classes"/>
                                <sourcefiles encoding="UTF-8">
                                    <fileset dir="src/main/java"/>
                                    <fileset dir="src/test/java"/>
                            <check failonviolation="true" violationsproperty="violation">
                                <rule element="BUNDLE">
                                    <limit counter="INSTRUCTION" value="COVEREDRATIO" minimum="0.95" />
                            <html destdir="${project.build.directory}/jacoco-internal"/>




All Articles