To launch JaCoCo as part of your Maven build, use this command:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false
For more details on JaCoCo, see its documentation.
You can also play with this sample project.
There is a known issue on version 0.7.3 of JaCoCo agent producing binary reports incompatible with JaCoCo analyzer 0.7.2 embedded in Java plugin. This issue is fixed with version 0.7.4 of the agent, so please prefer this version.
If your project uses the
argLine property to configure the surefire-maven-plugin, be sure that
argLine defined as a
property, rather than as part of the plugin configuration. Doing so will allow JaCoCo to set its agent properly. Otherwise the JVM may crash while tests are running.
argLine should be defined this way:
BEFORE 4.0.0 Force coverage to 0%
By default, when no coverage report is found, the Java Plugin will not set any value for coverage metric. This behavior can be overridden to force coverage to 0% in case of a lack of report by setting the following property :
If this property is set while a Jacoco report is provided, this property will have no effect.
Coverage per test (deprecated)
Using some unit test listeners you can collect the information on which lines where covered by which tests and display them in SonarQube. The scope of this functionality extends beyond pure SonarQube capabilities. You will first have to configure a custom SureFire JUnit listener (the actual listener will be org.sonar.java.jacoco.JUnitListener ) , and add a dependency (in your pom.xml) to org.sonarsource.java:sonar-jacoco-listeners . Deprecation notice: this feature is deprecated at SonarQube level and will no longer receive further improvements/maintenance.