从 Jenkins 的 Jacoco 覆盖范围中排除包

Exclude package from Jacoco coverage in Jenkins

由于我的 Jenkins 管道失败,正在尝试从覆盖率报告中排除包。我有一个包含所有 POJO:s 的子项目。我不想为所有这些编写 uittest。因此,他们会降低 branch/line 覆盖范围,以至于覆盖范围将低于阈值并使我的构建失败。

应该可以排除一些包,但我无法让它工作。

我有以下 jacoco.gradle 文件:

apply plugin: 'jacoco'  
apply plugin: 'java'  
jacoco {  
  toolVersion = "0.8.2"  
}  

jacocoTestReport {  
reports {  
  xml.enabled true  
  csv.enabled false  
  html.enabled true  
}  

afterEvaluate {  
  classDirectories = files(classDirectories.files.collect {  
     fileTree(dir: it, exclude: '**xxx/yyy/zzz/**')  
  })  
}  

task coverage { dependsOn 'jacocoTestReport' }  

check.dependsOn 'jacocoTestReport'  

我的 sonar.gradle 文件中的以下内容:

apply plugin: 'org.sonarqube'  

sonarqube {  
properties {  
  property "sonar.forceAnalysis", "true"  
  property "sonar.forceAuthentication", "true"  
  property "sonar.java.coveragePlugin", "jacoco"  
  property "sonar.login", ""  
  property "sonar.password", ""  
 }  
}  

subprojects {  
 sonarqube {  
  properties {  
     property "sonar.jacoco.reportPaths", 
 "$buildDir/reports/jacoco/allTests.exec"
     property "sonar.junit.reportsPath", "$buildDir/test-results/test"  
  }  
 }  
}  

task sonar { dependsOn 'sonarqube' }  

在我的build.gradle中:

apply from: 'gradle/sonar.gradle'  
...  
apply plugin: 'java'  
...  
subprojects {  
  apply from: '../gradle/jacoco.gradle'   
  ...  
}  

最后来自我的 Jenkins 文件:

step([$class: 'JacocoPublisher', changeBuildStatus: false, 
 exclusionPattern: '**/*Test*.class', inclusionPattern: 
 '**/*.class', minimumBranchCoverage: '80', sourcePattern: '**/src'])  

try {  
 dir(BUILD_DIR) {  
 sh './gradlew sonar'  
}  

但在 Jenkins 的覆盖率报告中仍然会生成 xxx.yyy.zzz!

终于成功了!关键是 JacocoPuplisher。

step([$class: 'JacocoPublisher', changeBuildStatus: false, exclusionPattern: 
'**/xxx/yyy/zzz/**/*.class, **/*Test*.class', inclusionPattern: '**/*.class', 
minimumBranchCoverage: '80', sourcePattern: '**/src'])

这是我让它与 Jenkins 一起工作的唯一方法。

设置 sonar.coverage.exclusion 或使用上面的 afterEvalueate 无效。

我能够通过直接添加到插件调用参数来完成 file/package 从 jacoco() Jenkins 管道插件(在我们的标准 Jenkinsfile 中被调用 pipeline { ... post { ... always { )中排除像这样:

...
    post {
        always {
            ...
            // Change the exclusion for the jacoco() jenkins plugin to exclude testutils package. 
            jacoco(exclusionPattern: '**/testutils/**,**/*Test*.class')

似乎整个主题中最大的困惑之一是有一个 jacocoTestReport gradle 目标,它有自己的排除语法,然后这个 jacoco() Jenkins 管道似乎完全独立的任务。声纳覆盖排除看起来像是第三件事?