0

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.

enter image description here

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?

Xuesong Ye
  • 505
  • 5
  • 13
  • Just pass it with `executionData` or run a shell script instead. And to answer your question: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html – Martin Zeitler Apr 25 '21 at 10:06
  • Does this answer your question? [How to get the list of files in a directory in a shell script?](https://stackoverflow.com/questions/2437452/how-to-get-the-list-of-files-in-a-directory-in-a-shell-script) – Martin Zeitler Apr 25 '21 at 10:10
  • Do you mean to use shell scripts? It is possible to use shell scripts, like the second command line example. " * " Can be recognized as a wildcard character. I hope that the " * " in the gradle configuration can also be correctly recognized as wildcard character instead of string. – Xuesong Ye Apr 25 '21 at 11:37
  • > Could not find method executionData() for arguments [file collection] on task ':qtx:jacocoEtoeAndroidTestReportByCLI' of type org.gradle.api.tasks.Exec. – Xuesong Ye Apr 25 '21 at 11:44
  • How can I pass `executionData` in jacocoReport task to Exec task? – Xuesong Ye Apr 25 '21 at 12:08
  • I can pass parameters now, but it still fails. Copying the failed command to the terminal can be executed successfully. This is really confusing. @Martin Zeitler – Xuesong Ye Apr 25 '21 at 13:29

0 Answers0