第十九章. Gradle 守护进程

Chapter 19. The Gradle Daemon

19.1. 走进守护进程

19.1. Enter the daemon

Gradle 守护进程 (有时也称为 构建守护进程 ) 的目的是改善 Gradle 的启动和执行时间。
The Gradle daemon (sometimes referred as the build daemon ) aims to improve the startup and execution time of Gradle.

我们准备了几个守护进程非常有用的用例。对于一些工作流,用户会多次调用 Gradle,以执行少量的相对快速的任务。举个例子:
We came up with several use cases where the daemon is very useful. For some workflows, the user invokes Gradle many times to execute a small number of relatively quick tasks. For example:

  • 当使用测试驱动开发时,单元测试会被执行多次。
    When using test driven development, where the unit tests are executed many times.
  • 当开发一个 web 应用程序中,应用程序会被组装多次。
    When developing a web application, where the application is assembled many times.
  • 当发现构建能做什么,在 gradle tasks 在哪里会执行多次。
    When discovering what a build can do, where gradle tasks is executed a number of times.

对以上各种工作流来说,让调用 Gradle 的启动成本尽可能小会很重要。
For above sorts of workflows, it is important that the startup cost of invoking Gradle is as small as possible.

此外,如果可以相对较快地建立 Gradle 模型,用户界面可以提供一些有趣的功能。例如,该守护进程可能用于以下情形:
In addition, user interfaces can provide some interesting features if the Gradle model can be built relatively quickly. For example, the daemon might be useful for following scenarios:

  • 在 IDE 中的内容帮助
    Content assistance in the IDE
  • 在 GUI 中的实时可视化构建
    Live visualisation of the build in a GUI
  • 在 CLI 中的 tab 键完成
    Tab completion in a CLI

一般情况下,构建工具的敏捷行为总是可以派上用场。如果你尝试在你的本地构建中使用守护进程的话,它会变得让你很难回到正常的 Gradle 使用。
In general, snappy behavior of the build tool is always handy. If you try using the daemon for your local builds it's going to be hard for you to go back to regular use of Gradle.

Tooling API (参见 第六十二章, 嵌入 Gradle ) 在整个过程当中都使用守护进程。如,你无法在没有守护进程时正式地使用 Tooling API。这意味着当您在 Eclipse中使用 STS Gradle 或在 Intellij IDEA 中使用 Gradle 支持时,您已经在使用 Gradle 守护进程。
The Tooling API (see Chapter 62, Embedding Gradle ) uses the daemon all the time, e.g. you cannot officially use the Tooling API without the daemon. This means that whenever you are using the STS Gradle plugin for Eclipse or the Gradle support in Intellij IDEA, you're already using the Gradle Daemon.

未来,该守护进程还会提供更多的功能:
In future the daemon will offer more features:

  • 敏捷的 up-to-date 检查:使用本地文件系统修改通知(例如,通过 jdk7 nio.2)预先执行 up-to-date 分析。
    Snappy up-to-date checks: use native file system change notifications (e.g. via jdk7 nio.2) to preemptively perform up-to-date analysis.
  • 更快的构建: 预评估项目,这样当用户接下来调用 Gradle 时,模型就准备好了。
    Even faster builds: preemptively evaluate projects, so that the model is ready when the user next invokes Gradle.
  • 我们提到了更快的构建吗?守护进程可以预先下载依赖项或进行快照依赖的新版本检查。
    Did we mention faster builds? The daemon can potentially preemptively download dependencies or check for new versions of snapshot dependencies.
  • 使用可用于编译和测试的一个可复用线程池。例如,Groovy 和 Scala 的编译器启动开销都很大。构建守护进程可以维持一个已下载的 Groovy 和 (或) Scala 进程。
    Utilize a pool of reusable processes available for compilation and testing. For example, both the Groovy and Scala compilers have a large startup cost. The build daemon could maintain a process with Groovy and/or Scala already loaded.
  • 预先执行某些任务,比如编译。更快的反馈。
    Preemptive execution of certain tasks, for example compilation. Quicker feedback.
  • 快速、 准确的 bash 的 tab 键完成。
    Fast and accurate bash tab completion.
  • Gradle 缓存的定期垃圾收集。
    Periodically garbage collect the Gradle caches.

19.2. 重用和失效的守护程序

19.2. Reusing and expiration of daemons

基本的思想是, gradle 命令会fork一个守护进程,用于执行实际的构建。Gradle 命令的后续调用将重用该守护进程,以避免启动开销。有时我们不能使用现有的守护进程,是因为它正忙或其 java 版本或 jvm 参数不同。关于fork一个完全新的守护进程的具体细节,请阅读下面的专题。守护进程将在空闲3小时后自动失效。
The basic idea is that the gradle command forks a daemon process, which performs the actual build. Subsequent invocations of the gradle command will reuse the daemon, avoiding the startup costs. Sometimes we cannot use an existing daemon because it is busy or its java version or jvm arguments are different. For exact details on when exactly new daemon process is forked read the dedicated section below. The daemon process automatically expire after 3 hours of idle time.

以下是我们fork一个新的守护进程的所有情况:
Here're all situations in which we fork a new daemon process:

  • 如果该守护进程当前正忙于运行一些作业,将启动一个全新的守护进程。
    If the daemon process is currently busy running some job, a brand new daemon process will be started.
  • 对每个java home,我们会fork一个单独的守护进程。所以即使有一些闲置的守护进程等待构建请求,但你碰巧通过不同的 java HOME 运行构建,那么一个全新的守护进程将会被fork。
    We fork a separate daemon process per java home. So even if there is some idle daemon waiting for build requests but you happen to run build with a different java home then a brand new daemon will be forked.
  • 如果用于构建的jvm的参数足够不同,我们会fork一个单独的守护进程。例如,如果某些系统属性已经更改,我们不会fork一个新的守护进程。然而,如果-Xmx 内存设置更改了,或一些基本的不变的系统属性更改了 (例如 file.encoding),那么将fork新的守护进程。
    We fork a separate daemon process if the jvm arguments for the build are sufficiently different. For example we will not fork a new daemon if a some system property has changed. However if -Xmx memory setting change or some fundamental immutable system property changes (e.g. file.encoding) then new daemon will be forked.
  • 在这一刻,守护进程会被加上 Gradle 的特定版本号。这意味着即使一些守护进程处于空闲状态,但您正在运行的构建与 Gradle 不同版本,也将启动一个新的守护进程。这也有一种 --stop命令行指令的结果: 当运行 --stop时,您仅可以停止以你的Gradle版本启动的守护进程。
    At the moment daemon is coupled with particular version of Gradle. This means that even if some daemon is idle but you are running the build with a different version of Gradle, a new daemon will be started. This also has a consequence for the --stop command line instruction: You can only stop daemons that were started with the Gradle version you use when running --stop .

我们计划在将来改进守护进程的managing / pooling的方法。
We plan to improve the ways of managing / pooling the daemons in future.

19.3. 用法和故障排除

19.3. Usage and troubleshooting

关于命令行的用法,可以看一下专题 附录 DGradle 命令行。如果你已经厌倦反复使用相同的命令行选项,可以看看 第 20.1 章节,“通过 gradle.properties 配置构建环境”。这一章节包含了有关如何以一种“持久化”的方式配置某些行为(包括在默认情况下打开守护进程)的信息。
For command line usage take a look dedicated section in Appendix D, Gradle Command Line . If you are tired of using the same command line options again and again, take a look at Section 20.1, “Configuring the build environment via gradle.properties” . The section contains information on how to configure certain behavior of the daemon (including turning on the daemon by default) in a more 'persistent' way.

以下是有关 Gradle 守护进程的故障排除的一些方面:
Some ways of troubleshooting the Gradle daemon:

  • 如果你的构建有问题,请尝试暂时禁用守护进程 (您可以通过使用命令行开关 --no-daemon)。
    If you have a problem with your build, try temporarily disabling the daemon (you can pass the command line switch --no-daemon ).
  • 有时候,您可能想要通过 --stop命令行选项或更有力的方式停止守护程序。
    Occasionally, you may want to stop the daemons either via the --stop command line option or in a more forceful way.
  • 默认情况下位于 Gradle 用户主目录有一个守护进程的日志文件。
    There is a daemon log file, which by default is located in the Gradle user home directory.
  • 你可能想要以 --foreground模式启动守护程序,以观察构建是怎么执行的。
    You may want to start the daemon in --foreground mode to observe how the build is executed.

19.4. 配置守护进程

19.4. Configuring the daemon

可以配置一些守护进程的设置,例如 JVM 参数、 内存设置或Java home目录。有关更多信息请参阅 20.1章节, “通过 gradle.properties 配置构建环境”
Some daemon settings, such as JVM arguments, memory settings or the Java home, can be configured. Please find more information in Section 20.1, “Configuring the build environment via gradle.properties”