How to setup sonarqube task in default multi modules gradle projects

The currend documentation doesn’t explain how to setup Sonarcloud on a standard gradle multi module project and I’m having quite an hard time trying to.

The project I am referring slightly differ from a project created using the following steps with gradle 6.7.1 but it is totally equivalent.

gradle init

Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3

Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 2

Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 2

Project name (default: application):
Source package (default: application):

> Task :init
Get more help with your project: https://docs.gradle.org/6.7.1/samples/sample_building_java_applications_multi_project.html

I’ve tried to configure my build settings using the official documentation, repositories examples, some workaround from this forum and the SonarSource github page.

Here is my buildSrc/build.gradle.kts

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    // Support convention plugins written in Kotlin. Convention plugins are build scripts in 'src/main' that automatically become available as plugins in the main build.
    `kotlin-dsl`
}

repositories {
    // Use the plugin portal to apply community plugins in convention plugins.
    gradlePluginPortal()
}

dependencies {
    implementation("org.sonarsource.scanner.gradle", "sonarqube-gradle-plugin", "3.0")
}

And here is my buildSrc/src/main/kotlin/com.ttbmp.cinehub.java-common-conventions.gradle.kts which is included in all the project modules as a plugin.

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    // Apply the java Plugin to add support for Java.
    java
    org.sonarqube
}

repositories {
    // Use JCenter for resolving dependencies.
    jcenter()
}

dependencies {
    // Use JUnit Jupiter API for testing.
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2")

    // Use JUnit Jupiter Engine for testing.
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

tasks.test {
    // Use junit platform for unit tests.
    useJUnitPlatform()
}

sonarqube {
    properties {
        property("sonar.sourceEncoding", "UTF-8")
        property("sonar.projectKey", "TTBMP_Cinehub")
    }
}

This are details about the error i’m facing:

  • ALM used: GitHub

  • CI system used: Travis CI

  • Languages of the repository: java

  • Repository URL: https://github.com/TTBMP/Cinehub

  • Error observed:

    BUILD SUCCESSFUL in 8s
    20 actionable tasks: 6 executed, 14 up-to-date
    The command "./gradlew build" exited with 0.
    $ ./gradlew sonarqube
    > Task :buildSrc:extractPrecompiledScriptPluginPlugins UP-TO-DATE
    > Task :buildSrc:generateExternalPluginSpecBuilders UP-TO-DATE
    > Task :buildSrc:compilePluginsBlocks UP-TO-DATE
    > Task :buildSrc:generatePrecompiledScriptPluginAccessors UP-TO-DATE
    > Task :buildSrc:configurePrecompiledScriptDependenciesResolver
    > Task :buildSrc:generateScriptPluginAdapters UP-TO-DATE
    > Task :buildSrc:compileKotlin UP-TO-DATE
    > Task :buildSrc:compileJava NO-SOURCE
    > Task :buildSrc:compileGroovy NO-SOURCE
    > Task :buildSrc:pluginDescriptors UP-TO-DATE
    > Task :buildSrc:processResources UP-TO-DATE
    > Task :buildSrc:classes UP-TO-DATE
    > Task :buildSrc:inspectClassesForKotlinIC UP-TO-DATE
    > Task :buildSrc:jar UP-TO-DATE
    > Task :buildSrc:assemble UP-TO-DATE
    > Task :buildSrc:compileTestKotlin NO-SOURCE
    > Task :buildSrc:pluginUnderTestMetadata UP-TO-DATE
    > Task :buildSrc:compileTestJava NO-SOURCE
    > Task :buildSrc:compileTestGroovy NO-SOURCE
    > Task :buildSrc:processTestResources NO-SOURCE
    > Task :buildSrc:testClasses UP-TO-DATE
    > Task :buildSrc:test NO-SOURCE
    > Task :buildSrc:validatePlugins UP-TO-DATE
    > Task :buildSrc:check UP-TO-DATE
    > Task :buildSrc:build UP-TO-DATE
    > Task :core:compileJava UP-TO-DATE
    > Task :data:compileJava UP-TO-DATE
    > Task :app:compileJava UP-TO-DATE
    > Task :app:processResources NO-SOURCE
    > Task :app:classes UP-TO-DATE
    > Task :app:compileTestJava UP-TO-DATE
    > Task :app:sonarqube
    Failed to check if project 'TTBMP_Cinehub' is bound
    > Task :app:sonarqube FAILED
    FAILURE: Build failed with an exception.
    * What went wrong:
      Execution failed for task ':app:sonarqube'.
    > java.lang.NullPointerException (no error message)
    * Try:
      Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    * Get more help at https://help.gradle.org
      BUILD FAILED in 42s
      5 actionable tasks: 1 executed, 4 up-to-date
      The command "./gradlew sonarqube" exited with 1.
      Done. Your build exited with 1.
    

Running your Gradle command with `–debug`` will probably give you a more meaningful error message to work with!

Thank you for the reply,
Please use this link to see the log of the the task with the debug flag set.

I’ve found in the log [org.sonarqube.gradle.SonarQubeTask] GET 401 https://sonarcloud.io/api/alm_integration/is_project_bound?project=TTBMP_Cinehub
but visiting the url in my browser gives me the json

{
"is_bound": true
}

Is this a token privileges problem?

:angry: the way that Gradle handles logging is never my friend…

If I can suggest one more trying to use --s rather than --debug and see if there’s a meaningful stacktrace.

That’s interesting – and where it seems to fail is where it would be trying to submit the analysis to SonarCloud. You might try generating a new token and using it just to see.