# Sonar analysis across multi modules project

**URL:** <https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776>\
**Category:** SonarQube Server / Community Build\
**Tags:** gradle, java, coverage\
**Created:** [August 16, 2024, 1:26am UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776 "2024-08-16T01:26:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirnyi](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mirnyi](https://community.sonarsource.com/u/mirnyi)\
**Post date:** [August 16, 2024, 1:26am UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776/1 "2024-08-16T01:26:32Z")

</div>

**Information**

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

Scanner is executed by below command

```auto
./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`

```auto
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.

```auto
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?

```auto
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

```auto
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/**")
             }
         }
}

```

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [August 19, 2024, 5:29pm UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776/2 "2024-08-19T17:29:20Z")

</div>

Hi,

Welcome to the community!

> [@mirnyi](#):
>
> I should add if I follow the mulit-module setup and define everything in the root build.grade.kts as below everything executes correctly

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

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![mirnyi](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mirnyi](https://community.sonarsource.com/u/mirnyi)\
**Post date:** [August 22, 2024, 12:28pm UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776/3 "2024-08-22T12:28:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [August 22, 2024, 12:29pm UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776/4 "2024-08-22T12:29:54Z")

</div>

Hi,

Per [the docs](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/scanners/sonarscanner-for-gradle/#analyzing-multi-project-builds),

> 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.

&nbsp;  
HTH,  
Ann

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [August 29, 2024, 12:30pm UTC](https://community.sonarsource.com/t/sonar-analysis-across-multi-modules-project/120776/5 "2024-08-29T12:30:28Z")

</div>

Closed on August 29, 2024, 12:30pm UTC

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