Multi-project with Gradle. How to separate multi-project in individual project

  • SonarQube version : 3.2.0
  • Sonar-scanner : 3.1.0

Hi, I’m i new to sonarqube and was task to add sonarcloud to our bitbucket pipeline. I decided that i wanted to be able to run it locally first before adding it to the pipeline. I have a gradle multi-project structure and i want every gradle subproject to be their own project. ATM, if i run the scan and go to localhost:9000, i see project MyApp and the code of subapp1 & subapp2 are included in that project. What i want is to have a project for each so that we see subapp1 and subapp2 as two projects.

This is my project structure

MyApp
|—subapp1
| |—gradle.build
|—subapp2
| |—gradle.build
|—
|—build.gradle
|—setting.gradle

myApp/settings.gradle

rootProject.name = 'myApp'
include 'subapp1', 'subapp2'

myapp/build.gradle

sonarqube {
    properties {
        property "sonar.projectKey", "subapp1"
        property "sonar.host", "http://localhost:9000"
        property "sonar.name", "subapp1"
        property "sonar.login", "myToken"
    }
}

myapp/subapp1/build.gradle

sonarqube {
    properties {
        property "sonar.projectKey", "subapp1"
        property "sonar.host", "http://localhost:9000"
        property "sonar.name", "subapp1"
        property "sonar.login", "myToken1"
    }
}

myapp/subapp2/build.gradle

sonarqube {
    properties {
        property "sonar.projectKey", "subapp2"
        property "sonar.host", "http://localhost:9000"
        property "sonar.name", "subapp2"
        property "sonar.login", "myToken2"
    }
}

I used this article as a guide: SonarScanner for Gradle | SonarQube Docs

I think it looks into MyApp/build.gradle first and skip my MyApp/subapp1/build.gradle & MyApp/subapp2/build.gradle. I think that’s the case because if i comment out the sonarqube part of the MyApp/build.gradle i get the following error.

Execution failed for task ':sonarqube'.
> Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.login or other credentials in sonar.login and sonar.password.

ATM i use the following command at the root of the project to start the scan ./gradlew sonarqubeSo, my question is, how can i run analysis on subapp1 & subbapp2 as 2 separate project? I feel like i’m not far off.

Hi @SaveurDeLime ,

Welcome to SonarSource Community! :sonarsource:

Are you really using SonarQube version : 3.2.0? Can you confirm what version is listed at the bottom of the SonarQube UI. It should show your version number.

You almost have it correct. Take a look at my example of “gradle-multimodule-coverage” in sonar-scanning-examples repo. You will need to do a few modifications to have separate SonarQube projects analyzed within one large “mono-repo”-like Gradle project:

In root build.gradle:

  • Remove/comment out the id ‘org.sonarqube’ version ‘3.0’ from plugins block
  • Remove/comment out the subprojects block
  • Remove/comment out the reference to sonar.gradle: apply from: “$project.rootDir/sonar.gradle”

In application/build.gradle:

  • Add id ‘org.sonarqube’ version ‘3.0’ to plugins block
  • Add this to the bottom of the file:
apply plugin: "org.sonarqube"

sonarqube {
    properties {
        // Insert other SonarQube properties here
        property 'sonar.projectName', 'APP-ONLY'
        property 'sonar.projectKey', 'APP-ONLY'
    }
}

In list/build.gradle or utilities/build.gradle:

(do the same as I mentioned in application/build.gradle except change the sonar.projectName, sonar.projectKey, etc.)


From command line, to build and do sonar scan analysis, you can pick just the single module such “list” module to build and do Sonar analysis like so:

./gradlew clean :list:build :list:sonarqube -Dsonar.host.url=<INSERT-SONARQUBE-HOSTNAME> -Dsonar.login=<INSERT-USER-TOKEN>

Give that a try and let me know what you think.


By the way, this error you get is because you did not pass the sonar.login and/or sonar.password. For example, if you login with username and password you would pass it to your gradlew command like so:

./gradlew clean build codeCoverageReport -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=password sonarqube

or if you are using a user token:

./gradlew clean build codeCoverageReport -Dsonar.host.url=http://localhost:9000 -Dsonar.login=cb7db4ed2541e198fa2ce4e4f299482fcc132c2 sonarqube

Please review User Tokens.

Joe

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