Bamboo + sonar.dynamicAnalysis=reuseReports = 0% 规则合规性

2022-01-17 00:00:00 sonarqube bamboo java

Bamboo 可以进行持续构建,但希望使用 Sonar 进行质量指标跟踪.必须在 Bamboo 中捕获单元测试通过/失败和 Clover 代码覆盖率指标.但是,这些相同的值也应该发送到 Sonar,以免通过两次运行单元测试和 Clover 来增加构建持续时间.

Bamboo is in place to do continuous builds, but want to use Sonar for quality metrics tracking. Unit test pass/fail and Clover code coverage metrics must be captured in Bamboo. But, these same values should be sent to Sonar as well, so as to not increase build duration by running unit tests and Clover twice.

已经能够使用带有 maven 构建的sonar.dynamicAnalysis=reuseReports"指令将这些指标从 Bamboo 构建发送到 Sonar.但 Sonar 中的规则合规性"指标为 0%.

Have been able to send these metrics from Bamboo builds to Sonar using "sonar.dynamicAnalysis=reuseReports" directive with the maven build. But the "Rules Compliance" metric in Sonar goes to 0%.

根据之前未使用指令的修改,规则合规性得分较高.所以很明显,使用该指令会以某种方式阻止计算该指标.

Based on prior tinkering without using the directive the Rules Compliance score is higher. So it's clear using the directive is somehow preventing that metric from being calculated.

有人知道如何使用该指令并获得规则合规分数吗?或者他们能指出一些可以帮助诊断的东西吗?使用-e"选项运行 maven 并没有提供任何特别有用的东西.

Does anyone know how to use this directive and get a Rules Compliance score? Or can they point out something to look at to help diagnose? Running maven with the "-e" option has not provided anything particularly useful.

推荐答案

经过多次反复试验,我终于能够让 Sonar、Jacoco 和 Bamboo 和谐地合作.我记录了这里的过程,但我会复制我的此处的解决方案,以确保它始终可用.

After much trail and error, I finally was able to get Sonar, Jacoco, and Bamboo working harmoniously together. I documented the process here!, but I'll copy my solution here to ensure it's always available.

对于我的应用程序,我实际上使用了声纳亚军任务.您有更明确的步骤来安装和配置 sonar-runner,安装指南中没有提到.首先,您必须安装 sonar-runner 并在 sonar-runner.properties 中指定以下属性:

For my application, I actually used a sonar runner tasks. You have more explicit steps to install and configure the sonar-runner, which isn't mentioned in the install guide. First, you must install the sonar-runner and specify the following properties in your sonar-runner.properties:

#----- Default Sonar server
sonar.host.url=http://localhost:9000

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#sonar.jdbc.driver=org.postgresql.Driver

#----- Global database settings
sonar.jdbc.username=user
sonar.jdbc.password=passwd

在顶部的 ant 构建脚本中包含 jacoco xmlns:

Include the jacoco xmlns in your ant build script at the top:

<project basedir="." default="build" name="project" xmlns:jacoco="antlib:org.jacoco.ant">
    <property environment="env" />

    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
       <classpath path="libs/independent/jacocoant.jar"/>
    </taskdef>

接下来,您必须将 jacoco 覆盖添加到您的 ant 构建脚本中:

Next, you have to add jacoco coverage to your ant build script:

<jacoco:coverage enabled="${tests.code.coverage}" destfile="${jacoco.exec.dest}">
    <junit fork="yes" printsummary="withOutAndErr" dir="${tests.working.dir}">
    ...

最后,您需要告诉来自竹子的声纳使用 jacoco 结果并重用您构建中生成的报告.您可以通过将以下属性添加到任务配置中的自定义额外参数"以在 Bamboo 作业中进行声纳分析.配置以下选项:

Last, you need to tell sonar, from bamboo, to use jacoco results and reuse the reports generated in your build. You do this by adding the following properties to your "Custom Extra Parameters" in the Task configure for sonar analysis in the Bamboo Job. Configure the following options:

-DbuildNumber=${bamboo.buildNumber}
-Dsonar.core.codeCoveragePlugin=jacoco
-Dsonar.jacoco.reportPath=tests/jacoco-exec/jacoco.exec
-Dsonar.dynamicAnalysis=reuseReports
-Dsonar.surefire.reportsPath=tests/test-reports

配置完所有这些后,我的测试覆盖率开始显示在声纳中,列出了 # 个成功的测试.

Once I had all this configured, my test coverage started showing up in sonar with the # successful tests listed.

请确保您设置了 sunfire 属性以重用它们从您的单元测试中生成的报告.否则,即使您告诉声纳重复使用报告,声纳也不知道在哪里找到它们.希望对您下次尝试有所帮助.

Just be sure you set the sunfire property to reuse their reports generated from your unit tests. Otherwise, sonar won't know where to find them even if you tell it to reuse reports. Hope that helps at your next attempt.

相关文章