2

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.

carlspring
  • 31,231
  • 29
  • 115
  • 197
Upen
  • 1,388
  • 1
  • 22
  • 49

2 Answers2

2

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 https://github.com/SonarSource/sonar-jacoco about how to do this.

slim
  • 447
  • 2
  • 10
  • 27
benzonico
  • 10,635
  • 5
  • 42
  • 50
  • I changed my java plugin to 2.1 and added jacoco plugin to Sonarqube 4.2. Now the test cases are running along with the build phase and also in the sonar goal. I am using jenkins. So the test results are overwritten by Sonar running the test case failing most of the tests. Coverage value is wrong. I have added sonar.dynamicAnalysis=true and sonar.reuseReports=true. – Upen Aug 12 '14 at 18:41
  • Fixed the issue by changing the plugins version. sonar-surefire-plugin-2.1, sonar-squid-java-plugin-2.1, sonar-jacoco-plugin-2.1, sonar-checkstyle-plugin-2.1, sonar-build-stability-plugin-1.2, sonar-pmd-plugin-2.1, sonar-findbugs-plugin-2.1, sonar-java-plugin-2.1. With these versions, sonar automatically runs jacoco and created report. – Upen Aug 13 '14 at 17:27
  • However I would recommend to use latest version of the plugin and generate jaCoCo reports before analysis. – benzonico Aug 13 '14 at 19:30
0

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).

max
  • 471
  • 6
  • 8