将 Gradle 升级到 v2.8 后,Travis 未通过 Findbugs

After upgrading Gradle to v2.8 Travis doesn't pass Findbugs

我刚刚将 gradle 从 2.2 版更新到最新的 2.8 版。我对 2.2 版的 findbugs 没有任何问题。

我正在开发一个包含两个模块的 Android 项目。为了在两个模块中使用查找错误,我在根目录的主 build.gradle 文件上进行了以下配置。

configure(allprojects) {
    apply plugin: 'findbugs'

    task findbugs(type: FindBugs) {
        ignoreFailures = false
        effort = "max"

        classes = fileTree('build/intermediates/classes/')
        source = fileTree('src/main/java')
        classpath = files()

        excludeFilter = file("exclude.xml")

        reportLevel = "high"
        reports {
            xml.enabled = false
            html.enabled = true
        }
    }
}

当我在我的本地机器上 运行 ./gradlew findbugs 一切正常并且构建成功但是当我将我的 PR 推送到 Github 并且 Trivis 尝试构建然后我得到错误:

:findbugs UP-TO-DATE
:passenger-app:findbugs
:passenger-sdk:findbugs FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':passenger-sdk:findbugs'.
> FindBugs rule violations were found. See the report at: file:///home/travis/build/project-name/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html

我真的很困惑为什么我在本地机器上没有问题而 Travis 却显示错误!我试图在 Travis 上打印内容 findbugs.html 但我的权限被拒绝了:(

我使用的是 java 1.8,而 Travis 使用的是 1.7。问题与此有关吗?谢谢


更新:

为了在 Trivis 上打印 findbugs.html 的内容,我创建了一个 print_findbugs.sh,这是它的内容。

#!/usr/bin/env bash

echo '**********************'
echo '*** Print Findbugs ***'
echo '**********************'
echo file:///home/travis/build/company/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html

然后我在 .travis.yml 文件中设置 sudo true。我在这个文件中有什么。

sudo: true
language: android
android:
  components:
    - build-tools-23.0.1
    - android-23
    - extra-android-support
    - extra-google-google_play_services
    - extra-google-m2repository
    - extra-android-m2repository

env:
  global:
// some settings are there


before_cache:
  # Delete the gradle lock file which forces creation of a new build cache
  - rm ~/.gradle/caches/modules-2/modules-2.lock

cache:
  directories:
  - ~/.gradle

before_script:
  # Overwrite the keystore if it is a pull request
  - ./before_script.sh

script:
  # Override Travis default script to not run connectedCheck until this bug is fixed:
  # https://code.google.com/p/android/issues/detail?id=59592
  - ./gradlew clean build findbugs -PdisablePreDex
  - ./print_findbugs.sh

before_deploy:
  # Clean up the output folder
  # Link up the new builds into individual html files
  - ./before_deploy.sh

after_deploy:
  # Upload to...

最后我的 travis 打印出来:

:findbugs UP-TO-DATE
:passenger-app:findbugs
:passenger-sdk:findbugs
FindBugs rule violations were found. See the report at: file:///home/travis/build/company/passenger-android/passenger-sdk/build/reports/findbugs/findbugs.html
BUILD SUCCESSFUL
Total time: 8 mins 9.966 secs
The command "./gradlew clean build findbugs -PdisablePreDex" exited with 0.
 $ ./print_findbugs.sh
/home/travis/build.sh: line 41: ./print_findbugs.sh: Permission denied
The command "./print_findbugs.sh" exited with 126.
before_cache
$ rm ~/.gradle/caches/modules-2/modules-2.lock
cache.2
Done. Your build exited with 1.

我不使用它,我需要有关权限被拒绝的更多信息。

Html 报告

过去,我使用 Travis-ci、point 5 here 打印了 html 报告。我使用 apt-get 下载了 lynx(现在无法使用容器基础设施和 sudo: false)并转换并打印了报告。

before_script:   
  # - echo 'LOGCAT'   
  # Check logcat debug output: http://developer.android.com/tools/help/logcat.html
  # Check debugging log: http://developer.android.com/tools/debugging/debugging-log.html
  # Comment the lines belows to debug output and redirect it to a file. Custom tags for your app.
  - adb -e logcat *:W | tee logcat.log > /dev/null 2>&1 &

after_failure:
  # - echo 'FAILURE'
  # Check apt configuration: http://docs.travis-ci.com/user/ci-environment/#apt-configuration
  # Comment out the lines below to show log about tests with app name customized on exports section.
  - sudo apt-get install -qq lynx
  - export MOD_NAME=yourappmodulename
  - export LOG_DIR=${TRAVIS_BUILD_DIR}/${MOD_NAME}/build/outputs/reports/androidTests/connected/
  - lynx --dump ${LOG_DIR}com.android.builder.testing.ConnectedDevice.html > myConnectedDevice.log
  - lynx --dump ${LOG_DIR}com.android.builder.testing.html > myTesting.log
  - for file in *.log; do echo "$file"; echo "====================="; cat "$file"; done || true

Xml 报告

可用于传统基础设施和基于容器的基础设施。

我了解到您可以像这样启用 xml 报告:

    reports {
        xml.enabled = true
        html.enabled = true
    }

您可以轻松地使用 cat here:

在 Travis-ci 上打印 xml 报告
  - cat ${TRAVIS_BUILD_DIR}/ui/espresso/*/app/build/outputs/androidTest-results/connected/* # logs

添加 * 此时将包括所有子文件夹。

您需要先在本地找到 xml 报告的文件夹,在我的例子中与 html 不是同一个文件夹,然后将类似这样的内容添加到您的 travis.yml 文件中:

after_failure:
  - cat /home/travis/build/*/passenger-android/passenger-sdk/build/reports/findbugs/*

这不能解决您的问题,但可能有助于找到原因。

更新:

我建议您先尝试使用 cat 和没有脚本的 xml 版本。

权限问题很好的解释here and how to solve it making the file executable:

before_script:
 - chmod +x yourscript

更新二:

解释了解决 permission denied 问题的更好方法

使用它并提交更改:

git update-index --chmod=+x yourscript

终于我能够通过一切。

我首先使用以下命令而不是简单的 $ ./gradlew findbugs

./gradlew clean aGD findbugs -PdisablePreDex

aGD 是我的任务的缩写,类似于 assembleDebug.

我可以看到 gradle 的很多抱怨。可能 Travis 正在展示这个(调试后),但由于它在控制台中打印 html 页面,因此人类无法阅读。所以我在 Findbug 的 exclude file (ref) 中添加了过滤器,以便通过 Findbugs 检查。

我成功了!一旦我推送了我的代码,Travis 又失败了。

但是由于我在 Travis 上打印了 Findbug 的输出并且只有一个问题我可以在我的代码中找到它并修复它。之后Travis没有失败,通过了我的PR

所以不幸的是问题仍然存在。我可以让 Findbugs 通过我的本地项目,而 Travis 有时会发现更多问题。我怀疑 Travis 是否能够加载最新版本的 Findbugs,但由于缓存问题,我的无法加载。但是,是的,这是我的假设!

在我的例子中,ignoreFailures 是正确的,但是 findbugs 任务在升级 gradle 后失败了,因为 build.gradle 依赖于 findbugs:annotations:3.0.0 而不是 findbugs:findbugs-annotations:3.0.1。 (注:annotations对比findbugs-annotations,这是新神器