Sonar analysis across multi modules project

Information

org.sonarqube version: 5.1.0.4882
gradle version: 8.4
Java 17

Scanner is executed by below command

./gradlew  testCodeCoverageReport sonar -Dsonar.host.url=https://our-server-url/ -Dsonar.token=some-token -Dsonar.projectVersion=1

Sonar server deployed in our datacenter

Project structure
Root project ‘our-service’
±-- Project ‘:app’
±-- Project ‘:projectA’
-– Project ‘:projectB’

Here is my buildSrc/build.gradle.kts

plugins {
     `kotlin-dsl`
 }
 
 repositories {
     gradlePluginPortal()
 }
 dependencies {
      implementation("org.sonarqube:org.sonarqube.gradle.plugin:5.1.0.4882")
}

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

import org.gradle.accessors.dm.LibrariesForLibs
import org.sonarqube.gradle.SonarTask

plugins {
    java
    id("com.diffplug.spotless")
    jacoco
    id("org.sonarqube")
}

tasks.named<SonarTask>("sonar") {
    if (project.name == "projectA") {
        sonar {
            isSkipProject = true
        }
    } else {
        sonar {
            properties {
                property("sonar.sources", "src/main")
                property("sonar.tests", "src/test")
                property("sonar.language", "java")
                property("sonar.java.source", "17")
                property("sonar.junit.reportPaths", "build/test-reports")
                property("sonar.coverage.exclusions", "**/build/**")
            }
        }
    }
}

In Sonar we have setup a single project and expect all modules to be analyzed in it. The question is where should below properties defined?

sonar {
    properties {
        property("sonar.projectKey", "our-service")
        property("sonar.projectName", "our-service")
        property("sonar.coverage.jacoco.xmlReportPaths", "${project.projectDir}/app/build/reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml")
    }
}

If I add them to buildSrc/src/main/kotlin/com.test…java-common-conventions.gradle.kts it seems that only projectB information is recorded by Sonar – unitTest count and codeCoverage. If add them to buildSrc/build.gradle.kts they are ignored and each module creates its own sonar project, which makes sense to me.

where should it be defined?

I should add if I follow the mulit-module setup and define everything in the root build.grade.kts as below everything executes correctly

sonar {
    properties {
        property("sonar.projectKey", "our-service)
        property("sonar.projectName", "our-service")
        property("sonar.coverage.jacoco.xmlReportPaths", "${project.projectDir}/app/build/reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml")
    }
}

project(":moduleA") {
    sonar {
        isSkipProject = true
    }
}

subprojects {
         sonar {
             properties {
                 property("sonar.sources", "src/main")
                 property("sonar.tests", "src/test")
                 property("sonar.language", "java")
                 property("sonar.java.source", "17")
                 property("sonar.junit.reportPaths", "build/test-reports")
                 property("sonar.coverage.exclusions", "**/build/**")
             }
         }
}

Hi,

Welcome to the community!

I don’t understand what the question is. It seems like you’ve already figured it out…?

 
Ann

is it not possible to have the configuration in buildSrc and it must reside in the root build.gradle.kts?

Hi,

Per the docs,

To analyze a project hierarchy, apply the SonarQube plugin to the root project of the hierarchy. Typically (but not necessarily) this will be the root project of the Gradle build.

 
HTH,
Ann

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