Sonarqube-Gradle on CI with Init Script

As a follow-up to my other post, I have a CI server where I want to store the credentials for SQ & DB authentication in an init script and have that init script always run whenever there is a new build. I want to also make work for both single and multi-module builds. Here’s what the CI admin came up with:

initscript {
	repositories {
	   	maven {url 'http://repo1'}
		maven {url 'http://repo2'}
     	}
	dependencies {
            	classpath "org.sonarqube.gradle:gradle-sonarqube-plugin:1.1"
     	}

}
allprojects{
  if (allprojects.size() > 1){
	subprojects{
		apply plugin: org.sonarqube.gradle.SonarQubePlugin
		sonarqube {
			properties {
				property "sonar.jdbc.username", System.getProperty('sonar.jdbc.username')
				property "sonar.jdbc.password", System.getProperty('sonar.jdbc.password')
				property "sonar.jdbc.url", System.getProperty('sonar.jdbc.url')
				property "sonar.jdbc.driverClassName", System.getProperty('sonar.jdbc.driverClassName')
				property "sonar.host.url", System.getProperty('sonar.host.url')
				property "sonar.login" , System.getProperty('sonar.login')
				property "sonar.password", System.getProperty('sonar.password')
			}
		}
	}
  }
   else {
	apply plugin: org.sonarqube.gradle.SonarQubePlugin
	sonarqube {
		properties {
			property "sonar.jdbc.username", System.getProperty('sonar.jdbc.username')
			property "sonar.jdbc.password", System.getProperty('sonar.jdbc.password')
			property "sonar.jdbc.url", System.getProperty('sonar.jdbc.url')
			property "sonar.jdbc.driverClassName", System.getProperty('sonar.jdbc.driverClassName')
			property "sonar.host.url", System.getProperty('sonar.host.url')
			property "sonar.login" , System.getProperty('sonar.login')
			property "sonar.password", System.getProperty('sonar.password')
		}
	}
}

}

For single module projects that run in CI and override properties like sonar.projecKey, sonar.projectName, etc. it works fine. However, in a multi-module build I was getting errors in the project build.gradle, where gradle was complaining about this:

Caused by: org.gradle.api.internal.MissingMethodException: Could not find method sonarqube() for arguments [sonarqube_an6bhftw8aiz4ofj0bkumpfjk$runclosure2@15e4f3a] on root project 'xxxxxxx.

When I run the sonarqube task locally on my machine without using the init script, it works fine, and publishes the results of the multi-module build as expected. I have tried to put the sonarqube{} closure for adding additional properties in the allprojects section, the subprojects section and outside of either, with no luck. I am running out ideas. Thank you…

I simplified you example and this is working for me:

initscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.5.0.2730"
    }
}

allprojects{
    apply plugin: org.sonarqube.gradle.SonarQubePlugin
    sonarqube {
        properties {
            property "sonar.host.url", "https://sonar.company.com"
            property "sonar.projectKey", "some.project"
            property "sonar.login", "......."
        }
    }
}

With gradle 7.5.1.

This allows me to:

  • use a newer java version (the SonarQube plugin requires java 11) for the gradle run that executes the :sonar task
  • to control the plugin version and configuration from a central place (the init.gradle) is shared across multiple project (no need to edit the build.gradle in each repo when the plugin has to be updated).