# How to configure multi-module gradle SonarQube scan to only scan built artifacts

**URL:** <https://community.sonarsource.com/t/how-to-configure-multi-module-gradle-sonarqube-scan-to-only-scan-built-artifacts/143690>\
**Category:** SonarQube Server / Community Build\
**Tags:** multi-module, gradle, sonarqube\
**Created:** [June 30, 2025, 8:37am UTC](https://community.sonarsource.com/t/how-to-configure-multi-module-gradle-sonarqube-scan-to-only-scan-built-artifacts/143690 "2025-06-30T08:37:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akshay\_Srivastava](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/akshay_srivastava/32/66573_2.png) [@Akshay\_Srivastava](https://community.sonarsource.com/u/Akshay_Srivastava)\
**Post date:** [June 30, 2025, 8:37am UTC](https://community.sonarsource.com/t/how-to-configure-multi-module-gradle-sonarqube-scan-to-only-scan-built-artifacts/143690/1 "2025-06-30T08:37:52Z")

</div>

We are using the following SonarQube:

- Version: Enterprise Edition `v2025.2`
- Deployment: GKE
- Gradle Sonar Plugin: `6.0.1.5171`

Our project is a multi-module gradle repo with the following layout:

```auto
├── build.gradle
├── golang_services
│ ├── golang_service1
│ │ ├── build.gradle
│ ├── golang_service2
│ │ ├── build.gradle
├── java_services
│ ├── java_services1
│ │ ├── build.gradle
│ │ └── src
│ │ ├── main
│ │ └── test
│ ├── java_services2
│ │ ├── build.gradle
│ │ ├── build
│ │ │ ├── libs
│ │ │ │ ├── java_services2-0.1.0.jar
│ │ └── src
│ │ ├── main
│ │ └── test
│ ├── java_services3
│ │ ├── build.gradle
│ │ └── src
│ │ ├── main
│ │ └── test
│ ├── java_services4
│ │ ├── build.gradle
│ │ └── src
│ │ ├── main
│ │ └── test
├── shared
│ ├── build.gradle
│ ├── build
│ │ ├── libs
│ │ │ └── shared-1.0.0.jar
│ └── src
│ ├── main
│ └── test

```

We have multiple golang and java services with their own `build.gradle` files and a root `build.gradle`

There is a `shared` Java module is used by the golang\_services and java\_services.

As part of our CI process, we build only the modules that have changed in the PR and it’s dependent services.

So if `java_services2` was updated, then the following modules are built:

- `java_services2`
- `shared`

We want to run a sonar scan only on the modules that were updated, and **not on the entire multi-module project**. For this, we use a custom logic which checks for the built artifacts and then skips the projects that did not have a JAR.

root `build.gradle`

```auto
subprojects {
    apply plugin: 'org.sonarqube'

    afterEvaluate { project ->
        def hasJars = hasBuiltJars(project)

        if (!hasJars) {
            sonar {
                skipProject = true
            } 
        } else {
        }
    }
  }
  
sonar {
    properties {
        property "sonar.projectName", "projectName"
        }
}

```

The approach we’re trying right now is to:

- Get the list of modules that have built artefacts (`shared-1.0.0.jar` and `java_services2-0.1.0.jar`)
- In the `subprojects` section, skip the modules that do not have JARs
- Configure sonarqube to focus on the .java and .class files for `shared` and `java_services2` modules

However gradle sonarqube is failing with the following error

```auto
Executed tasks: :sonar
File shared/src/test/java/shared/spring/services/ServiceImplTest.java can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

```

From my understanding, seems that the sonar scanner is scanning the `shared` module twice

- when scanning the `shared` module
- when scanning the `java_services2` module, which has implemented the `shared` module

How can i configure SonarQube to do this selective scan only on the built artefacts and their files?

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [July 2, 2025, 8:10am UTC](https://community.sonarsource.com/t/how-to-configure-multi-module-gradle-sonarqube-scan-to-only-scan-built-artifacts/143690/2 "2025-07-02T08:10:12Z")

</div>

Hey there.

> [@Akshay\_Srivastava](#):
>
> We want to run a sonar scan only on the modules that were updated, and **not on the entire multi-module project**.

Have you considered making sure you’re taking advantage of [incremental analysis](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/incremental-analysis/introduction/) instead of getting clever with excluding projects from the analysis.

In any case, I think that with your strategy, you just need to be more explicit about which folders should be indexed on each module, [rather than relying on the defaults](https://docs.sonarsource.com/sonarqube-server/2025.1/analyzing-source-code/scanners/sonarscanner-for-gradle/#additional-defaults-for-projects-with-javabase-or-java-plugin-applied). This probably looks like this.

```auto
sonar {
  properties {
    property "sonar.sources", "src/main/java"
    property "sonar.tests", "src/test/java"
  }
}

```

If that doesn’t help, I’d be happy to help you if you can provide a small reproducer project that exhibits the issue!
