在 Jenkins 管道中使用 Sonarqube 分析 Gradle Java 项目在

版本:SonarQube:7.9 SonarJava 插件:5.13.1 Java:11 Gradle:5.5


我正在尝试使用声明性 Jenkins 管道对 Gitlab Repo 和 Gradle/Java 项目进行 SonarQube 分析。这是詹金斯文件:


@Library('jenkins-shared')_

pipeline {

    agent {

        docker {

            image 'gradle:5.5-jdk11'

        }

    }

    stages {

        stage('build && SonarQube analysis') {

            steps {

                withSonarQubeEnv('sonar.tools.****) {

                 script {

                     cmd.withNexusCredentials {

                            sh 'gradle --info sonarqube  -Dsonar.projectKey=catalog-service -Dsonar.junit.reportPaths=./build/test-results/test -Dsonar.binaries=./build/classes -Dsonar.coverage.jacoco.xmlReportPaths=./build/reports/jacoco/test/html/index.html'

                        }

                }

            }

        }

    }

        stage("Quality Gate") {

            steps {

                timeout(time: 1, unit: 'HOURS') {

                    // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails

                    // true = set pipeline to UNSTABLE, false = don't

                    // Requires SonarScanner for Jenkins 2.7+

                    waitForQualityGate abortPipeline: true

                }

            }

        }

    }

}

我也将 Sonar 插件应用于 Gradle 构建文件。当我尝试运行此 Jenkins 管道时,出现以下错误:


> Task :sonarqube FAILED

JaCoCo report task detected, but XML report is not enabled or it was not produced. Coverage for this task will not be reported.

Caching disabled for task ':sonarqube' because:

  Build cache is disabled

Task ':sonarqube' is not up-to-date because:

  Task has not declared any outputs despite executing actions.

JaCoCo report task detected, but XML report is not enabled or it was not produced. Coverage for this task will not be reported.

User cache: ?/.sonar/cache

:sonarqube (Thread[Execution worker for ':',5,main]) completed. Took 0.221 secs.

其他所有任务都成功完成。即使当我运行 jacocoTestConverageVerification 或检查时,它总是成功运行,除了任务 Sonarqube



守候你守候我
浏览 157回答 1
1回答

扬帆大鱼

已解决:创建声纳分析所需的临时文件的用户授权存在问题,因此您需要使用 Jenkins 的适当用户运行分析。例如,只需尝试使用 root 用户运行它。此外,SonarQube 需要生成 XML 报告文件,这些文件默认处于关闭状态。您可以像这样在 build.gradle 中打开 XML 文件的生成:jacocoTestReport {    reports {        xml.enabled true    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java