Reputation: 18760
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.
Upvotes: 4
Views: 2014
Reputation: 18760
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.
Upvotes: 1
Reputation: 1523
To see the complete jacoco report, I would use the HTML Publisher plugin.
First, you need to configure the pom.xml file to generate the jacoco coverage report, adding the following plugin.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Then, after running mvn install
command, the jacoco report will be generated in the directory target/site/jacoco/index.html.
Now you have to configure Jenkins to show this report. So, the next step is to install the HTML Publisher plugin in Jenkins. You can use the plugin manager to do this task.
Finally, configure your Jenkins job to run the mvn install
command and set up the HTML Publisher as an after build task. Basically, you tell Jenkins to read the jacoco report and show it.
Another detail is that you need to run Jenkins using the command below to avoid problems when visualizing the report.
java -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox allow-same-origin allow-scripts; default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self'; style-src 'self' 'unsafe-inline'; font-src *;" -jar jenkins.war
The final report will be like this:
Upvotes: 1