Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Is there a way to exclude packages from SonarQube(instrumented by gradle + sonar-runner) coverage reports(generated by jacoco) without excluding them completely from the project ?

Below is what i tried so far:

Version information

  • SonarQube 4.5.1
  • Gradle 2.2.
  • Jacoco configuration

    // JaCoCo test coverage configuration
    tasks.withType(Test) { task ->
        jacoco {
            append = false
            // excluded classes from coverage defined in above configuration
            excludes = excludedClasses()
        jacocoTestReport {
            doFirst {
                classDirectories = fileTree(dir: "${buildDir}/classes/main/").exclude(excludedClasses())
    

    Sonarrunner configuration

    Property setting to exclude package from Sonar analysis. Adding this to my configuration lead to the situation that the packages do not show-up at all in Sonar.

    property 'sonar.exclusions', excludedClasses().join(',')
    

    Property setting to exclude packages from jacoco. Setting this leads to the situation that packages are excluded from coverage analysis however show up having 0% which accumulates to bad total scores.

    property 'sonar.jacoco.exclusions', excludedClasses().join(',')
                    Gradle config: sonarqube { 	properties{ 		property 'sonar.coverage.exclusions', "**/com/some/package/*,**/com/other/package/Class*" 	} }
    – pavel
                    Dec 29, 2016 at 19:34
                    Correct working pattern is : sonarqube {   	properties {   	 		property 'sonar.coverage.exclusions', '*/model/, */commons/, */exception/, */controller/, */utils/, **/Test.java'  	}  }
    – Bharath B
                    Nov 20, 2019 at 21:47
    

    To combine @Mikalai's answer and @pavel's comment into something that's a bit easier to copy and paste:

    To exclude a package or class from all Sonar checks (coverage, code smells, bugs etc), add the following to build.gradle:

    sonarqube {
        properties {
            property 'sonar.exclusions', "**/com/some/package/**"
    

    To exclude a package or class from only Sonar code coverage checks, add the following to build.gradle:

    sonarqube {
        properties {
            property 'sonar.coverage.exclusions', "**/com/some/package/**"
                    I am running sonar v7.9 and configured the same in my build.gradle. Still sonar dashboard show these classes as 0% coverage. Is there any other config/setting required?
    – San
                    Jan 22, 2021 at 16:00
    

    If you are using Gradle 5+ with Kotlin DSL, you can exclude the files from coverage like this:

    // configure the SonarQube plugin
    sonarqube {
        val exclusions = listOf(
            "**/com/some/package/**",
            "**/all/files/under/package/*",
            "**/com/some/package/OneClass.kt"
        // exclude the directories only from coverage (but not from other analysis)
        // https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/
        properties {
            property("sonar.coverage.exclusions", exclusions)
    

    To exclude res, assets, custom packages and auto generated classes from sonar code coverage for android project.

    Create Exclusion list like below

    exclusionList = [
                //Res and Assets
                "src/main/res/**/*.*",
                "src/main/assets/**/*.*",
                //Auto-Generated
                '**/R.class',
                '**/R$*.class',
                '**/BuildConfig.*',
                '**/*Manifest.*',
                'android/**/*.*',
                'androidx/**/*.*',
                // excluded packages
                **/com/<your-package-path>/**/*]
    

    Provide the exclusion list to sonar coverage property

    property 'sonar.coverage.exclusions', exclusionList
    

    NOTE:

  • Keep the full path to exclude the full directory
  • **/* includes all the files and sub directories under the parent directory Refer Here
  • *Bean includes all the class names contains "Bean".
  • **/ starting with it covers the parent directories for the particular package.
  • Run Jacoco command and check sonar portal's coverage section after doing above changes.

    properties { property 'sonar.exclusions', "**/com/student/result/details/**", "**/com/customer/order/**", "**/com/student/**";

    This worked for me with gradle 6, sonarqube plugin 2.8

    property "sonar.coverage.exclusions" , "**/com/some/package/entity/**/*," + "**/com/some/package/config/**/*,**/com/some/package/dto/**/*,**/com/some/package/dao/**/*"

    NOTE: packages are comma separated but in a single string

    Most of the answer I have seen here didn't work for me, resulting on the following error:

    Execution failed for task ':sonarqube'.
    > Could not find method property() for arguments [sonar.coverage.exclusions, **/package/path/Class1**, **/package/path/Class2**, **/package/path/Class3**, **/package/path/Class4**] on object of type org.sonarqube.gradle.SonarQubeProperties.
    

    The reason is that sonar is asking for a string instead of several comma-separated strings as property value. So instead of:

      sonarqube {
        properties {
            property 'sonar.exclusions', "**/com/student/result/details/**",
                                         "**/com/customer/order/**",
                                         "**/com/student/**";
    

    The solution for me was:

      sonarqube {
        properties {
            property 'sonar.exclusions', "**/com/student/result/details/**,"+
                                         "**/com/customer/order/**,"+
                                         "**/com/student/**";
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.