Gradle Jacoco 插件不生成报告

Gradle Jacoco plugin not generating reports

Java 8 和 Gradle 4.6 这里。我正在尝试配置我的 Gradle 构建以使用 Jacoco Plugin 但我遇到了一些困难。我已经让它与 Checkstyle 和 Findbugs 一起工作,这样 运行ning ./gradlew clean build 调用 Checkstyle 和 Findbugs 任务,因为它们是 check 任务的依赖项。

我现在正在尝试让 Jacoco 工作:

  1. 它不包括我的 com.me.myapp.domain.model 包裹及其所有内容;和
  2. 如果未排除的 类 的代码覆盖率低于 70%,我的构建将失败;和
  3. 通过或失败,我想要在build/目录下生成的HTML版本的Jacoco报告;和
  4. 理想情况下 我可以使用相同的 Gradle 命令调用 ./gradlew clean build 让 Jacoco 像这样工作

迄今为止我最好的尝试:

plugins {
    id 'java-library'
    id 'checkstyle'
    id 'findbugs'
    id 'jacoco'
}

dependencies {
    compile(
        'org.hibernate:hibernate-core:5.0.12.Final'
        ,'com.fasterxml.jackson.core:jackson-core:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-databind:2.8.10'
        ,'com.fasterxml.jackson.core:jackson-annotations:2.8.0'
    )

    testCompile(
        'junit:junit:4.12'
    )
}

repositories {
    jcenter()
    mavenCentral()
}

checkstyle {
    config = rootProject.resources.text.fromFile('buildConfig/checkstyle/checkstyle.xml')
    toolVersion = '8.11'
}

tasks.withType(FindBugs) {
    reports {
        xml.enabled false
        html.enabled true
    }
}

findbugs {
    excludeFilter = file('buildConfig/findbugs/findbugs-exclude.xml')
}

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

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                exclude: [
                    'com/me/myapp/domain/model/**'
                ]
            )
        })
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.7
            }

            failOnViolation true
        }
    }
}

jacoco {
    toolVersion = "0.8.1"
}

// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification

当我 运行 ./gradlew clean build 与上面的 build.gradle (^^^) Jacoco 如果我的覆盖率是小于 70%。 但是它不会为我生成任何HTML报告,这对修复它一点帮助都没有。

有什么想法吗?

来自文档 https://docs.gradle.org/current/userguide/jacoco_plugin.html HTML 报告默认启用。所以没必要在配置里放html.enabled。此外,文档还显示了如何指定目标文件夹。您可以尝试将其设置为某个已知文件夹以检查它是否有效,例如html.destination file("${buildDir}/jacocoHtml")。 HTML 报告应该结束的默认报告目录是 $buildDir/reports/jacoco/test 将 Jacoco 报告目录设置为某个明确的值也有助于识别任何配置问题 reportsDir.

请注意,Gradle Jacoco 插件确实提供了两个完全不相关的功能:

如果插件与Java插件一起应用,将创建上述每种类型的任务,即jacocoTestReportjacocoTestCoverageVerification。正如您从名称中看到的那样,它们都与 test 任务相关联。

但是,这些任务中的 none 会自动包含在常规 Gradle build 生命周期中。不包括报告任务的原因很简单,因为它不是实际构建实际软件所必需的。出于同样的原因,javadoc 任务不包含在 build 生命周期中(可能是在创建 javadoc jar 时)。不包括验证任务的原因比较棘手,但我们简单引用文档:

The JacocoCoverageVerification task is not a task dependency of the check task provided by the Java plugin. There is a good reason for it. The task is currently not incremental as it doesn't declare any outputs. Any violation of the declared rules would automatically result in a failed build when executing the check task. This behavior might not be desirable for all users. Future versions of Gradle might change the behavior.

您已经通过将 check.dependsOn jacocoTestCoverageVerification 添加到您的构建文件中解决了这个问题。这样,每次构建都会检查代码覆盖率(如果不足够则失败)。现在您想要在所有构建中生成报告,即使它由于代码覆盖率不足而失败。您需要确保在构建可能失败之前生成报告。您可以使用:

jacocoTestCoverageVerification.dependsOn jacocoTestReport