为什么 gitlab-ci.yml 无法识别 sonarqube -Dsonar 命令?

时间:2021-05-31 07:56:22

标签: gitlab sonarqube build.gradle gitlab-ci

所以我正在尝试将 SonarQube 与我的 Gitlab CI 集成。现在这是我目前拥有的 gitlab-ci.yml 文件:

stages:
  - build
  - sonarqube-check
  - test
  - deploy



cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches

build:
  stage: build
  script:
    - ./gradlew assemble
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week
  only:
    - master

sonarqube-check:
  stage: test
  image: gradle:jre11-slim
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script: ./gradlew sonarqube -Dsonar.qualitygate.wait=true
  allow_failure: true
  only:
    - master


test:
  stage: test
  script:
    - ./gradlew check

deploy-prod:
  stage: deploy
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."

after_script:
  - echo "End CI"

我的 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "org.sonarqube" version "2.7.1"
    id 'jacoco'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}


dependencies {
    implementation 'org.apache.httpcomponents:httpcore:4.4.1'
    implementation 'org.apache.httpcomponents:httpclient:4.5'
    implementation('io.jsonwebtoken:jjwt:0.2')
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'junit:junit:4.12'
    implementation 'org.modelmapper:modelmapper:2.4.1'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r'
    /**
     * JUnit jupiter with mockito.
     */
    testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
}
jacocoTestReport{
    dependsOn test
    reports{
        xml.enabled true
        csv.enabled false
        html.enabled false
        html.destination layout.buildDirectory.dir('jacocoHtml').get().asFile
    }
}

sonarqube{
    properties{
        property 'sonar.java.source', '1.8'
        property 'sonar.java.coveragePlugin', 'jacoco'
    }
}



test {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

tasks["sonarqube"].dependsOn("jacocoTestReport")

问题是我不断收到此错误:

Task '.qualitygate.wait=true' not found in root project 'demo'.

当我尝试使用 Dsonar.login 时,我也遇到此错误。 为什么会出现此错误,我该如何解决? 我需要在 build.gradle 文件中添加一些东西来解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

这是因为您没有指定要运行的 gradle 任务:

./gradlew -Dsonar.qualitygate.wait=true

试试这个:

./gradlew sonarqube -Dsonar.qualitygate.wait=true

答案 1 :(得分:1)

我遇到了同样的问题。尝试改变

./gradlew sonarqube -Dsonar.qualitygate.wait=true

./gradlew sonarqube -D'sonar.qualitygate.wait=true'

我参考了此处给出的示例:

https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-gradle/gradle-basic

我的 Gitlab runner 和 Sonarqube 服务器在 Windows 服务器机器上运行。

答案 2 :(得分:0)

我相信这是一个引用问题,很可能来自用于运行图像的 shell:

PS> docker run -it --rm gradle:6.8.1-jre11 gradle tasks -Dsonar.login=foo
...
FAILURE: Build failed with an exception.

* What went wrong:
Task '.login=foo' not found in root project 'gradle'.


PS> docker run -it --rm gradle:6.8.1-jre11 gradle tasks "-Dsonar.login=foo"
...
BUILD SUCCESSFUL in 4s
1 actionable task: 1 executed

尝试在与属性相关的开关周围添加引号。