How to configure Maven child projects as SonarQube projects (not aggregated)

I have a large multi-module maven project. When I run the maven-sonar-plugin, it is aggregating all the results under one single large project.

I would really like to have each of the child projects be their own SonarQube project. How do I configure maven to accomplish this?

Welcome :slight_smile:

if you do not want Sonarqube to create an aggregated project for your Maven multi-module project,
you need to ‘dir’ into each submodule and start a separate analysis for it.

here is some - very simplified - snippet from a Jenkins pipelline

if(isMultiModuleBuild) {
  println "Maven Multimodule Build, Modules: ${pom.getModules().join(',')}"
  pom.getModules().each {
  dir("$it") {
    subPom = readMavenPom(file: "pom.xml")
    nodejs(configId: xxx, nodeJSInstallationName: xxx) {
      withSonarQubeEnv(sonarenv) {
        withMaven(jdk: xxx, maven: xxx, globalMavenSettingsConfig: xxx) {
          sh "mvn ${sonarDebug ? '-X' : ''} sonar:sonar -Dsonar.branch.name=$BRANCH_NAME"
        }
      }
    }
  }
}

Gilbert

Gilbert,

Thank you for the response, I guess your response confirms what I was seeing is that there is no real way to easily integrate SonarQube into an existing large Maven project and have it analyze them as separate SonarQube projects. I was really hoping to be able to add a profile at the parent level and just call my sonar profile from the repo parent with the fictious option aggregate=false
Note: the below is wish, the aggregate flag does not exist

mvn verify -P sonar -Dsonar.aggregate=false

Based on your code, I am guessing you only have one parent project with modules? Our repo has multiple levels of parent projects so I think my script would need to be able to skip those.

Also, thank you for sharing your Jenkins pipeline, we use GitLab, you inadvertently answered another question I had about all my projects reporting as main even when on a different branch, looks like I need to set the sonar.branch.name to be able to get could analysis as well.

Thank you again for your help.

You might set a feature request for a property like sonar.aggregate, you’ll get my vote for that.