Embedding Gradle using the Tooling API - Gradle User Guide Version 4.3.1

# Chapter 14. Embedding Gradle using the Tooling API

## 14.1. Introduction to the Tooling API

Gradle provides 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.

## 14.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 builds that use different versions of Gradle, including versions that are newer or older than the version of the Tooling API that you are using. The Tooling API is Gradle wrapper aware and, by default, uses the same Gradle version as that used by the wrapper-powered build.

Some features that the Tooling API provides:

- Query 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.

- Execute a build and listen to stdout and stderr logging and progress messages (e.g. the messages shown in the 'status bar' when you run on the command line).

- Execute a specific test class or test method.

- Receive interesting events as a build executes, such as project configuration, task execution or test execution.

- Cancel a build that is running.

- Combine multiple separate Gradle builds into a single composite build.

- The Tooling API can download and install the appropriate Gradle version, similar to the wrapper.

- 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 embed in your application.

## 14.3. Tooling API and the Gradle Build Daemon

The Tooling API always uses the Gradle daemon. This means that subsequent calls to the Tooling API, be it model building requests or task executing requests will be executed in the same long-living process. Chapter 7, The Gradle Daemon contains more details about the daemon, specifically information on situations when new daemons are forked.

## 14.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.

To use the Tooling API, add the following repository and dependency declarations to your build script:

Example 14.1. Using the tooling API

`build.gradle`

    repositories {
        maven { url 'https://repo.gradle.org/gradle/libs-releases' }
    }

    dependencies {
        compile "org.gradle:gradle-tooling-api:${toolingApiVersion}"
        // The tooling API need an SLF4J implementation available at runtime, replace this with any other implementation
        runtime 'org.slf4j:slf4j-simple:1.7.10'
    }

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. You can use `GradleConnector.connect()` to create a `ProjectConnection`. A `ProjectConnection` connects to a single Gradle project. Using the connection you can execute tasks, tests and retrieve models relative to this project.

## 14.5. Gradle version and Java version compatibility

### 14.5.1. Provider side

The current version of Tooling API supports running builds using Gradle versions 1.2 and later. However, support for running builds with Gradle versions older than 2.6 is deprecated and will be removed in Tooling API version 5.0.

### 14.5.2. Consumer side

The current version of Gradle supports running builds via Tooling API versions 2.0 and later. However, support for running builds via Tooling API versions older than 3.0 is deprecated and will be removed in Gradle 5.0.

You should note that not all features of the Tooling API are available for all versions of Gradle. For example, build cancellation is only available when a build uses Gradle 2.1 and later. Refer to the documentation for each class and method for more details.

## 14.6. Java version

The Tooling API requires Java 8 or later. Java 7 is currently still supported but will be removed in Gradle 5.0. The Gradle version used by builds may have additional Java version requirements.