在Bamboo构建服务器上跳过Gradle JacocoTestReports任务,但在本地计算机上构建正常

时间:2015-05-05 15:45:50

标签: android gradle jacoco

我有一个gradle脚本,其中包含使用Jacoco进行报告的测试任务。 build.gradle看起来像这样:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
}
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
apply plugin: "sonar-runner"

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 21
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testHandleProfiling true
        testFunctionalTest true
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            testCoverageEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    apply plugin: 'android-unit-test'

    dependencies {
        testCompile 'org.easytesting:fest:1.0.16'
        testCompile 'junit:junit:4.10'
        testCompile 'org.robolectric:robolectric:2.4'
        testCompile 'com.squareup:fest-android:1.0.8'
    }
}

def coverageSourceDirs = [
        '../app/src/main/java', 'src/gen'
]

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }

    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/R$*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebug.exec')
}

sonarRunner {

    sonarProperties {

        property "sonar.projectKey", "coverage-example"
        property "sonar.projectName", "Coverage Example"
        property "sonar.projectVersion", "1.0"

        property "sonar.sources", "src/main/java"
        property "sonar.binaries", "build"
        property "sonar.test", "src/test/java"

        property "sonar.language", "java"
        property "sonar.profile", "Android Lint"
        property "sonar.android.lint.report", "lint-report.xml"
        property "sonar.dynamicAnalysis", "reuseReports"
        property "sonar.sourceEncoding", "UTF-8"

        property "sonar.junit.reportsPath", "build/outputs/reports/coverage/debug"
        property "sonar.cobertura.reportPath", "build/outputs/reports/coverage/debug/cobertura.xml"
        property "sonar.java.coveragePlugin", "cobertura"

        property "sonar.host.url", "https://sonar.domain.com"

        property "sonar.jdbc.url", "jdbc:mysql://sonar.domain.com:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"

        property "sonar.jdbc.username", "someuser"
        property "sonar.jdbc.password", "somepass"
    }

}

在本地计算机上运行所有测试运行,我按预期获得报告。但是在竹子服务器上运行我可以在日志中看到任务被跳过:

build   05-May-2015 16:19:24    :app:validateDebugSigning
build   05-May-2015 16:19:25    :app:packageDebug
build   05-May-2015 16:19:25    :app:zipalignDebug
build   05-May-2015 16:19:25    :app:assembleDebug
build   05-May-2015 16:19:25    :app:compileTestDebugJava
build   05-May-2015 16:19:25    :app:processTestDebugResources UP-TO-DATE
build   05-May-2015 16:19:25    :app:testDebugClasses
build   05-May-2015 16:19:25    :app:testDebug
build   05-May-2015 16:19:25    :app:jacocoTestReport SKIPPED
build   05-May-2015 16:19:26    :app:sonarRunner
build   05-May-2015 16:19:26    SonarQube Runner 2.3
build   05-May-2015 16:19:26    Java 1.7.0_79 Oracle Corporation (64-bit)
build   05-May-2015 16:19:26    Linux 3.13.0-46-generic amd64
build   05-May-2015 16:19:26    INFO: Runner configuration file: NONE
build   05-May-2015 16:19:26    INFO: Project configuration file: /bamboo/xml-data/build-dir/TSTIOSAPPA-TP-BDA/app/build/tmp/sonarRunner/sonar-project.properties
build   05-May-2015 16:19:26    INFO: Default locale: "en_US", source code encoding: "UTF-8"
build   05-May-2015 16:19:26    INFO: Work directory: /bamboo/xml-data/build-dir/TSTIOSAPPA-TP-BDA/app/build/sonar
build   05-May-2015 16:19:26    INFO: SonarQube Server 5.1

由于我没有收到任何错误,我不明白问题是什么。使用gradle-wrapper执行Gradle,因此它在两台机器上使用相同的版本构建。有任何想法吗?

编辑:

我看过Running jacocoReport,但这并没有真正解释为什么它在我的本地计算机上而不是在构建服务器上。如果我添加测试任务(使用java插件),我会收到以下错误:

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

如果我不应用java插件,我会收到以下错误:

Could not find method test() for arguments [build_58yd8leqt3uhm31fecj9zk2qm$_run_closure4@3a73cd3f] on project ':app'.

但也许我完全误解了如何使用测试任务?无法在项目':app'上找到参数[build_58yd8leqt3uhm31fecj9zk2qm $ _run_closure4 @ 3a73cd3f]的方法test()。

1 个答案:

答案 0 :(得分:2)

当没有要处理的覆盖数据时,将跳过jacocoTestReport任务。

运行测试时会生成覆盖率数据。在android上,任务androidTest将执行测试。

要生成jacoco报告,您必须使用如下所示的行运行测试:

.\gradlew androidTest