Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude packages from Jacoco report using Sonarrunner and Gradle

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(',')
like image 801
fyr Avatar asked Nov 20 '14 07:11

fyr


People also ask

How do I exclude a package from JaCoCo?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated.

How do I exclude a package from sonar code coverage?

Ignore Code Coverage To do so, go to Project Settings > General Settings > Analysis Scope > Code Coverage and set the Coverage Exclusions property.


5 Answers

I have managed to exclude particular packages from coverage reports by using sonar.coverage.exclusions property in sonar-project.properties. Property is described in official documentation

like image 173
Mikalai Parafeniuk Avatar answered Sep 29 '22 22:09

Mikalai Parafeniuk


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/**"
    }
}
like image 34
Ben Watson Avatar answered Sep 27 '22 22:09

Ben Watson


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.

like image 25
takharsh Avatar answered Sep 27 '22 22:09

takharsh


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)
    }
}
like image 36
Stefan Zhelyazkov Avatar answered Sep 26 '22 22:09

Stefan Zhelyazkov


exclude multiple module or class from Sonarqube. add below code to build.gradle:

example : package name= com.student.result.detail , com.customer.order ,com.student

  sonarqube {
    properties {
        property 'sonar.exclusions', "**/com/student/result/details/**",
                                     "**/com/customer/order/**",
                                     "**/com/student/**";
        }
    }
like image 4
Parth Avatar answered Sep 25 '22 22:09

Parth