Easy distinct unit and integration test code coverage with sonarqube

Simple compute distinct code coverage for unit tests and integration tests. I used jacoco 0.7.2, sonarqube 5.0 and maven 3.2.5 to cook the numbers ;).

<project ...>
    ...
        <profile>
            <id>sonar-coverage</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.2.201409121644</version>
                        <configuration>
                            <append>true</append>
                        </configuration>
                        <executions>
                            <execution>
                                <id>agent-for-ut</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>agent-for-it</id>
                                <goals>
                                    <goal>prepare-agent-integration</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>jacoco-site</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    ...
</project>
A simple call with maven to run the test suite and activate jacoco class instrumentation would be:
mvn clean verify -P sonar-coverage
Later on the usual sonarqube run:
mvn sonar:sonar

See also Updates:
  • 2015-02-07, no need for extra failsafe config
  • 2015-10-25, change phase for using the results, dont let failing integration tests miss your integration coverage