微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

生成报告时未找到源文件

如何解决生成报告时未找到源文件

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

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,但正如您在架构中看到的那样,我的每个项目的路径都是正确的。

enter image description here

编辑 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)

解决方法

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

,

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。