java 具有多个子模块的 Gradle jacoco 覆盖率报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38675853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle jacoco coverage report with more than one submodule(s)?
提问by k3b
Does anybody know how to configure a gradle file for java jacoco report that contain codecoverage of more than one gradle submodule?
有人知道如何为包含多个 gradle 子模块的代码覆盖率的 java jacoco 报告配置 gradle 文件吗?
my current approach only shows codecoverage of the current submodule but not codecoverage of a sibling-submodul.
我目前的方法只显示当前子模块的代码覆盖率,而不显示同级子模块的代码覆盖率。
I have this project structure
我有这个项目结构
- build.gradle (1)
- corelib/
- build.gradle (2)
- src/main/java/package/Core.java
- extlib/
- build.gradle (3)
- src/main/java/package/Ext.java
- src/test/java/package/Integrationtest.java
when i execute gradlew :extlib:check :extlib:jacocoTestReport
the junit-test "Integrationtest.java"
is executed and a codecoverage report is generated that does not contain codecoverage for corelib classes like Core.java
当我执行gradlew :extlib:check :extlib:jacocoTestReport
junit-test“Integrationtest.java”时,会生成一个代码覆盖率报告,其中不包含 Corelib 类(如 Core.java)的代码覆盖率
The result should include the codecoverage of Ext.java and Core.java
结果应该包括 Ext.java 和 Core.java 的代码覆盖率
I already read
我已经读过
- https://docs.gradle.org/current/userguide/jacoco_plugin.html
- and Filter JaCoCo coverage reports with Gradle
but found no clues
但没有发现任何线索
here is content of the gradle files
这是gradle文件的内容
// root build.gradle (1)
// Top-level build file where you can add configuration options
// common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
// build.gradle (2) subproject build file for corelib.
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
// build.gradle (3) subproject build file for extlib.
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
compile project(':corelib')
testCompile 'junit:junit:4.11'
// this does not compile
// jacocoTestReport project(':pixymeta-core-lib')
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
// workaround because android-studio does not make the test resources available
// see https://code.google.com/p/android/issues/detail?id=64887#c13
task copyTestResources(type: Copy) {
from sourceSets.test.resources
into sourceSets.test.output.classesDir
}
processTestResources.dependsOn copyTestResources
[Update 2016-08-01]
[更新 2016-08-01]
thanks to @Benjamin Muschko i also tried in the root gradle file
感谢@Benjamin Muschko 我也在根 gradle 文件中尝试过
// https://discuss.gradle.org/t/merge-jacoco-coverage-reports-for-multiproject-setups/12100/6
// https://docs.gradle.org/current/dsl/org.gradle.testing.jacoco.tasks.JacocoMerge.html
task jacocoMerge(type: JacocoMerge) {
subprojects.each { subproject ->
executionData subproject.tasks.withType(Test)
}
}
but got error message (with gradle-2.14)
但收到错误消息(使用 gradle-2.14)
* What went wrong:
Some problems were found with the configuration of task ':jacocoMerge'.
> No value has been specified for property 'jacocoClasspath'.
> No value has been specified for property 'executionData'.
> No value has been specified for property 'destinationFile'.
there is also the gradle plugin https://github.com/paveldudka/JacocoEverywherewhere i have asked for mulit-submodule support https://github.com/paveldudka/JacocoEverywhere/issues/16
还有 gradle 插件https://github.com/paveldudka/JacocoEverywhere我已经要求多子模块支持https://github.com/paveldudka/JacocoEverywhere/issues/16
[update 2016-08-01] i found a working solution based on https://github.com/palantir/gradle-jacoco-coverage
[更新 2016-08-01] 我找到了一个基于https://github.com/palantir/gradle-jacoco-coverage的工作解决方案
See my own answer below
请参阅下面我自己的答案
回答by k3b
Finally I found this plugin: https://github.com/palantir/gradle-jacoco-coveragethat did the job for me:
最后我找到了这个插件:https: //github.com/palantir/gradle-jacoco-coverage为我完成了这项工作:
root gradle.build
根 gradle.build
buildscript {
repositories {
jcenter()
}
dependencies {
// see https://jcenter.bintray.com/com/android/tools/build/gradle/
classpath 'com.android.tools.build:gradle:2.1.0'
// classpath 'com.android.tools.build:gradle:2.2.0-alpha1'
// https://github.com/palantir/gradle-jacoco-coverage
classpath 'com.palantir:jacoco-coverage:0.4.0'
}
}
// https://github.com/palantir/gradle-jacoco-coverage
apply plugin: 'com.palantir.jacoco-full-report'
all subprojects that has
所有具有的子项目
apply plugin: 'jacoco'
are included in the report.
包含在报告中。
回答by Balázs Hámorszky
One possible solution (with some sonar specific parts):
一种可能的解决方案(带有一些声纳特定部分):
def getJacocoMergeTask(Project proj){
def jmClosure = {
doFirst {
logger.info "${path} started"
executionData.each { ed ->
logger.info "${path} data: ${ed}"
}
}
onlyIf {
executionData != null && !executionData.isEmpty()
}
}
def jacocoMerge = null
if(!proj.tasks.findByName('jacocoMerge')){
jacocoMerge = proj.tasks.create('jacocoMerge', JacocoMerge.class)
jacocoMerge.configure jmClosure
// sonar specific part
proj.rootProject.tasks["sonarqube"].mustRunAfter jacocoMerge
proj.sonarqube {
properties {
property "sonar.jacoco.reportPaths", jacocoMerge.destinationFile.absolutePath
}
}
// end of sonar specific part
logger.info "${jacocoMerge.path} created"
} else {
jacocoMerge = proj.tasks["jacocoMerge"]
}
jacocoMerge
}
afterEvaluate { project ->
def jacocoMerge = getJacocoMergeTask(project)
project.tasks.withType(Test) { task ->
logger.info "${jacocoMerge.path} cfg: ${task.path}"
task.finalizedBy jacocoMerge
jacocoMerge.dependsOn task
task.doLast {
logger.info "${jacocoMerge.path} executionData ${task.path}"
jacocoMerge.executionData task
}
def cfg = configurations.getByName("${task.name}Runtime")
logger.info "${project.path} process config: ${cfg.name}"
cfg.getAllDependencies().withType(ProjectDependency.class).each { pd ->
def depProj = pd.dependencyProject
logger.info "${task.path} dependsOn ${depProj.path}"
def jm = getJacocoMergeTask(depProj)
task.finalizedBy jm
jm.dependsOn task
task.doLast {
logger.info "${jm.path} executionData ${task.path}"
jm.executionData task
}
}
}
}
This will merge all the executionData from all the projects, that used a certain project during testing as a dependency.
这将合并所有项目中的所有 executionData,这些项目在测试期间使用某个项目作为依赖项。
回答by Le Duc Duy
You can create a merged report without merged exec
file. Create a new task to the root of build.gradle
with following content.
您可以创建没有合并exec
文件的合并报告。创建一个新任务到根目录,build.gradle
内容如下。
task jacocoReport(type: JacocoReport) {
for (p in allprojects) {
def testTask = p.tasks.findByName("test")
if (testTask != null)
dependsOn(testTask)
executionData.setFrom(file("${p.buildDir}/jacoco/test.exec"))
classDirectories.from(file("${p.buildDir}/classes/java/main"))
}
}
回答by Wei Yuan
This works for me
这对我有用
plugins {
id 'org.kordamp.gradle.jacoco' version '0.29.0'
}
config {
jacoco {
enabled
mergeExecFile
mergeReportHtmlFile
mergeReportXmlFile
additionalSourceDirs
additionalClassDirs
}
}
https://aalmiray.github.io/kordamp-gradle-plugins/#_org_kordamp_gradle_jacoco
https://aalmiray.github.io/kordamp-gradle-plugins/#_org_kordamp_gradle_jacoco
回答by Benjamin Muschko
You will need to create a new task of type JacocoMergethat aggregates the JaCoCo reports from all subprojects. See a similar discussion in this post.
您将需要创建一个JacocoMerge类型的新任务,用于聚合来自所有子项目的 JaCoCo 报告。请参阅此帖子中的类似讨论。