Search code examples
javagradlesonarqubemulti-module

Sonar with multi-module gradle project


I have java and kotlin based multi module gradle project. I am trying to setup sonar analysis for the same. I configured sonar at root project and ran analysis with CircleCI. The result in sonarcloud gets for only one of the sub-project.

My project structure is as below:

  • projectA/build.gradle
  • ProjectB/build.gradle
  • ProjectC/build.gradle
  • build.gradle

Here is my root build.gradle.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
    }
}

ext {
    springCloudVersion = 'Hoxton.SR1'
    springBootVersion = '2.2.4.RELEASE'
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

allprojects {
    group = 'com.organiz'
    version = '1.0.0-SNAPSHOT'

    repositories {
        mavenCentral()
        maven {
            //        url ="s3url"  //   only for releases
            url ="s3url"  //  only for snapshots
            credentials(AwsCredentials) {
                accessKey project.accessKey
                secretKey project.secretKey
            }
        }
    }
}

subprojects {

    apply plugin: 'java'
    apply plugin: 'kotlin'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'idea'
    apply plugin: 'org.sonarqube'
    sourceCompatibility = '1.8'

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
        implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
        implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
        implementation("com.h2database:h2:1.4.200")
    }

    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "someurlhere"
        }
        someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }

    test {
        useJUnitPlatform()
    }

    compileKotlin {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    sonarqube {
         properties {
    property "sonar.projectKey", "projectKey"
    property "sonar.organization", "org"
    property "sonar.host.url", "https://sonarcloud.io"
    property "sonar.verbose", "true"
  }
}
}

project(':project1') {
    dependencies {
        implementation project(":common")
    }
}

project(':project2') {
    dependencies {
        implementation project(":common")
    }
}

Solution

  • Need to include sonarqube outside the subproject block and then all the sub modules will be analysed and report will be exported.

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
            classpath 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
            classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
            classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
        }
    }
    
    ext {
        springCloudVersion = 'Hoxton.SR1'
        springBootVersion = '2.2.4.RELEASE'
    }
    
    configurations.all {
        resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    }
    
    allprojects {
        group = 'com.organiz'
        version = '1.0.0-SNAPSHOT'
    
        repositories {
            mavenCentral()
            maven {
                //        url ="s3url"  //   only for releases
                url = "s3url"  //  only for snapshots
                credentials(AwsCredentials) {
                    accessKey project.accessKey
                    secretKey project.secretKey
                }
            }
        }
    }
    
    apply plugin: 'org.sonarqube'
    sonarqube {
        properties {
            property "sonar.projectKey", "projectKey"
            property "sonar.organization", "org"
            property "sonar.host.url", "https://sonarcloud.io"
            property "sonar.verbose", "true"
        }
    }
    
    subprojects {
    
        apply plugin: 'java'
        apply plugin: 'kotlin'
        apply plugin: 'org.springframework.boot'
        apply plugin: 'idea'
        sourceCompatibility = '1.8'
    
        dependencies {
            implementation 'org.springframework.boot:spring-boot-starter'
            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
            }
            implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            implementation('someappcommon:1.0.0-SNAPSHOT') { changing = true }
            implementation("com.h2database:h2:1.4.200")
        }
    
        repositories {
            mavenCentral()
            jcenter()
            maven {
                url "someurlhere"
            }
            someappMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
        }
    
        test {
            useJUnitPlatform()
        }
    
        compileKotlin {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
        compileTestKotlin {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    project(':project1') {
        dependencies {
            implementation project(":common")
        }
    }
    
    project(':project2') {
        dependencies {
            implementation project(":common")
        }
    }