How to setup sonar for multi-module gradle project

Hi,

We am working on splitting out our gradle build for a multi module gradle project. Previously we used to build the whole repo together using a build.gradle at the top level and sonarqube works fine but now we want to build submodules in the repo with their own build.gradle files. The issue we are hitting is with integrating sonarqube for submodules.

  1. When I am keeping sonarqube property in top level and nested level build.gradle, I am hitting this error:
    File abc/src/main/java/abc.java can’t be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

  2. When I removed sonarqube property from the top level and only having it at the nested level, I am getting this error:
    sonarqubeSonarQube server [http://localhost:9000] can not be reached

    even though sonar.host.url is specified in nested level build.gradle

I am trying to build my submodule with this command:
gradle :abc:build sonarqube -Dsonar.login=USERNAME -Dsonar.password=PASSWORD -Dsonar.branch.name=​test -Dsonar.projectVersion=test -Dsonar.sources=abc/src/main/java

Repo structure:

project:
	build.gradle
	abc/:
		build.gradle
		src/main/java
		.....
	xyz/:
		build.gradle
		src/main/java
		.....

Can someone please help me in setting sonarqube correctly for our submodule builds?

Hi @Nitish,

Welcome to the Community!

Yes, multi-module Gradle projects can be a bit painful to set up. I’m assuming you’re using a Gradle multi-project build? I experimented with this a few months ago; here’s what worked for me.

In the top-level build.gradle I have two blocks:

sonarqube {
    properties {
        property "sonar.host.url", "http://<hostname>:<port>"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.projectName", "Example Gradle multi-project"
        property "sonar.projectKey", "my-example-gradle-multi-project"
    }
}

...

subprojects {
    version = '1.0'
    sonarqube {
        properties {
            property "sonar.sources", "src/main"
            property "sonar.tests", "src/test"
            // I was testing with JaCoCo, so the next line is not needed if you're using some other tool
            property "sonar.coverage.jacoco.xmlReportPaths", "../build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
            property "sonar.host.url", "http://<hostname>:<port>"
        }
    }
}

Then in the top-level settings.gradle file I have:

rootProject.name = 'example_gradle_multi_project'
include 'abc'
include 'xyz'

Nothing needed at the abc/build.gradle or xyz/build.gradle level :slight_smile:

Let me know whether this works for you.

Regards,

Cameron.