sonarQube 无法生成覆盖文件

sonarQube fails to generate coverage file

我无法让 sonarQube 输出我的 java Spring 项目的覆盖率报告。因此,它始终在界面中显示 0.0% 的覆盖率。我跟着 this 还是不生成文件。以下是 pom.xml 的相关部分,底部是日志。默认情况下,覆盖率报告应该在 target/site/jacoco/jacoco.xml; 中,但是即使我注释掉 它仍然不会输出任何内容。

    <sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco-aggregate/jacoco.xml
        </sonar.coverage.jacoco.xmlReportPaths>
        <sonar.exclusions>
            **/atlas/base/api/config/*.java,
            **/atlas/base/api/model/*.java,
            **/atlas/base/api/repository/*.java
            **/atlas/bse/api/ClientIntegrationApplication.java
        </sonar.exclusions>
        <!-- JaCoCo Properties -->
        <jacoco.version>0.8.7</jacoco.version>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!--        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>-->
        <sonar.language>java</sonar.language>
.
.
.
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.6</version>
        </dependency>
.
.
.
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
[INFO] --- sonar-maven-plugin:3.9.0.2155:sonar (default-cli) @ **** ---
[INFO] User cache: C:\Users\***\.sonar\cache
[INFO] SonarQube version: 9.1.0
[INFO] Default locale: "en_US", source code encoding: "UTF-8"
[INFO] Load global settings
[INFO] Load global settings (done) | time=168ms
[INFO] Server id: BF41A1F2-**
[INFO] User cache: C:\Users\***\.sonar\cache
[INFO] Load/download plugins
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=115ms
[INFO] Load/download plugins (done) | time=263ms
[INFO] Process project properties
[INFO] Process project properties (done) | time=48ms
[INFO] Execute project builders
[INFO] Execute project builders (done) | time=43ms
[INFO] Project key: as:asdf
[INFO] Base dir: C:\Code\***\***
[INFO] Working dir: C:\Code\***\***\target\sonar
[INFO] Load project settings for component key: '***'
[INFO] Load project settings for component key: '***' (done) | time=37ms
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=87ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=2291ms
[INFO] Indexing files...
[INFO] Project configuration:
[INFO]   Excluded sources: **/atlas/base/api/config/*.java, **/atlas/base/api/model/*.java, **/atlas/base/api/repository/*.java
**/atlas/bse/api/ClientIntegrationApplication.java
[INFO] 17 files indexed
[INFO] 2 files ignored because of inclusion/exclusion patterns
[INFO] 0 files ignored because of scm ignore settings
[INFO] Quality profile for java: Sonar way
[INFO] Quality profile for xml: Sonar way
[INFO] ------------- Run sensors on module tango-maguire-client-integration
[INFO] Load metrics repository
[INFO] Load metrics repository (done) | time=42ms
[INFO] Sensor JavaSensor [java]
[INFO] Configured Java source version (sonar.java.source): 8
[INFO] JavaClasspath initialization
[INFO] JavaClasspath initialization (done) | time=54ms
[INFO] JavaTestClasspath initialization
[INFO] JavaTestClasspath initialization (done) | time=31ms
[INFO] Java "Main" source files AST scan
[INFO] 15 source files to be analyzed
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=44ms
[INFO] 15/15 source files have been analyzed
[INFO] Java "Main" source files AST scan (done) | time=5933ms
[INFO] Java "Test" source files AST scan
[INFO] 1 source file to be analyzed
[INFO] 1/1 source file has been analyzed
[INFO] Java "Test" source files AST scan (done) | time=420ms
[INFO] No "Generated" source files to scan.
[INFO] Sensor JavaSensor [java] (done) | time=6972ms
[INFO] Sensor CSS Rules [cssfamily]
[INFO] No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
[INFO] Sensor CSS Rules [cssfamily] (done) | time=16ms
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[WARNING] No coverage report can be found with sonar.coverage.jacoco.xmlReportPaths='C:\Code\TheAtlasTango\tango-maguire-client-integration\target/site/jacoco-aggregate/jacoco.xml'. Using default locations: target/site/jacoco/jacoco
.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
[INFO] No report imported, no coverage information will be imported by JaCoCo XML Report Importer
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=47ms
[INFO] Sensor C# Project Type Information [csharp]
[INFO] Sensor C# Project Type Information [csharp] (done) | time=22ms
[INFO] Sensor C# Analysis Log [csharp]
[INFO] Sensor C# Analysis Log [csharp] (done) | time=32ms
[INFO] Sensor C# Properties [csharp]
[INFO] Sensor C# Properties [csharp] (done) | time=0ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [C:\***\target\surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=158ms
[INFO] Sensor JavaXmlSensor [java]
[INFO] 1 source file to be analyzed
[INFO] 1/1 source file has been analyzed
[INFO] Sensor JavaXmlSensor [java] (done) | time=279ms
[INFO] Sensor HTML [web]
[INFO] Sensor HTML [web] (done) | time=21ms
[INFO] Sensor XML Sensor [xml]
[INFO] 1 source file to be analyzed
[INFO] 1/1 source file has been analyzed
[INFO] Sensor XML Sensor [xml] (done) | time=360ms
[INFO] Sensor VB.NET Project Type Information [vbnet]
[INFO] Sensor VB.NET Project Type Information [vbnet] (done) | time=16ms
[INFO] Sensor VB.NET Analysis Log [vbnet]
[INFO] Sensor VB.NET Analysis Log [vbnet] (done) | time=25ms
[INFO] Sensor VB.NET Properties [vbnet]
[INFO] Sensor VB.NET Properties [vbnet] (done) | time=0ms
[INFO] ------------- Run sensors on project
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=65ms
[INFO] Sensor Java CPD Block Indexer
[INFO] Sensor Java CPD Block Indexer (done) | time=71ms
[INFO] SCM Publisher SCM provider for this project is: git
[INFO] SCM Publisher 1 source file to be analyzed
[INFO] SCM Publisher 0/1 source files have been analyzed (done) | time=223ms
[WARNING] Missing blame information for the following files:
[WARNING]   * pom.xml
[WARNING] This may lead to missing/broken features in SonarQube
[INFO] CPD Executor 4 files had no CPD blocks
[INFO] CPD Executor Calculating CPD for 11 files
[INFO] CPD Executor CPD calculation finished (done) | time=55ms
[INFO] Analysis report generated in 183ms, dir size=184.4 kB
[INFO] Analysis report compressed in 488ms, zip size=60.6 kB
[INFO] Analysis report uploaded in 53ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=atlas%3Atango-maguire-client-integration
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://localhost:9000/api/ce/task?id=AXx7eZTCNRdggghymtti
[INFO] Analysis total time: 15.831 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  22.053 s
[INFO] Finished at: 2021-10-13T14:05:38-07:00
[INFO] ------------------------------------------------------------------------

