Configuring Jacoco in a Maven Project

With reference to my earlier post on configuring jacoco in a gradle project, in this post we shall see how to configure the same in a maven project.

To enable jacoco code coverage in a maven project, just add the below configuration to your pom.xml file in the plugins section.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<excludes>
<exclude>snmaddula/app/domain/*.class</exclude>
<exclude>snmaddula/app/exception/*.class</exclude>
<exclude>snmaddula/app/filter/*.class</exclude>
<exclude>snmaddula/app/App.class</exclude>
</excludes>
</configuration>
</plugin>

Now execute mvn clean test in the project directory. Navigate to target and open jacoco report file in a web browser to view the coverage report.

That’s all you need to configure jacoco in a maven project.