如何使用Gradle从JaCoCo报告中排除文件

mrphzbgm  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(243)

我正在尝试排除多模块Sping Boot 项目中的一些文件。我尝试在test中使用JaCoCo的excludes扩展,并在jacocoTestReport中添加afterEvaluate块。这两个选项都不起作用。Gradle版本是8.1.1,Sping Boot 版本是3.1.2。有人能帮助我吗?下面是主gradle文件:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

plugins {
    id "org.sonarqube" version "4.2.1.3168"
}

group = "${group}"
version = "${version}"

subprojects {
    apply {
        plugin "java"
        plugin "org.springframework.boot"
        plugin "io.spring.dependency-management"
        plugin 'jacoco-report-aggregation'
    }

    java {
        sourceCompatibility = JavaVersion.VERSION_17
    }

    dependencies {
        //removed for brevity
    }

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

    tasks.named('test') {
        useJUnitPlatform()
//        jacoco {
//            excludes = [
//                    "**/config**"
//            ]
//        }
    }

    bootJar {
        enabled = false
    }

    jacoco {
        toolVersion = "${jacocoVersion}" //0.8.9
    }

    jacocoTestReport {
        reports {
            xml.required = false
            csv.required = false
            html.required = true
        }
    }

    jacocoTestReport {
        dependsOn test

        afterEvaluate {
            classDirectories.setFrom(files(classDirectories.files.collect {
                fileTree(dir: it, exclude: [
                        "**/config**"
                ])
            }))
        }
    }
    
}

字符串

daupos2t

daupos2t1#

我添加了一个testCodeCoverageReportjacocoTestCoverageVerification任务到build.gradle,它现在工作正常。
我还将这个配置移到了包含我的测试用例和main方法的模块中。
更新jacoco配置:

def jacocoExclude = [
        '**/config/**'
]

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report

    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: jacocoExclude)
        }))
    }
}

jacocoTestCoverageVerification {
    afterEvaluate {
        getClassDirectories().setFrom(classDirectories.files.collect {
            fileTree(dir: it, exclude: jacocoExclude)
        })
    }
}

testCodeCoverageReport {
    afterEvaluate {
        getClassDirectories().setFrom(classDirectories.files.collect {
            fileTree(dir: it, exclude: jacocoExclude)
        })
    }
}

字符串

相关问题