我已按照 Jacoco 文档和 soanrqube 文档上的说明进行操作。 我的声纳服务器在自托管运行器上运行。我已将 Sonar UI 上的参数 sonar.coverage.jacoco.xmlReportPaths 留空,因为前面提到它将采用默认路径为 target/site/jacoco/jacoco.xml。我也没有在我的 pom 中设置此属性,当我在本地运行 mvn test 时,我可以看到在同一位置生成的报告 xml 文件。我还可以看到有关我的代码覆盖率的 html 页面。 现在我希望它也显示在我的声纳用户界面上。如前所述,我正在使用带有 github 操作的远程自托管运行器。我可以看到 sonarqube UI 上添加的新代码(我对 jacoco 的 pom 所做的所有更改),但代码覆盖率仍然为 0%。
我看到了在工作流程步骤之间移动工件的答案,但不明白。这是我的工作流程文件(从文件中删除的任何内容都将替换为...):
name: SonarQube
on:
workflow_dispatch:
pull_request:
jobs:
scanning:
runs-on: self-hosted
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 21
uses: actions/setup-java@v1
with:
java-version: 21
- name: Configure Maven settings
uses: whelk-io/maven-settings-xml-action@v22
with:......
- name: Cache Maven packages
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
SONAR_TOKEN: ${{ ... }}
SONAR_HOST_URL: ${{ ... }}
run: mvn -B verify -DskipTests org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=... -Dsonar.projectName='...'
在pom.xml中,我添加了插件和依赖,plugin:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
pom.xml 中的属性:
<jacoco.version>0.8.10</jacoco.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
pom.xml 中的依赖关系:
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
</dependency>
我还尝试在 pom 属性和 sonar gui 上为 sonar.coverage.jacoco.xmlReportPaths 提供相同的路径,但它仍然不起作用。尽管在服务器上我可以在声纳容器日志中看到,但我没有任何错误日志:
2024.05.06 10:44:06 INFO ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute new coverage | status=SUCCESS | time=34ms
2024.05.06 10:44:06 INFO ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute coverage measures | status=SUCCESS | time=1ms
2024.05.06 10:44:06 INFO ce[b5aba757-5a3e-4285-bddc-eda685e8126e][o.s.c.t.s.ComputationStepExecutor] Compute comment measures | status=SUCCESS | time=0ms
-DskipTests 是我的工作流程命令中的问题