无法在sonarqube中获得测试覆盖率

时间:2016-05-30 12:55:06

标签: maven sonarqube jacoco

[在此处输入图像说明] [1] [在此处输入图像说明] [2]我知道很多人都遇到过类似的问题。我展示了许多答案,尝试了声纳站点上给出的示例代码。该样本工作正常。我还显示以下链接

How to configure multi-module Maven + Sonar + JaCoCo to give merged coverage report?

但是我的情况似乎没有任何效果。虽然mvn clean intsall在index.html有报道报告的网站上制作了jacoco文件夹,但它没有在sonarqube中显示IT测试覆盖率。 我在网上尝试了几乎所有的东西,但无法解决问题。 它是一个多模块项目 我正在使用

java8。

sonarqube 4.5.6 with java plugin 2.5.1。

maven 3.0.5

请帮我解决这个问题。

以下是父模块pom文件

<build>
<pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>    
                    <configuration>
                        <destFile>${project.basedir}/target/jacoco.exec</destFile>
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
                <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}/../target/jacoco.exec</dataFile>
                    </configuration>
                </execution>
                <execution>
                    <id>pre-integration-test</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
                        <propertyName>failsafe.argLine</propertyName>
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.basedir}/../target/jacoco-it.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <!-- Sets the VM argument line used when unit tests are run. -->
                <argLine>${surefireArgLine}</argLine>   
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
            <configuration>
                <argLine>${failsafe.argLine}</argLine>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- automatic label creation -->

    </plugins>  
  </build>
    <!-- Plugin to generate unit test coverage in SonarQube 4.5.x report. -->   

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <configuration>
            <use>false</use>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>${maven-surefire-report-plugin.version}</version>
            <configuration>
                <alwaysGenerateFailsafeReport>true</alwaysGenerateFailsafeReport>
                <alwaysGenerateSurefireReport>true</alwaysGenerateSurefireReport>
                <aggregate>true</aggregate>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco-maven-plugin.version}</version>
            <configuration>
                <excludes>
                    <exclude>**/*.mar</exclude>
                    <exclude>${jacoco.excludePattern}</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</reporting>

同一个pom文件中的属性

   <sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>    <!-- This is the default, put here to be explicit -->
    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <sonar.language>java</sonar.language>
    <sonar.java.binaries>${project.basedir}/../target/classes</sonar.java.binaries>
    <jacoco-maven-plugin.version>0.7.4.201502262128</jacoco-maven-plugin.version>
    <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
    <maven-surefire-report-plugin.version>2.19.1</maven-surefire-report-plugin.version>
    <maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
    <maven-failsafe-plugin.version>2.16</maven-failsafe-plugin.version>

mvn clean install正在创建带有index.html的jacoco文件夹,用于测试覆盖但是mvn声纳:声纳没有在sonarqube中显示

我犯了什么错误。

在mvn声纳:声纳,它成功构建,其中一条线是JaCoCoSensor:未找到JaCoCo报告。

可能是什么原因

我认真地觉得这是jacoco或sonarqube的错误。可能是它与java 8或其他东西不兼容。我几乎尝试了所有的事情。声纳java插件2.5.1不推荐使用很多东西。请帮帮我,我急需解决方案

2 个答案:

答案 0 :(得分:0)

刚试过,sample project完美无缺。请确保您执行以下操作:

git clone https://github.com/SonarSource/sonar-examples.git
cd projects/languages/java/code-coverage/combined ut-it/combined-ut-it-multimodule-maven-jacoco
mvn clean package
mvn sonar:sonar

另外 - 不确定这是否相关,但您使用的是旧版本的SQ Java插件(2.5.1)。我只能建议您将其更新到最新版本。

答案 1 :(得分:0)

嗯,我不确定为什么上面的代码不起作用,但我尝试了同样的事情,奇迹发生了。让我给出解决方案,它对我有用。

我从父pom中删除了以下代码并将其放入所有子pom中。此外,因为我在子模块中使用它我使用target / jacoco.exec而不是$ {project.basedir} /../ target / jacoco.exec和jacoco-it.exec相同。

  <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <executions>
            <execution>
                <id>pre-unit-test</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>    
                <configuration>
                    <destFile>target/jacoco.exec</destFile>
                    <propertyName>surefireArgLine</propertyName>
                    <append>true</append>
                </configuration>
            </execution>
            <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>target/jacoco.exec</dataFile>
                </configuration>
            </execution>
            <execution>
                <id>pre-integration-test</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <destFile>target/jacoco-it.exec</destFile>
                    <propertyName>failsafe.argLine</propertyName>
                    <append>true</append>
                </configuration>
            </execution>
            <execution>
                <id>post-integration-test</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
                <configuration>
                    <dataFile>target/jacoco-it.exec</dataFile>
                </configuration>
            </execution>
        </executions>
    </plugin>

其他信息。如果仅在声纳报告中需要测试覆盖,则不需要我在我的问题中提到的插件。如果需要单独的代码报告,则需要

 <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco-maven-plugin.version}</version>
        <configuration>
            <excludes>
                <exclude>**/*.mar</exclude>
                <exclude>${jacoco.excludePattern}</exclude>
            </excludes>
        </configuration>
    </plugin>