【问题标题】:Not able to get it test coverage in sonarqube无法在 sonarqube 中测试覆盖率
【发布时间】:2016-09-28 07:35:04
【问题描述】:

[在此处输入图像描述][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 和 java 插件 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 sonar:sonar 未在 sonarqube 中显示它

我犯了什么错误。

在 mvn sonar:sonar 中,它构建成功,其中一行是 JaCoCoSensor: JaCoCo report not found。

可能是什么原因

我真的觉得这是 jacoco 或 sonarqube 的错误。可能它与 java 8 或其他东西不兼容。我几乎尝试了每一件事。声纳 java 插件 2.5.1 不推荐使用许多东西。请帮助我,我迫切需要解决方案

【问题讨论】:

  • sonar.dynamicAnalysis 已弃用 sonar java 插件 2.5.1
  • 可能是插件的组合会是问题
  • 在线示例代码运行良好,因为其中没有集成测试用例......请帮助我

标签: maven sonarqube jacoco


【解决方案1】:

刚刚试了一下,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)。我只能建议您将其更新到最新版本。

【讨论】:

  • 谢谢法布里斯。我之前确实尝试过示例项目。无论如何,我现在能够解决它。我通过将 Jacoco 插件及其步骤放入子 pom 文件中得到了解决方案。还有一件事,我的模块是一个大模块,所以构建它会出现问题,所以我使用 argLine 作为属性而不是在surefire插件中
【解决方案2】:

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

我从父 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> 

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2021-07-09
    • 2022-07-01
    • 2018-10-18
    • 2014-05-04
    • 2019-05-15
    • 2015-03-13
    • 2016-10-03
    • 2020-09-22
    相关资源
    最近更新 更多