Sonar maven jacoco code coverage for Multimodule p

2019-07-22 03:46发布

问题:

I have a multimodule project with a parent pom.xml and two modules in it. Project.jar and Project.war. All the test cases are under Project.jar. When I run mvn sonar.sonar goal on the Parent pom, jacoco.exec is not getting generated and code coverage is blank. I have the following properties in the parent pom.

<properties>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
</properties>

Please help. I am using Sonarqube 4.2.

回答1:

With version 2.4 of the java plugin you have to generate the coverage report before running the SonarQube analysis. You can see this page http://docs.codehaus.org/display/SONAR/JaCoCo+Plugin about how to do this.



回答2:

For those that have a similar issue, I solved this problem slightly differently (where I'm running it from the command-line and not having Jenkins do it). I didn't have an issue with the plugins.

All these settings are defined in the parent POM. I am using the following version:

<plugin.jacoco.version>0.7.2.201409121644</plugin.jacoco.version>

The report path is slightly different, this makes it so only one jacoco.exec is created. By default Sonar will append results together if the file already exists.

<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>

In the pluginManagement section, I have the following plugin:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${plugin.jacoco.version}</version>
  <configuration>
    <destFile>${sonar.jacoco.reportPath}</destFile>
  </configuration>
  <executions>
    <execution>
      <id>agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Then in my plugins section I make sure the plugin is available for the entire project:

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
  </plugin>

As benzonico noted, you need to run tests prior to sonar analyzing them, which I do as follows:

mvn clean test sonar:sonar

I did not have to make any changes to the Surefire plugin (as I've had to do in the past).