Domanda

I have a Jenkins Sonar setup, with a maven project. This maven project has a single pom, but in contains java, javascript, html, etc. So for sonar it is a multi module project so that I can get the statistics for each part.

We also want to to get the code coverage analysis for the project, which means we need to run the tests and export them before the sonar analysis (at least from what I have gathered sonar-runner can not do this type of anaysis).

So I have my jenkins job set up to do clean package as the first step, and then we run we add the Invoke Standalone Sonar Analysis as the second step. We have a sonar-project.properties file in the root of the project with all the various settings. Here it is:

sonar.projectKey=Project
sonar.projectName=Project
sonar.projectVersion=0.2.0-SNAPSHOT
sonar.projectDescription=Super Project
sonar.modules=project-java,project-web,project-js
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.core.codeCoveragePlugin=jacoco
project-java.sonar.dynamicAnalysis=reuseReports
project-java.sonar.junit.reportsPath=target/surefire-reports
project-java.sonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec
project-java.sonar.projectName=project-java
project-java.sonar.language=java
project-java.sonar.projectBaseDir=.
project-java.sonar.sources=src/main/java
project-java.sonar.java.source=1.7
project-web.sonar.projectName=project-web
project-web.sonar.language=web
project-web.sonar.projectBaseDir=.
project-web.sonar.sources=src/main/webapp/partials
project-js.sonar.projectName=project-js
project-js.sonar.language=js
project-js.sonar.projectBaseDir=.
project-js.sonar.sources=src/main/webapp/js

Now in my pom I added the following (followed along from Creating Code Coverage Reports for Unit And Integration Tests with The JaCoCo Maven Plugin for the most part):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <!--<skipTests>${skip.unit.tests}</skipTests>-->
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>   
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.4.201312101107</version>
    <executions>
        <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Surefire plugin is executed.
        -->
         <execution>
             <id>pre-unit-test</id>
             <goals>
                 <goal>prepare-agent</goal>
             </goals>
             <configuration>
                 <!-- Sets the path to the file which contains the execution data. -->
                 <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                 <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                 <propertyName>surefireArgLine</propertyName>
             </configuration>
         </execution>
         <!--
            Ensures that the code coverage report for unit tests is created after
            unit tests have been run.
        -->
         <execution>
             <id>post-unit-test</id>
             <phase>test</phase>
             <goals>
                 <goal>report</goal>
             </goals>
             <configuration>
                 <!-- Sets the path to the file which contains the execution data. -->
                 <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                 <!-- Sets the output directory for the code coverage report. -->
                 <outputDirectory>${project.build.directory}/jacoco-ut</outputDirectory>
             </configuration>
         </execution>
    </executions>
</plugin>

So when I do mvn clean package locally everything runs, and I can go to the target directory, I see the reports that were generated (I know sonar does not use these, but it has the code coverage information in it). Now when the Sonar job runs everything seems to work fine, but the results are missing the Unit Test Coverage numbers:

Sample Output

Looking through the build logs I found:

14:39:43.929 INFO  - Sensor JaCoCoSensor...
14:39:43.932 INFO  - Project coverage is set to 0% since there is no directories with classes.
14:39:43.932 INFO  - Sensor JaCoCoSensor done: 3 ms

I have seen “Project coverage is set to 0%” – JaCoCo and Sonar in Jenkins, and tried the suggestions there to no avail. So I assume I am missing another setting or have something set wrong.

È stato utile?

Soluzione

I was missing a property, called sonar.binaries. So I added the line

project-java.sonar.binaries=target/classes

to the properties file and that did the trick.

Altri suggerimenti

Can you try doing mvn clean install?

I had a same problem: missing coverage.

But in my project it is sufficient to add org.jacoco:jacoco-maven-plugin:prepare-agent to my maven build.

I don't need any sonar and jacoco configuration (sonar is running on the same machine as jenkins) 1and just call:

clean org.jacoco:jacoco-maven-plugin:prepare-agent install -P integration-tests --fail-at-end

After that sonar is called by a post build action via the jenkins sonar plugin. http://docs.codehaus.org/display/SONAR/Configuring+SonarQube+Jenkins+Plugin

Versions:

Jenkins 1.583 Sonar Plugin 2.1

PS I configured the plugin version in the pom.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top