I using Jacoco0.8.3 and Sonarqube6.7, I could not get the correct coverage results in gradle’s multi-module projects
- jacoco 0.8.3
- jdk1.8
- sonarqube-gradle-plugin 2.7.1
- sonarqube 6.7
- gradle 5.4.1
- cucumber 5.5.0
I have gone through a great deal of documentation. And I’ve tried to use jacocoMerge
to merge multiple modules’ Jacoco reports into one. But some cross-module unit tests consistently fail to produce the desired coverage values. For example, If I complete a unit test of the submoudleA class in submoudleB, coverage cannot be counted. Especially when used in combination with Cucumber.
My gradle configuration is as follows(root build.gradle):
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7.1"
classpath 'org.jacoco:org.jacoco.ant:0.8.3'
}
}
apply plugin: "java"
apply plugin: 'idea'
apply plugin: "org.sonarqube"
sonarqube {
properties {
property 'sonar.host.url',sonarurl
property "sonar.jacoco.reportPaths", "${rootProject.buildDir}/jacoco/test.exec"
}
}
task jacocoMerge(type: JacocoMerge) {
dependsOn project.allprojects.collect { it.tasks.withType(JacocoReport.class) }
destinationFile = file("${rootProject.buildDir}/jacoco/test.exec")
def classpath = rootProject.buildscript.configurations.classpath
project.subprojects.each { Project s ->
classpath.plus(s.buildscript.configurations.classpath)
if (s.file("${s.buildDir}/jacoco/test.exec").exists()) {
executionData file("${s.buildDir}/jacoco/test.exec")
}
if (s.file("${s.buildDir}/jacoco/cucumber.exec").exists()) {
executionData file("${s.buildDir}/jacoco/cucumber.exec")
}
}
jacocoClasspath = classpath
}
subprojects {
apply plugin: "java"
apply plugin: 'idea'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.3"
}
repositories {
mavenLocal()
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
}
And my gradle configuration is as follows(cucumber moudle build.gradle):
task cucumber() {
dependsOn assemble, compileTestJava, processTestResources
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = sourceSets.test.runtimeClasspath + sourceSets.test.output + sourceSets.main.output
def jacocoAgent = zipTree(configurations.jacocoAgent.singleFile).filter { it.name == "jacocoagent.jar" }.singleFile
jvmArgs = ["-javaagent:$jacocoAgent=destfile=$buildDir/jacoco/cucumber.exec,append=true"]
args = $args
}
}
}
test.dependsOn cucumber
test.mustRunAfter cucumber
dependencies {
testImplementation 'io.cucumber:cucumber-java8:5.5.0'
testImplementation 'io.cucumber:cucumber-java:5.5.0'
testImplementation 'io.cucumber:cucumber-junit:5.5.0'
testImplementation project(':submoudle')
}