3

So I am trying to integrate SonarQube with my Gitlab CI. Now this is the gitlab-ci.yml file that I currently have:

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"

My 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")

The problem is that I keep getting this error:

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

I also get this error when I try it with Dsonar.login. Why do I get this error, and how do I fix it? Do I need to add something to my build.gradle file in order to fix this problem?

LardinoisJosse
  • 363
  • 2
  • 5
  • 22

3 Answers3

2

It's because You didn't specify gradle task to be run:

./gradlew -Dsonar.qualitygate.wait=true

Try this instead:

./gradlew sonarqube -Dsonar.qualitygate.wait=true
  • I got the same error doing this: $ ./gradlew sonarqube -Dsonar.qualitygate.wait=true FAILURE: Build failed with an exception. * What went wrong: Task '.qualitygate.wait=true' not found in root project 'demo'. – LardinoisJosse May 31 '21 at 08:37
1

I encountered the same issue. Try changing

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

to

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

I referred to the examples given here:

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

My Gitlab runner and Sonarqube server are running on a Windows server machine.

eastmael
  • 371
  • 3
  • 7
0

I believe this is a quoting issue, most probably from the shell that is being used to run the image:

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

Try to add quotes around your property-related switches.

raspy
  • 3,995
  • 1
  • 14
  • 18