How to get Jenkins to exclude entire folders from code coverage? How to get Jenkins to exclude entire folders from code coverage? jenkins jenkins

How to get Jenkins to exclude entire folders from code coverage?


To exclude entire directories by changing the Jenkins JaCoCo plugin configuration you would need to add an entry to the 'Exclusions' field.

For instance, if you want to exclude any files under any directory named 'test' you would add the following exclusion:

**/test/**

Keep in mind that if you want to add multiple exclusions you have to separate each one by a comma and there can be no spaces (due to a bug with the plugin).

Here is my JaCoCo plugin configuration:Example JaCoCo Plugin Configuration


If you are using pipelines and Jenkinsfile you can use the following as an example of the settings (assumes gradle):

 stage("Check code quality and coverage") {          steps{           sh "./gradlew jacocoTestReport sonarqube -x check"                          step( [$class: 'JacocoPublisher',                  exclusionPattern: '**/*Exception*,**/*Configuration*,**/ApiApplication*,**/*Test*'] )          }    }

Of note here is the exclusionPattern is comma separated and NO SPACES between the multiple exclusion patterns.

The easiest way to see the full list of potential settings is to look at the code:

https://github.com/jenkinsci/jacoco-plugin/blob/master/src/main/java/hudson/plugins/jacoco/JacocoPublisher.java

And check out the @DataBoundSetter's

public JacocoPublisher() {    this.execPattern = "**/**.exec";    this.classPattern = "**/classes";    this.sourcePattern = "**/src/main/java";    this.inclusionPattern = "";    this.exclusionPattern = "";    this.skipCopyOfSrcFiles = false;    this.minimumInstructionCoverage = "0";    this.minimumBranchCoverage = "0";    this.minimumComplexityCoverage = "0";    this.minimumLineCoverage = "0";    this.minimumMethodCoverage = "0";    this.minimumClassCoverage = "0";    this.maximumInstructionCoverage = "0";    this.maximumBranchCoverage = "0";    this.maximumComplexityCoverage = "0";    this.maximumLineCoverage = "0";    this.maximumMethodCoverage = "0";    this.maximumClassCoverage = "0";    this.changeBuildStatus = false;    this.deltaInstructionCoverage = "0";    this.deltaBranchCoverage = "0";    this.deltaComplexityCoverage = "0";    this.deltaLineCoverage = "0";    this.deltaMethodCoverage = "0";    this.deltaClassCoverage = "0";    this.buildOverBuild = false;}


Exclude classes from sonar analysis by specifying sonar.jacoco.excludes parameter like this:

sonar.jacoco.excludes=*/exceptions/*:*/dto/*