I have a Java project for which I want to see automated test coverage in Jenkins builds.
For this purpose I modified the files as shown below.
Jenkinsfile
:
pipeline {
agent any
stages {
stage("Test") {
steps {
sh 'cat settings_default.xml'
sh 'mvn -gs settings_default.xml test'
}
}
stage("Test Reports") {
steps {
jacoco(
execPattern: 'target/*.exec',
classPattern: 'target/classes',
sourcePattern: 'src/main/java',
exclusionPattern: 'src/test*',
changeBuildStatus: true,
runAlways: true,
minimumBranchCoverage: '60'
)
}
}
stage("Build") {
steps {
sh 'mvn -gs settings_default.xml package'
}
}
}
}
pom.xml
: Added following fragment to build/plugins
:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
<exclude>**/configuration/*.*</exclude>
<exclude>**/model/*</exclude>
<exclude>**/MyApplication.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check-coverage</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
When I run the Jenkins job, I only see the instruction coverage in the report:
What do I need to change and where (pom.xml
, Jenkinsfile
, Jenkins configuration on the server, something else) in order to see the branch and class coverage as well?
Update 1: Adding
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
to pom.xml
did not solve the problem.
The reason for the error was the fact that this was a multi-module Maven project with two different pom.xml
. The Jenkins report was being generated for a module (subdirectory) and one of the steps was configured wrongly (the configuration pointed to the wrong directory).
stage("Test Reports") {
steps {
dir('my-module') {
jacoco(
[...]
Surrounding the jacoco
statement with dir('my-module') { [...] }
fixed the problem.