【问题标题】:How to add source code to Jacoco report in Gradle如何在 Gradle 中将源代码添加到 Jacoco 报告
【发布时间】:2016-06-15 09:27:25
【问题描述】:

在 Gradle 中,当您有多个项目时,您可能希望从一个项目生成 Jacoco 测试覆盖率报告,并将其他项目的类显示在报告中。

为此,Gradle 2.14 中的 JacocoReport documentation 列出了两对指令:

  • sourceDirectories & classDirectories
  • additionalSourceDirs & additionalClassDirs

但是,对于所有项目源集和输出文件,两者都需要一个 FileCollection 和一些胶水,需要在 someJavaProjectSourceSet.srcDirs 上调用 files() 才能获得 代码行 级别使用报告中嵌入的实际源代码进行审计。

有没有更好的办法?

【问题讨论】:

    标签: java gradle test-coverage


    【解决方案1】:

    sourceSets 指令将其他源集添加到报告中,包括源代码和类文件。

    虽然由于某种原因它没有出现在插件文档中,但这实际上是插件本身在默认jacocoTestReport任务中添加当前项目文件的方式。

    /**
     * Adds a source set to the list to be reported on.
     * The output of this source set will be used as classes to include in the report.
     * The source for this source set will be used for any classes included in the report.
     *
     * @param sourceSets one or more source sets to report on
     */
    public void sourceSets(final SourceSet... sourceSets)
    

    要包含来自其他项目的源集,您可以这样做:

    jacocoTestReport {
        sourceSets project(':myAlphaProject').sourceSets.main
        sourceSets project(':myBetaProject').sourceSets.main
    }
    

    简单!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-29
      • 2018-09-13
      • 2019-07-20
      • 2018-05-29
      • 2018-08-15
      • 2018-12-18
      • 2015-08-03
      • 1970-01-01
      相关资源
      最近更新 更多