1. Sonar Server Configuration
PART 1 : SETTING UP SONAR SERVER LOCALLY
Table of Contents
JaCoCo Integration with Maven and Sonar for Code Coverage
This post will explain the Steps to integrate JaCoCo plugin with Sonar and Maven for Code Coverage.
With this integration you will be able to see below details on Sonar dashboard:
1. Bugs
2. Vulnerabilities
3. Debt
4. Code Smells
5. Code Coverage
6. Unit tests
7. Duplicate Code Percentage
8. Duplicate Code Block
JaCoCo
JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years.
To understand more about JaCoCo click here
Prerequisite: we should have Sonar server up and running with the token. to know about this you can follow my previous blog:
Once the above steps are completed successfully, follow the below steps to configure JaCoCo with Maven & Sonar
For this process, we will use a project from below git repository:
https://github.com/onlyfullstack/scalable-web-json-comparator
clone this git project into your local system and follow below steps:
Step-1 : Add below JaCoCo configuration in the pom.xml file under properties section
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin> <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis> <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath> <sonar.language>java</sonar.language> <jacoco.version>0.7.9</jacoco.version>
Step-2 : Add below JaCoCo plugin in the pom.xml file under Plug In section
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <configuration> <skip>${maven.test.skip}</skip> <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile> <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile> <output>file</output> <append>true</append> <excludes> <exclude>*MethodAccess</exclude> </excludes> </configuration> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> <phase>test-compile</phase> </execution> <execution> <id>jacoco-site</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
Step-3 : Execute below mvn command from project root folder(where pom file exist)
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false
This command will create JaCoCo reports for us which will contain the code coverage reports at scalable-web-json-comparatorscalable-webtargetcoverage-reports path.
the code coverage report will be in jacoco-unit.exec file.
Step-4 : Execute below mvn command to push the sonar report to our sonarQube server.
mvn sonar:sonar -Dsonar.jacoco.reportPaths=target/coverage-reports/jacoco-unit.exec -Dsonar.projectName="scalable-web" -Dsonar.projectKey="scalable-web" -Dsonar.host.url=http://localhost:9000 -Dsonar.login=1c01ff3138588827f552cc6e7d4971ed004f5874
sonar.projectName – Project name
sonar.projectKey – Project key
sonar.host.url – Sonar host url
sonar.login – Carries the token which was generated in the Sonar, refer previous post to know more.
sonar.jacoco.reportPaths – JaCoCo report to get the code coverage
Step-5 : Once above build is successful, launch Sonar dashboard using the below URL and verify code coverage and unit test case percentage.
http://localhost:9000
With these steps, we have successfully integrated the JaCoCo plugin with Sonar and Maven for Code Coverage.
Let’s go to our next tutorial where we will discuss below points :
– What is Eclemma?
– How to find out the code coverage in eclipse?
– EclEmma Configuration in Eclipse
Hello,
I have a project which has both Scala and Java code. I'm using Jacoco plugin for code coverage.I could see both java and scala test cases in Jacoco report but my sonarQube analysis is only picking up Java code coverage and not Scala code coverage. Could you please guide in how to configure SonarQube for Scala code coverage with Jacoco reports?