在适用于Android的Azure DevOps中运行JaCoCo:找不到方法jacocoTestReport()

时间:2019-02-04 10:51:57

标签: kotlin azure-devops jacoco azure-pipelines-build-task

我们已经在Android Kotlin项目中实现了Jacoco,可以通过在本地调用。/gradlewclean build jacocoTestReport

但是,当我们部署到VSTS / Azure DevOps时,出现以下错误:

2019-02-04T09:37:35.5760285Z BUILD SUCCESSFUL in 12s
2019-02-04T09:37:35.5760428Z 1 actionable task: 1 executed
2019-02-04T09:37:35.5801607Z SYSTEMVSSCONNECTION exists true
2019-02-04T09:37:35.5816653Z [command]C:\Windows\system32\cmd.exe /D /S /C "C:\vstsagent\A1\_work\2\s\ApolloClient\gradlew.bat clean build jacocoRootReport"
2019-02-04T09:37:36.7652264Z 
2019-02-04T09:37:36.7653533Z FAILURE: Build failed with an exception.
2019-02-04T09:37:36.7653767Z 
2019-02-04T09:37:36.7653947Z * Where:
2019-02-04T09:37:36.7654401Z Build file 'C:\vstsagent\A1\_work\2\s\ApolloClient\build.gradle' line: 44
2019-02-04T09:37:36.7654582Z 
2019-02-04T09:37:36.7654768Z * What went wrong:
2019-02-04T09:37:36.7654952Z A problem occurred evaluating root project 'ApolloClient'.
2019-02-04T09:37:36.7655191Z > Could not find method jacocoTestReport() for arguments [build_abtyecjstjhjqmdmcxnlw2kq0$_run_closure4$_closure8@50246031] on project ':app' of type org.gradle.api.Project.
2019-02-04T09:37:36.7655365Z 
2019-02-04T09:37:36.7655547Z * Try:
2019-02-04T09:37:36.7655747Z Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
2019-02-04T09:37:36.7655931Z 
2019-02-04T09:37:36.7656098Z * Get more help at https://help.gradle.org
2019-02-04T09:37:36.7656242Z 
2019-02-04T09:37:36.7656585Z BUILD FAILED in 1s
2019-02-04T09:37:37.1999815Z Error: C:\vstsagent\A1\_work\2\s\ApolloClient\gradlew.bat failed with return code: 1
2019-02-04T09:37:37.1999996Z     at ChildProcess.<anonymous> (C:\vstsagent\A1\_work\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.143.2\node_modules\vsts-task-lib\toolrunner.js:639:25)
2019-02-04T09:37:37.2000102Z     at emitTwo (events.js:106:13)
2019-02-04T09:37:37.2000154Z     at ChildProcess.emit (events.js:191:7)
2019-02-04T09:37:37.2000226Z     at maybeClose (internal/child_process.js:886:16)
2019-02-04T09:37:37.2000286Z     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

我不理解的是,它是否正在寻找名为 jacocoRootReport jacocoTestReport 的任务?

2 个答案:

答案 0 :(得分:1)

我无法获得创建报告的实际步骤,因此我找到了解决方法

  • 将gradle任务设置为“ build jacocoRootReport”
  • 使用“发布代码覆盖率结果”任务(设置为使用Jacoco)

答案 1 :(得分:0)

我也陷入了这个困境,而Burf2000的答案为我提供了一些出路。希望这能使其他人受益。我的项目是纯Java而不是原始问题,而不是Kotlin,但我认为这都适用。

在Azure devops中为Android运行Jacoco:

Jacoco位

这是假设您一开始没有Jacoco的工作任务。如果您这样做,请跳至 Azure Devops位

  1. 在顶级build.gradle文件中添加Jacoco依赖项:
 dependencies {
        ...
        classpath 'org.jacoco:org.jacoco.core:0.8.5'
    }
  1. jacoco.gradle文件添加到您的应用程序根目录。我的看起来像这样:
apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.5"
}

def buildTypes = android.buildTypes.collect { type -> type.name }

buildTypes.each {
    buildTypeName ->
        def sourceName, sourcePath
        sourceName = sourcePath = "${buildTypeName}"
        def testTaskName = "test${sourceName.capitalize()}UnitTest"

        task "${testTaskName}Coverage" (type:JacocoReport, dependsOn: "$testTaskName") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build."
            logger.info(description)
            classDirectories = fileTree(
                    dir: "${project.buildDir}/intermediates/javac/${sourcePath}/classes",
                    excludes: ['**/R.class', '**/R$*.class', '**/*$ViewInjector*.*', '**/BuildConfig.*', '**/Manifest*.*'] )
            def coverageSourceDirs = [
                    "src/main/java",
                    "src/$buildTypeName/java"
            ]
            additionalSourceDirs = files(coverageSourceDirs)
            sourceDirectories = files(coverageSourceDirs)
            executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
            reports {
                xml.enabled = true
                html.enabled = true
            }
        }
}

它是this blogpost的一个重要名称。他们还针对不同的产品口味进行了测试,我将其剔除掉。除此之外,上述博客和链接博客之间的主要区别是classDirectories文件路径。

  1. 在应用程序构建脚本jacoco.gradle

  2. 中应用apply from: '../jacoco.gradle'
  3. 向要运行覆盖率指标的所有testCoverageEnabled true添加buildTypes

如果运行./gradlew tasks,现在应该看到报告标题下有一些代码覆盖任务。运行它们,并确保它们按预期工作,因为反馈循环在下一个阶段会慢很多。

Azure Devops位

Burf2000并未说明他是如何发生Could not find method jacocoTestReport() ...错误的,但是我是通过在我的codeCoverageToolOption:的gradle任务中将"jaCoCo"设置为azure-pipelines.yml来解决的。不要这样做,这是一个陷阱。 The github docs for the gradle task告诉您,如果您的构建已启用代码覆盖率,则不应使用它。

相反,您想要配置Azure Devops Gradle任务以调用您之前创建的Jacoco任务之一,然后将PublishCodeCoverageResults任务指向生成的XML。我的azure-pipelines.yml文件的相关部分如下所示:

  - task: Gradle@2
    inputs:
      workingDirectory: ""
      ...
      tasks: "assemble test testReleaseUnitTestCoverage"

  - task: PublishCodeCoverageResults@1
    inputs:
      codeCoverageTool: "JaCoCo"
      summaryFileLocation: $(System.DefaultWorkingDirectory)/app/build/**/testReleaseUnitTestCoverage.xml
      pathToSources: $(System.DefaultWorkingDirectory)/app/src/main/java
      failIfCoverageEmpty: true