简体   繁体   English

Gradle Jacoco 插件不生成报告

[英]Gradle Jacoco plugin not generating reports

Java 8 and Gradle 4.6 here. Java 8 和 Gradle 4.6 在这里。 I'm trying to configure my Gradle build to use the Jacoco Plugin but am having some difficulty.我正在尝试将我的 Gradle 构建配置为使用Jacoco 插件,但遇到了一些困难。 I've already got it working with Checkstyle and Findbugs, such that running ./gradlew clean build invokes the Checkstyle and Findbugs tasks because they're dependencies of the check task.我已经让它与 Checkstyle 和 Findbugs 一起使用,这样运行./gradlew clean build调用 Checkstyle 和 Findbugs 任务,因为它们是check任务的依赖项。

I'm now trying to get Jacoco working such that:我现在正试图让 Jacoco 工作,这样:

  1. It excludes my com.me.myapp.domain.model package and all of its contents;它不包括我的com.me.myapp.domain.model package 及其所有内容; and
  2. It fails my build if code coverage on the non-excluded classes falls below 70%;如果非排除类的代码覆盖率低于 70%,我的构建将失败; and
  3. Pass or fail, I want an HTML version of the Jacoco report generated under the build/ directory;通过或失败,我想要在build/目录下生成的 Jacoco 报告的 HTML 版本; and
  4. Ideally I could just use the same Gradle command invocation of ./gradlew clean build to get Jacoco working like this理想情况下,我可以使用相同的 Gradle 命令调用./gradlew clean build来让 Jacoco 像这样工作

My best attempt thus far:迄今为止我最好的尝试:

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

When I run ./gradlew clean build with the following build.gradle above (^^^) Jacoco does fail the build if my coverage is less than 70%.当我使用上面的以下build.gradle (^^^) 运行./gradlew clean build时,如果我的覆盖率小于 70%,Jacoco确实会失败。 However it does not generate any HTML report for me, which is not helpful at all in terms of fixing it.但是,它不会为我生成任何 HTML 报告,这在修复它方面根本没有帮助。

Any ideas?有任何想法吗?

Please note, that the Gradle Jacoco plugin does provide two totally unrelated functionalities: 请注意,Gradle Jacoco插件确实提供了两个完全不相关的功能:

If the plugin is applied together with the Java Plugin, a task of each of the mentioned types is created, namely jacocoTestReport and jacocoTestCoverageVerification . 如果将插件与Java插件一起应用,则将创建上述每种类型的任务,即jacocoTestReportjacocoTestCoverageVerification As you can see by the name both of them are associated with the test task. 如您所见,这两个名称均与test任务相关联。

However, none of those tasks is automatically included into the regular Gradle build lifecycle. 但是,所有这些任务都不会自动包含在常规Gradle build生命周期中。 The reason for not including the report task is simply because it's not necessary for actually building the actual software. 不包括报告任务的原因仅仅是因为它不需要实际构建实际的软件。 For the same reason, the javadoc task is not included in the build lifecycle (it might be when creating a javadoc jar). 出于相同的原因, build生命周期中不包括javadoc任务(它可能是在创建javadoc jar时出现的)。 The reason for not including the verification task is trickier, but let's simply quote the docs: 不包括验证任务的原因比较棘手,但让我们简单地引用以下文档:

The JacocoCoverageVerification task is not a task dependency of the check task provided by the Java plugin. JacocoCoverageVerification任务不是Java插件提供的check任务的任务依赖项。 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. Gradle的未来版本可能会改变行为。

You already solved this problem by adding check.dependsOn jacocoTestCoverageVerification to your build file. 您已经通过将check.dependsOn jacocoTestCoverageVerification添加到您的构建文件中来解决此问题。 This way, the code coverage will be checked with each build (and fail if not sufficient). 这样,每次构建都会检查代码覆盖率(如果不够,将失败)。 Now you want to generate the report in all builds, even if it fails due to unsufficient code coverage. 现在,您希望在所有版本中生成报告,即使由于代码覆盖范围不足而导致报告失败。 You need to ensure that the report is generated before the build might fail. 您需要确保在生成可能失败之前生成报告。 You could use: 您可以使用:

jacocoTestCoverageVerification.dependsOn jacocoTestReport

From the docu https://docs.gradle.org/current/userguide/jacoco_plugin.html The HTML report is enabled by default. 从文档https://docs.gradle.org/current/userguide/jacoco_plugin.html默认情况下启用HTML报告。 So it is not necessary to put html.enabled in the configuration. 因此,没有必要将html.enabled放入配置中。 Also, the docu shows how to specify a destination folder. 此外,文档显示了如何指定目标文件夹。 You could try to set it to some known folder to check if it works at all, eg html.destination file("${buildDir}/jacocoHtml") . 您可以尝试将其设置为某个已知文件夹,以检查它是否可以正常工作,例如html.destination file("${buildDir}/jacocoHtml") The default report directory where the HTML report should end up is $buildDir/reports/jacoco/test Setting the Jacoco report directory to some explicit value could also help to identify any configuration issues reportsDir . HTML报告应$buildDir/reports/jacoco/test结尾的默认报告目录为$buildDir/reports/jacoco/test将Jacoco报告目录设置为某个显式值也可以帮助识别任何配置问题reportsDir

I even executed the below command on my local terminal gradle --stacktrace -Dtest.ignoreFailures=true -DtestMaxParallelForks=1 -DtestForkEvery=1 -PstrictTestThresholds=true -Pjava.warnings.hide:reporting:test:reporting:jacocoTestReport -x:scala-lib:test我什至在本地终端 gradle --stacktrace -Dtest.ignoreFailures=true -DtestMaxParallelForks=1 -DtestForkEvery=1 -PstrictTestThresholds=true -Pjava.warnings.hide:reporting:test:reporting:jacocoTestReport -x:scala 上执行了以下命令-lib:测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM