【每日鲜蘑】Jenkins异常`Gradle build daemon disappeared unexpectedly`解决

8,066 阅读1分钟

异常

在将项目集成到 Jenkins 后,小概率会出现构建失败,Jenkins 控制台输出的错误信息为:

Daemon vm is shutting down... The daemon has exited normally or was terminated in response to a user interrupt.
----- End of the daemon log -----


FAILURE: Build failed with an exception.

* What went wrong:
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)

* Try:
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.

* Get more help at https://help.gradle.org
> Task :compileJava
Build step 'Invoke Gradle script' changed build result to FAILURE
Build step 'Invoke Gradle script' marked build as failure

调查

经过研究,问题可能出在 ./gradlew assembleDebug 这条命令上。

在Gradle的官方文档中,得知从 Gradle 3.0 开始,Daemon 便默认开启的。

The Gradle Daemon is enabled by default, and we recommend always enabling it. 

Daemon是一个长时间运行的后台进程,作用是在内存中存储构建信息,以便在之后的构建过程中复用信息提高构建速度。

但是在文档中,也做了提示。

If you run CI builds in ephemeral environments (such as containers) that do not reuse any processes, use of the Daemon will slightly decrease performance (due to caching additional information) for no benefit, and may be disabled. 

大概的意思是如果通过持续集成进行项目构建,Daemon就没啥用,反倒会因为存储额外的信息而降低系统性能,从而导致不好用。

解决方案一

那么,如何在 持续构建 中停用 Daemon

很简单,如下:

./gradlew --no-daemon assembleDebug

在命令里加入一个 --no-daemon 参数就好啦。

解决方案二

在gradle的配置文件[«USER_HOME»/.gradle/gradle.properties]中增加设置。

org.gradle.daemon=false

各个系统的用户路径有细微差异

  • Windows: C:\Users<username>
  • Mac: /Users/
  • Linux: /home/

本文使用 mdnice 排版