How to use with Gradle Convention Plugins?

Must-share information (formatted with Markdown):

  • which versions are you using: SonarQube Community Build
  • how is SonarQube deployed: locally on my Linux machine
  • what are you trying to achieve: just want to test it out
  • what have you tried so far to achieve this: I Googled everywhere

I have 5 separate Gradle modules in my Android project. I wish to apply sonar to all of them. Here is what I have tried so far.

I am using Version Catalogs. For Sonar, I have the following entries:

[versions]
sonarQubeVersion = "6.0.1.5171"
sonarScannerVersion = "3.3"

[libraries]
sonarGradlePluginLibrary = { group = "org.sonarsource.scanner.gradle", name = "sonarqube-gradle-plugin", version.ref = "sonarScannerVersion" }

[plugins]
sonarQubePlugin = { id = "org.sonarqube", version.ref = "sonarQubeVersion" }

In my project’s build.gradle.kts:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.sonarQubePlugin) apply false
}

In my convention build.gradle.kts:

dependencies {
    compileOnly(libs.sonarGradlePluginLibrary)
}

In my custom plugin:

class AppPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                alias(libs.plugins.sonarQubePlugin)
            }
            configureSonar()
        }
    }
}

Where configureSonar is:

import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.sonarqube.gradle.SonarQubeExtension

internal fun Project.configureSonar() {
    pluginManager.apply(libs.plugins.sonarQubePlugin.get().pluginId)

    val sonarExtension = extensions.findByType<SonarQubeExtension>()
    sonarExtension?.apply {
        properties {
            property("sonar.projectName", "Project Name")
            property("sonar.projectKey", "Project-Name")
            property("sonar.language", "kotlin")
            property("sonar.sourceEncoding", "UTF-8")
            property("sonar.host.url", "http://localhost:9000")
            property("sonar.token", "my-token")
        }
    } ?: println("there was a problem applying Sonar")
}

Now, when I refresh Gradle, I get the following error regarding the SonarQubeExtension import:

Unable to load class 'org.sonarqube.gradle.SonarQubeExtension'
org.sonarqube.gradle.SonarQubeExtension

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

I have tried refreshing all gradle imports but still the same error. Can someone tell me where I am making a mistake?

Hey there.

You should need none of this. org.sonarsource.scanner.gradle:sonarqube-gradle-plugin is a very old reference that was succeeded by org.sonarqube.

Can you first try removing any references to this old library and see if you get further?

I also want to point out that typically, you don’t need to do any of this to accomplish what you’re after.

Unless I’m missing something, you should simply be able to add this to your root module. which will automatically orchestrate the analysis of any submodules.

plugins {
  id "org.sonarqube" version "6.0.1.5171"
}

What efficiencies are you hoping to gain by doing it the way you’re doing it?

Update. The following setup seems to work. I just put the same code in my project level build.gradle.kts and now it syncs just fine and I can use ./gradlew sonar

subprojects {
    configureSonar()
}

fun Project.configureSonar() {
    apply<SonarQubePlugin>()
    configure<SonarExtension> {
        properties {
            property("sonar.projectName", "Project Name")
            property("sonar.projectKey", "Project-Name")
            property("sonar.language", "kotlin")
            property("sonar.sourceEncoding", "UTF-8")
            property("sonar.host.url", "http://localhost:9000")
            property("sonar.token", "my-token")
        }
    }
}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.