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:
gradle tasks
会被执行多次。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:
一般来说,构建工具的敏捷行为总是方便的。如果你尝试在你的本地构建中使用守护进程的话,那么你将很难再回到 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(请参阅《第62章,嵌入Gradle》)会始终使用守护进程。例如,如果没有守护进程,你就不能正式使用 Tooling API。这意味着无论你是在 Eclipse 中使用 STS Gradle 插件,还是在 Intellij IDEA 中使用 Gradle 支持,都已经使用了Gradle Daemon。
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:
基本思想是,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:
--stop
命令行指令也有一个后果:当运行 --stop
时,你只能停止运行这个命令的 Gradle 版本所启动的守护进程。 --stop
command line instruction: You can only stop daemons that were started with the Gradle version you use when running --stop
. 我们计划在将来改进管理及合并守护进程的方式。
We plan to improve the ways of managing / pooling the daemons in future.
对于命令行的用法,可以看看专题《附录 D,Gradle 命令行》。如果你已经厌倦了一次又一次地使用相同的命令行选项,可以看《第 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
)。--no-daemon
).--stop
命令行选项或更有力的方式停止守护进程。--stop
command line option or in a more forceful way.--foreground
模式启动守护程序,来观察构建是怎么执行的。--foreground
mode to observe how the build is executed.
可以配置一些守护进程的设置,例如 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”