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

Why does gitlab-ci.yml not recognize sonarqube -Dsonar commands?

所以我正在尝试将 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 文件中添加一些内容才能解决此问题?

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

./gradlew -Dsonar.qualitygate.wait=true

试试这个:

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

我认为这是一个引用问题,很可能来自用于 运行 图片的 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

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

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

./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 服务器机器上。