如何使用 scoverage 与 Flink shadowjar 自定义 gradle 依赖配置

How to use scoverage with Flink shadowjar custom gradle dependency configuration

总结

我有一个 Gradle Flink Scala 项目,我正在尝试添加 Scoverage 报告,但由于自定义影子 jar 配置,compileScoverageScala 任务无法找到所有依赖项。

build.gradle

这是构建文件,它遵循 Flink 的 Gradle 示例。唯一的区别是我尝试添加 Scoverage。

// Here are the plugins I'm using
plugins {
    id 'scala'
    id 'application'
    // shadow plugin to produce fat JARs
    id 'com.github.johnrengelman.shadow' version '5.2.0'
    id "org.scoverage" version "4.0.1"
    id "com.github.maiflai.scalatest" version "0.25"
}
...

// Here's the custom configuration used to build the shadow jar without including the main Flink libraries
configurations {
    flinkShadowJar // dependencies which go into the shadowJar

    // always exclude these (also from transitive dependencies) since they are provided by Flink
    flinkShadowJar.exclude group: 'org.apache.flink', module: 'force-shading'
    flinkShadowJar.exclude group: 'com.google.code.findbugs', module: 'jsr305'
    flinkShadowJar.exclude group: 'org.slf4j'
    flinkShadowJar.exclude group: 'log4j'
}
...

// Here's where the custom configuration is added to the classpath, Scoverage isn't picking these up.
sourceSets {
    main.compileClasspath += configurations.flinkShadowJar
    main.runtimeClasspath += configurations.flinkShadowJar

    test.compileClasspath += configurations.flinkShadowJar
    test.runtimeClasspath += configurations.flinkShadowJar

    javadoc.classpath += configurations.flinkShadowJar
}

这是构建输出:

$ ./gradlew clean build

> Task :compileScala
Pruning sources from previous analysis, due to incompatible CompileSetup.

> Task :compileScoverageScala FAILED
Pruning sources from previous analysis, due to incompatible CompileSetup.
/Users/david.perkins/dev/wffh/flink-validation-fhir/src/main/scala/com/ibm/watson/health/foundation/hri/flink/FhirValidationJob.scala:6: object core is not a member of package com.ibm.watson.health.foundation.hri.flink
import com.ibm.watson.health.foundation.hri.flink.core.BaseValidationJob
                                                  ^

缺少的包被声明为 flinkShadowJar 依赖项。 compileScala 任务能够找到它,但 compileScoverageScala 不能。

有人知道是否有办法明确告诉 Scoverage 包含 flinkShadowJar 配置吗?我希望其他使用 Flink 的人之前已经 运行 了解过这个问题并且知道修复它的方法。

我从 Scoverage 团队那里得到了一些帮助:https://github.com/scoverage/gradle-scoverage/issues/138

将它添加到我的 sourceSets 声明中修复了它。

    scoverage.compileClasspath += configurations.flinkShadowJar
    scoverage.runtimeClasspath += configurations.flinkShadowJar