【发布时间】: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 中,我定义了以下内容:
- 带有项目名称的变量。
- 测试 useTestNG() 后,jacocoTestReport() 任务启动。
- 在 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