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 (参见
第六十二章,
嵌入 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:
基本的思想是, 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
.
我们计划在将来改进守护进程的managing / pooling的方法。
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”