Skip to content

Ferdinand Agyei-Yeboah

Sonar and Jacoco Maven Configuration

October 30, 2020

Background

Sonar: Static analysis for code coverage and code quality (bad practices, bugs, etc..)

Jacoco: Java code coverage tool which can generate code coverage reports and enforce code coverage thresholds.

POM Examples

Sonar configuration

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.host.url>http://sonar_url_here/</sonar.host.url>
<sonar.exclusions>**/model/*</sonar.exclusions>
</properties>
...
...
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>

To run use: mvn sonar:sonar
Pass arguments: mvn sonar:sonar -Dsonar.host.url=<sonar_url>

Jacoco configuration

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
<excludes>
<exclude>**/Main.class</exclude>
<exclude>**/model/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

Software Engineering Tutorials & Best Practices