Sonar + maven configuration + Jenkins
Revision as of 15:07, 20 May 2015 by WikiFreak (talk | contribs) (Created page with "Category:Development Category:Linux SonarQube requires quite some configuration to be fully useful! This article explains '''how to configure your maven projects'''...")
SonarQube requires quite some configuration to be fully useful!
This article explains how to configure your maven projects to use SonarQube properly, bringing both Unit and Integration tests coverage.
Contents
Principle
To work well SonarQube requires 4 configuration steps:
Sonar properties
First of all you need to configure SONAR by setting up some properties and paths.
Key points:
- SONAR is configured for JAVA language and it will use Jacoco as coverage tool.
- Each maven module will have its own Unit Tests results (Surefire reports + .exec file) inside its own
target
directory - The Integration Tests results are common. Meaning, all modules will write in the same directory. That directory is relative to the maven execution.
Technical details:
- Surefire unit tests plugin
- Outputs in ${project.build.directory}
- 1 XML file per test class in: ${sonar.junit.reportsPath}
- SONAR report file (jacoco-ut.exec) will be available at ${sonar.out.unitTestsReport}
- Failsafe integration tests plugin
- All outputs in the same directory, relative to the local execution: ${session.executionRootDirectory}
POM.xml extract:
<properties>
...
<!-- Maven plugins -->
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<maven-failsafe-plugin.version>2.18.1</maven-failsafe-plugin.version>
<!-- Test frameworks -->
<junit.version>4.12</junit.version>
<!-- ==== SONARQUBE quality metrics ==== -->
<maven.jacoco.version>0.7.4.201502262128</maven.jacoco.version>
<maven.sonar-jacoco-listeners.version>3.2</maven.sonar-jacoco-listeners.version>
<!-- Global Sonar settings. Do not change them! -->
<sonar.language>java</sonar.language>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<jacoco.lib.path>
${settings.localRepository}/org/jacoco/org.jacoco.agent/${maven.jacoco.version}/org.jacoco.agent-${maven.jacoco.version}-runtime.jar
</jacoco.lib.path>
<javaagent>${jacoco.lib.path}</javaagent>
<!-- Don't let Sonar execute tests. We will let Maven do it during build phase -->
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- Report -->
<!-- IMPORTANT: integration test path must be ABSOLUTE, especially for muli-modules projects -->
<!-- ${session.executionRootDirectory} = directory from where the "mvn" command is run -->
<!-- a) Where sonar will find the standard test reports -->
<sonar.junit.reportsPath>${project.build.directory}/surefire-reports</sonar.junit.reportsPath>
<sonar.junit.itReportsPath>${session.executionRootDirectory}/target/failsafe-reports</sonar.junit.itReportsPath>
<!-- b) Sonar specific reports -->
<sonar.out.unitTestsReport>${project.build.directory}/jacoco-ut.exec</sonar.out.unitTestsReport>
<sonar.out.integrationTestsReport>${session.executionRootDirectory}/target/reports/jacoco-it.exec</sonar.out.integrationTestsReport>
</properties>
Jacoco plugin (coverage agent)
Surefire configuration (UT tests)
Failsafe configuration (IT tests)
Real life example
You can check out one of my GitHub project:
Even better, you can check out this excellent tutorial:
References
This article is based on my daily work in VEHCO.
To update to SonarQube I used the following articles and code examples:
- http://www.aheritier.net/maven-failsafe-sonar-and-jacoco-are-in-a-boat/
- VERY good GitHub example, provided by the same author: https://github.com/dgageot/coverage
- http://stackoverflow.com/questions/26253277/jacoco-agent-for-integration-tests-on-remote-machine