Exclude packages from Jacoco report using Sonarrunner and Gradle

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

Below I am trying to do this:

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

Set properties to exclude a package from sonar analysis. Adding this to my configuration results in packages not being displayed at all in Sonar.

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



Setting property to exclude packages from jacoco. Setting this leads to a situation where packets are excluded from the analysis of coverage, but find that they have 0%, which accumulate to poor final grades.

 property 'sonar.jacoco.exclusions', excludedClasses().join(',') 
+8
gradle jacoco sonar-runner
Nov 20 '14 at 7:10
source share
4 answers

I managed to exclude certain packages from coverage reports using the sonar.coverage.exclusions property in sonar-project.properties. The property is described in the official documentation.

+18
Dec 12 '14 at 14:45
source share

To combine @Mikalai answer and @pavel comment into something that is a little easier to copy and paste:

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

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

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

 sonarqube { properties { property 'sonar.coverage.exclusions', "**/com/some/package/**" } } 
+4
Feb 27 '19 at 15:04
source share
 sonarqube { properties{ property 'sonar.exclusions', "**/com/package/name/*/*" }} 

The above configuration worked for me. It excludes the mentioned package from all categories, such as code smell, bugs, vulnerabilities and test coverage.

0
Mar 29 '19 at 7:26
source share

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

Create an exception list as shown 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 a list of exceptions for the sonar coverage object

 property 'sonar.coverage.exclusions', exclusionList 

Note:

  • Keep full path to exclude full catalog
  • ** / * includes all files and subdirectories in the parent directory . See here
  • * Bean includes all class names, contains "Bean".
  • ** / starting with it covers parent directories for a specific package.

Run the Jacoco team and check the sonar portal coverage section after making the above changes.

0
Sep 18 '19 at 10:09 on
source share



All Articles