Since the Gradle integrated jacoco plugin cannot be used, there is a task skipped problem. I try to use Jacoco Command Line Interface and Exec - Gradle DSL Version 7.0.
def reportTask = tasks.create(reportTaskName, Exec.class) {
group = 'Reporting'
description = "Generate End To End Android Test Jacoco coverage reports on the ${buildVariant.capitalize()} build by Command Line Interface."
workingDir "${project.buildDir}"
commandLine "java", "-jar", "${jacocoCLI}", "report", "${project.buildDir}/outputs/code_coverage/${buildVariant}AndroidTest/connected/*.ec",
"--classfiles", "${project.buildDir}/intermediates/javac/${buildVariant}/classes/",
"--classfiles", "${project.buildDir}/tmp/kotlin-classes/${buildVariant}/",
"--html", "${project.buildDir}/jacocoReport/jacocoHtml/",
"--sourcefiles", "${project.projectDir}/src/main/java",
"--xml", "${project.buildDir}/jacocoReport/jacocoXml.xml"
}
reportTask
When I run this task, I get:
> Task :qtx:jacocoEtoeAndroidTestReportByCLI FAILED
[INFO] Loading execution data file <xxx>/build/outputs/code_coverage/etoeDebugAndroidTest/connected/*.ec.
Exception in thread "main" java.io.FileNotFoundException: <<xxx>>/build/outputs/code_coverage/etoeDebugAndroidTest/connected/*.ec (No such file or directory)
However, if I execute it in the terminal, as shown in the figure below, the task will succeed.
What's the difference between these two usages and how can I use Regular Expressions in Gradle Exec?
UPDATE
def _createAndroidTestCoverageReportByCLI(productFlavorName = "") {
def buildTypeName = "debug"
def buildVariant
buildVariant = "${productFlavorName}${buildTypeName.capitalize()}"
def getExecDataTaskName = "${ANDROID_TEST_REPORT_TASK_PREFIX}${productFlavorName.capitalize()}${ANDROID_TEST_REPORT_TASK_SUFFIX}ExecDataGetter"
def reportTaskName = "${ANDROID_TEST_REPORT_TASK_PREFIX}${productFlavorName.capitalize()}${ANDROID_TEST_REPORT_TASK_SUFFIX}ByCLI"
def jacocoCLI
String execData
project.rootProject.allprojects.each { project ->
if (project.name == "testlib") {
jacocoCLI = "${project.projectDir}/lib/jacococli.jar"
}
}
def getExecDataTask = tasks.create(getExecDataTaskName, JacocoReport.class) {
def listEc = []
def dir = new File("${project.buildDir}/outputs/code_coverage/${buildVariant}AndroidTest/connected/")
dir.eachFileRecurse() { file ->
listEc << file
}
execData = listEc.join(" ")
}
def reportTask = tasks.create(reportTaskName, Exec.class) {
group = 'Reporting'
description = "Generate End To End Android Test Jacoco coverage reports on the ${buildVariant.capitalize()} build by Command Line Interface."
workingDir "${project.buildDir}"
// commandLine "java", "-jar", "${jacocoCLI}", "report", "${execData}",
// "--classfiles", "${project.buildDir}/intermediates/javac/${buildVariant}/classes/",
// "--classfiles", "${project.buildDir}/tmp/kotlin-classes/${buildVariant}/",
// "--html", "${project.buildDir}/jacocoReport/jacocoHtml/",
// "--sourcefiles", "${project.projectDir}/src/main/java",
// "--xml", "${project.buildDir}/jacocoReport/jacocoXml.xml"
commandLine "java -jar ${jacocoCLI} report ${execData} --classfiles ${project.buildDir}/intermediates/javac/${buildVariant}/classes/ --classfiles ${project.buildDir}/tmp/kotlin-classes/${buildVariant}/ --html ${project.buildDir}/jacocoReport/jacocoHtml/ --sourcefiles ${project.projectDir}/src/main/java --xml ${project.buildDir}/jacocoReport/jacocoXml.xml"
}
reportTask.dependsOn getExecDataTask
}
Error
> Task :qtx:jacocoEtoeAndroidTestReportByCLI FAILED
Execution failed for task ':qtx:jacocoEtoeAndroidTestReportByCLI'.
> A problem occurred starting process 'command 'java -jar <RepoPath>/testlib/lib/jacococli.jar report <RepoPath>/QTX/build/outputs/code_coverage/etoeDebugAndroidTest/connected/spoon1619160691046.ec <RepoPath>/QTX/build/outputs/code_coverage/etoeDebugAndroidTest/connected/spoon1619161047292.ec <RepoPath>/QTX/build/outputs/code_coverage/etoeDebugAndroidTest/connected/spoon1619160787358.ec --classfiles <RepoPath>/QTX/build/intermediates/javac/etoeDebug/classes/ --classfiles <RepoPath>/QTX/build/tmp/kotlin-classes/etoeDebug/ --html <RepoPath>/QTX/build/jacocoReport/jacocoHtml/ --sourcefiles <RepoPath>/QTX/src/main/java --xml <RepoPath>/QTX/build/jacocoReport/jacocoXml.xml''
I tried to pass in execfiles
through list, the task still failed. When I copied the failed command to the terminal for execution, it could be executed successfully.
Why? How can it be executed in Gradle?