Chapter 13. Tooling API (Embedding Gradle)

13.1. Introduction to the Tooling API

Since Gradle 1.0-milestone-3, Gradle has had a programmatic API called the Tooling API, which you can use for embedding Gradle into your own software. 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; however, the API is open for anyone who needs to embed Gradle in their application.

  • Gradle TestKit uses the Tooling API for functional testing of your Gradle plugins.
  • Eclipse Buildship uses the Tooling API for importing your Gradle project and running tasks.
  • IntelliJ IDEA uses the Tooling API for importing your Gradle project and running tasks.

13.2. Tooling API Features

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.

Some features that the Tooling API provides today:

  • 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 messages shown in the 'status bar' when you run on the command line).
  • 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.
  • The implementation is lightweight, with only a small number of dependencies. It is also a well-behaved library, and makes no assumptions about your classloader structure or logging configuration. This makes the API easy to bundle in your application.

In the future we may support other interesting features:

  • 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.
  • Notifications when things in the build change. For example, your Eclipse or IDEA project could be updated immediately in the background.
  • Validating and prompting for user supplied configuration.
  • Prompting for and managing user credentials.

13.3. Tooling API and the Gradle Build Daemon

The Tooling API uses the daemon all the time. 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 6, The Gradle Daemon contains more details about the daemon, specifically information on situations when new daemons are forked.

13.4. Quickstart

As the Tooling API is an interface for developers, the Javadoc is the main documentation for it. We provide several samples that live in samples/toolingApi in your Gradle distribution. These samples specify all of the required dependencies for the Tooling API with examples for querying information from Gradle builds and executing tasks from the Tooling API.

The main entry point to the Tooling API is the GradleConnector. You can navigate from there to find code samples and explore the available Tooling API models. There are two ways of using the GradleConnector to connect to a Gradle build.

  • You can use GradleConnector.connect() to create a ProjectConnection. ProjectConnection can only be used to connect to a single Gradle project at a time. Executing tasks and retrieving models are all relative to this project.
  • You can use GradleConnector.newGradleConnection() to create a GradleConnectionBuilder. GradleConnectionBuilder can be used to connect to any number of Gradle builds at one time. Executing tasks and retrieving models are performed in the context of the composite. Instead of retrieving a single model for a single Gradle project, the Tooling API can provide all models for all projects with a single method call.