[英]How do I get a Jacoco coverage report using gradle plugin when all my tests are in a separate submodule
I'm having trouble setting up jacoco coverage for my java project since I'm new to gradle.我在为我的 java 项目设置 jacoco 覆盖时遇到问题,因为我是 gradle 的新手。 My final goal is to connect this to sonarqube.
我的最终目标是将其连接到 sonarqube。 All my tests are in a separate module
我所有的测试都在一个单独的模块中
structure:结构体:
./build.gradle
settings.gradle
./submodule1/build.gradle
./submodule1/src/main/java/prismoskills/Foo.java
./submodule2/build.gradle
./submodule2/src/main/java/com/project/prismoskills/Bar.java
./test/build.gradle
./test/src/test/java/prismoskills/TestFooBar.java
One way I can think of is to set additionalSourceDirs in test module and enable jacoco only in root and test module.我能想到的一种方法是在 test 模块中设置additionalSourceDirs并仅在 root 和 test 模块中启用 jacoco。
The problem with this approach is that my project has a lot of sub modules(which I haven't shown here) and I am having trouble passing additionalsourcedirs to test module's JacocoReport task in an automated way.这种方法的问题是我的项目有很多子模块(我没有在这里显示)并且我无法以自动化的方式将附加的源目录传递给测试模块的 JacocoReport 任务。
Also it looks like this use case can be handled in maven easily by referring to this https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp Also it looks like this use case can be handled in maven easily by referring to this https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp
Any leads on how to proceed further with gradle will be appreciated.任何有关如何进一步使用 gradle 的线索将不胜感激。 Thanks in advance
提前致谢
gradle version: 6.4
jacoco gradle plugin version: 0.8.5
I think the following solution should solve your problem.我认为以下解决方案应该可以解决您的问题。 The idea is that:
这个想法是:
It does the same for JUnit reports because it is easier to see all tests reports together in the root project instead of navigating between directories.它对 JUnit 报告执行相同的操作,因为在根项目中一起查看所有测试报告而不是在目录之间导航更容易。
plugins {
id 'base'
id 'org.sonarqube' version '3.0'
}
allprojects {
apply plugin: 'jacoco'
apply plugin: 'project-report'
// ...
jacoco {
toolVersion = 0.8.5
}
}
subprojects {
// ...
test {
reports.html.enabled = false
useJunitPlatform()
finalizedBy jacocoTestReport
}
jacocoTestReport {
dependsOn test
reports.html.enabled = false
}
}
// ...
task testReport(type: TestReport) {
destinationDir = file("${buildDir}/reports/test")
reportOn subprojects*.test
}
task jacocoTestReport(type: JacocoReport) {
subprojects { subproject ->
subproject.tasks.findAll { it.extensions.findByType(JacocoTaskExtension) }.each { extendedTask ->
configure {
sourceSets subproject.sourceSets.main
if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {
executionData(extendedTask)
}
}
}
}
reports.xml.enabled = true
}
rootProject.getTasksByName('test', true).each {
it.finalizedBy(testReport)
it.finalizedBy(jacocoTestReport)
}
This line这条线
if (file("${subproject.buildDir}/jacoco/${extendedTask.name}.exec").exists()) {
is added to prevent build failures when some subprojects don't have tests at all.添加以防止在某些子项目根本没有测试时构建失败。
Following can be defined in the build.gradle of root project.以下可以在根项目的 build.gradle 中定义。 Jacoco plugin will be applied to all the submodules.
Jacoco 插件将应用于所有子模块。
subprojects{
plugins{
id 'java'
id 'jacoco'
}
test.finalizedBy jacocoTestReport
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.