属性 称为“sonar.coverage.jacoco.xmlReportPaths”(注意“s”)。您的文本和代码示例为此指定了不同的 属性 名称和值。弄清楚它在哪里并使用它。不同的构建配置可能会将其放在不同的位置。查看构建的工作区以查看文件的创建位置。

我的第一个猜测是你的插件没有设置好。这是一个工作示例:

        <!-- Dependencies needed for creating SonarQube coverage reports -->
         <plugin>
             <groupId>org.jacoco</groupId>
             <artifactId>jacoco-maven-plugin</artifactId>
             <version>0.8.7</version>
             <executions>
                 <execution>
                     <id>prepare-agent</id>
                     <goals>
                         <goal>prepare-agent</goal>
                     </goals>
                 </execution>
                 <execution>
                     <id>report</id>
                     <phase>prepare-package</phase>
                     <goals>
                         <goal>report</goal>
                     </goals>
                 </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>
                         <!-- Sets the output directory for the code coverage report. -->
                         <outputDirectory>target/jacoco-ut</outputDirectory>
                     </configuration>
                 </execution>
             </executions>
             <configuration>
                 <systemPropertyVariables>
                     <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                 </systemPropertyVariables>
             </configuration>
         </plugin>

请注意 <id>post-unit-test</id> 执行步骤以及 dataFileoutputDirectory 元素。在这里你应该设置路径。

您似乎正在执行“mvn sonar:sonar” - “sonar”目标不会生成覆盖率报告,它希望覆盖率报告已经生成。 “prepare-agent”目标负责生成 .exec 文件,“report”目标从 .exec 文件生成 .xml 报告。 在您的配置中,“报告”目标绑定到包阶段,您在 运行 宁“mvn sonar:sonar”之前 运行“mvn package”或“mvn install”吗?