第六十二章. 嵌入 Gradle

Chapter 62. Embedding Gradle

62.1. Tooling API 简介

62.1. Introduction to the Tooling API

1.0 里程碑 3 发行版带来了新 API,叫 tooling API,可用于嵌入 Gradle。这个 API 允许你执行和监视构建,以及向 Gradle 查询有关构建的细节。它的主要受众是 IDE,CI 服务器,其他的 UI 作者或你的 Gradle 插件的集成测试。不过,它是向所有需要在其应用程序中嵌入 Gradle 的人开放的。
The 1.0 milestone 3 release brought a new API called the tooling API, which you can use for embedding Gradle. This API allows you to execute and monitor builds, and to query Gradle about the details of a build. The main audience for this API is IDE, CI server, other UI authors, or integration testing of your Gradle plugins. However, it is open for anyone who needs to embed Gradle in their application.

这个 tooling API 的基本特征是它以独立于版本的方式运行。这意味着你可以使用同一 API 来处理不同目标g版本的 Gradle。Toolin API 是能感知到 Gradle 包装器的,默认情况下,它使用与包装器驱动的项目相同的 Gradle 版本。
A fundamental characteristic of the tooling API is that it operates in a version independent way. This means that you can use the same API to work with different target versions of Gradle. The tooling API is Gradle wrapper aware and, by default, uses the same target Gradle version as that used by the wrapper-powered project.

目前这个 tooling API 提供了以下这些功能:
Some features that the tooling API provides today:

  • 你可以查询 Gradle 关于构建的详细信息,包括项目层次结构和项目依赖,外部依赖(包括源代码及 javadoc jars),每一个项目的源代码目录和任务。
    You can query Gradle for the details of a build, including the project hierarchy and the project dependencies, external dependencies (including source and javadoc jars), source directories and tasks of each project.
  • 你可以执行构建,并且监听标准输出和错误输出的日志和进度(例如当你在命令上运行时显示在“状态栏”上的内容)。
    You can execute a build, and listen to stdout and stderr logging and progress (e.g. the stuff shown in the 'status bar' when you run on the command line).
  • Tooling API 可以下载并安装相应的 Gradle 版本,类似于包装器。请记住,tooling API 是有包装器感知的,因此你不应该直接配置 Gradle 分发包。
    Tooling API can download and install the appropriate Gradle version, similar to the wrapper. Bear in mind that the tooling API is wrapper aware so you should not need to configure a Gradle distribution directly.
  • 它的实现是轻量级的,只有很少的依赖。同时它也是一个行为良好的库,并且不会假定你的类装入器结构或日志记录配置。这便于把 API 捆绑在应用程序中。
    The implementation is lightweight, with only a small number of dependencies. It is also a well-behaved library, and makes no assumptions about your class loader structure or logging configuration. This makes the API easy to bundle in your application.

将来我们可能会支持一些其他有趣的功能:
In future we may support other interesting features:

  • 性能。这个API 让我们有机会进行大量的缓存,静态分析以及抢占式的工作,从而为用户提供更快的速度。
    Performance. The API gives us the opportunity to do lots of caching, static analysis and preemptive work, to make things faster for the user.
  • 改进进度监视和构建取消。例如,允许监视测试执行。
    Better progress monitoring and build cancellation. For example, allowing test execution to be monitored.
  • 当构建中的内容有变化时进行通知,以便可以更新 UI 和模型。例如,Eclipse 或 IDEA 项目将在后台立即更新。
    Notifications when things in the build change, so that UIs and models can be updated. For example, your Eclipse or IDEA project will update immediately, in the background.
  • 对用户提供的配置进行验证和输入提示。
    Validating and prompting for user supplied configuration.
  • 对用户凭据的输入提示和管理。
    Prompting for and managing user credentials.

62.2. Tooling API 及 Gradle 构建守护进程

62.2. Tooling API and the Gradle Build Daemon

请查看《第 19 章,Gradle 守护进程》。Tooling API 全程使用守护进程,举例来说,没有守护进程,你就不能正式使用 Tooling API。这意味着后续调用 Tooling API 时,都会在同一个长期存活的进程中执行构建请求或任务执行请求的模型。《第 19 章,Gradle 守护进程》包含了有关守护进程的更多详细内容,特别是有关派生新守护程序情况的内容。
Please take a look at Chapter 19, The Gradle Daemon. The Tooling API uses the daemon all the time, e.g. you cannot officially use the Tooling API without the daemon. This means that subsequent calls to the Tooling API, be it model building requests or task executing requests can be executed in the same long-living process. Chapter 19, The Gradle Daemon contains more details about the daemon, specifically information on situations when new daemons are forked.

62.3. 快速入门

62.3. Quickstart

由于Tooling API 是面向程序员的接口,因此大部分的文档都在 Javadoc 中。这正是我们的目的——我们不希望本章节内容篇幅太大。相反,我们将添加更多的代码示例并改进 Javadoc 文档。Tooling API 的主要入口点是 GradleConnector。你可以从该处浏览并找到代码示例及其他说明。学习如何使用Tooling API 的有效方法是检出并运行 $gradleHome/samples/toolingApi 中的 示例
Since the tooling API is an interface for a programmer most of the documentation lives in the Javadoc. This is exactly our intention - we don't expect this chapter to grow very much. Instead we will add more code samples and improve the Javadoc documentation. The main entry point to the tooling API is the GradleConnector. You can navigate from there and find code samples and other instructions. Pretty effective way of learning how to use the tooling API is checking out and running the samples that live in $gradleHome/samples/toolingApi.

如果你正在嵌入 Gradle,并且正在寻找 tooling API 所需要的确切依赖,那么请查看 $gradleHome/samples/toolingApi 中的样本。依赖是在 Gradle 构建脚本中声明的。你还可以找到获取 Jar 的仓库声明。
If you're embedding Gradle and you're looking for exact set of dependencies the tooling API Jar requires please look at one of the samples in $gradleHome/samples/toolingApi. The dependencies are declared in the Gradle build scripts. You can also find the repository declarations where the Jars are obtained from.