【问题标题】:Source file was not found during generation of report生成报告时找不到源文件
【发布时间】:2021-09-21 09:37:54
【问题描述】:

请帮助我了解问题所在。 我曾与多模块项目合作并面临一个问题。 我的架构如下所示:

proj1
-src
--main
---java
----com.myProj.myApi
-----directories containing .java files

... // same proj2 and proj3

rootProject src
-main
--java
---com.myProj.myApi
----Application.java
-test
--test .java files

在根 gradle 中,我定义了以下内容:

  1. 带有项目名称的变量。
  2. 测试 useTestNG() 后,jacocoTestReport() 任务启动。
  3. 在 jacocoTestReport() 中,我已确定需要加载和检查哪些 java 文件。 我的根 build.gradle:
  repositories {
    mavenLocal()
    mavenCentral()
    tasks.withType(Javadoc).all { enabled = false }
    tasks.withType(JavaCompile).all { options.encoding = 'UTF-8'  }
  }

  group = 'com.myProj.myApi'
  version = '1.0.0-SNAPSHOT'

  apply plugin: 'java'
  apply plugin: 'jacoco'
  apply plugin: 'java-library'

  jacoco {
    toolVersion = "0.8.5"
  }

  def otherProjects = [':proj1', ':proj2', ':proj3']

test {
  useTestNG()
  finalizedBy jacocoTestReport
}

otherProjects.each {
  evaluationDependsOn it
}

jacocoTestReport {
  FileTree sourceTree = files().getAsFileTree()
  FileTree classTree = files().getAsFileTree()

  otherProjects.each {
    sourceTree += project(it).sourceSets.main.allJava
    classTree += project(it).sourceSets.main.output.asFileTree
  }

  sourceTree = sourceTree.matching {
    exclude '**/nonTestedClass.java'
  }

  classTree = classTree.matching {
    exclude '**/nonTestedClass.class'
  }

  getAdditionalSourceDirs().setFrom(sourceTree)
  getAdditionalClassDirs().setFrom(classTree)
  reports {
    html.enabled = true
    xml.enabled = true
    csv.enabled = false
  }
}

dependencies {
  implementation project(':proj1')
  implementation project(':proj2')
  implementation project(':proj3')
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.testng:testng'
}

Gradle 版本是 7.0.1。爪哇 11。 我查看了this post,但我的每个项目的路径都是正确的,正如您在架构中看到的那样。

编辑 1 我发现在每个子项目中,构建目录只包含特定子项目中的那些 java 文件的类文件。也许这就是问题所在?也许我需要将所有类文件发送到根构建目录? 附言子项目的 build.gradle 不包含任何 jacoco 设置。

编辑 2 我为 getAdditional...Dirs() 添加了 print 并了解到所有路径都是正确的并找到了。 终端也给了我这个信息:

> Task :jacocoTestReport
[ant:jacocoReport] Classes in bundle 'myProj' do no match with execution data. For report generation the same class files must be used as at runtime.

但这很奇怪,因为当我添加了这段代码时

println(JavaVersion.current())

我得到了这个结果:Java 版本:11。这是我的 Java 的正确版本:

$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

【问题讨论】:

    标签: java gradle jacoco multi-module


    【解决方案1】:

    也许com.myProj.myApi 是一个目录?而应该是相互嵌套的3个目录com/myProj/myApi

    【讨论】:

    • 还有。顺便说一句,上面我在标题下的 EDIT 指出了一个可能的原因,对吗?
    • 您将许多不相关的东西混合在一起 - “捆绑'myProj'中的类与执行数据不匹配”与原始“找不到源文件”和Java版本无关.
    • 而且 IMO 在您的描述中没有 stackoverflow.com/help/minimal-reproducible-example,因此不太可能有人能够盲目地猜测您的情况出了什么问题。查看docs.gradle.org/current/samples/… 并将您的设置与以下最小可重现示例github.com/gradle/gradle/tree/… 进行比较
    • 我得到了同样的结果。这很奇怪,因为当我使用 Intellij IDEA Run> Show Code Coverage> test.exec 时,IDE 工具包会显示高亮代码。
    【解决方案2】:

    我在更改文件工作时已修复它。我已经删除了从 'jacocoTestReport {' 到 'reports {' (不包括)的所有代码并插入:

      for (proj in otherProjects) {
        sourceSets project(module).sourceSets.main
      }
    

    我不明白为什么 getAdditionalSourceDirs() 和 getAdditionalClassDirs() 不起作用。或者,很可能,我不知道这两种与文件交互的方法之间的区别。如果您知道,您可以在此答案下发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-03-02
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      相关资源
      最近更新 更多