# Gradle

API Documentation: | `Gradle`

Represents an invocation of Gradle.

You can obtain a `Gradle` instance by calling `Project.getGradle()`.

### Properties

Property | Description
`gradle` | Returns this `Gradle` instance. This method is useful in init scripts to explicitly access Gradle properties and methods. For example, using `gradle.parent` can express your intent better than using `parent`. This property also allows you to access Gradle properties from a scope where the property may be hidden, such as, for example, from a method or closure.
`gradleUserHomeDir` | The Gradle user home directory. This directory is used to cache downloaded resources.
`gradleVersion` | The current Gradle version.
`parent` | The parent build of this build, if any.
`rootProject` | The root project of this build.
`startParameter` | The `StartParameter` used to start this build.
`taskGraph` | The `TaskExecutionGraph` for this build.

### Property details

#### `Gradle` `gradle` (read-only)

Returns this `Gradle` instance. This method is useful in init scripts to explicitly access Gradle properties and methods. For example, using `gradle.parent` can express your intent better than using `parent`. This property also allows you to access Gradle properties from a scope where the property may be hidden, such as, for example, from a method or closure.

#### `File` `gradleUserHomeDir` (read-only)

The Gradle user home directory. This directory is used to cache downloaded resources.

#### `String` `gradleVersion` (read-only)

The current Gradle version.

#### `Gradle` `parent` (read-only)

The parent build of this build, if any.

#### `Project` `rootProject` (read-only)

The root project of this build.

#### `StartParameter` `startParameter` (read-only)

The `StartParameter` used to start this build.

#### `TaskExecutionGraph` `taskGraph` (read-only)

The `TaskExecutionGraph` for this build.

### Script blocks

No script blocks

### Methods

Method | Description
`addBuildListener(buildListener)` | Adds a `BuildListener` to this Build instance. The listener is notified of events which occur during the execution of the build.
`addListener(listener)` | Adds the given listener to this build. The listener may implement any of the given listener interfaces:
`addProjectEvaluationListener(listener)` | Adds a listener to this build, to receive notifications as projects are evaluated.
`afterProject(closure)` | Adds a closure to be called immediately after a project is evaluated. The project is passed to the closure as the first parameter. The project evaluation failure, if any, is passed as the second parameter. Both parameters are optional.
`beforeProject(closure)` | Adds a closure to be called immediately before a project is evaluated. The project is passed to the closure as a parameter.
`buildFinished(closure)` | Adds a closure to be called when the build is completed. All selected tasks have been executed. A `BuildResult` instance is passed to the closure as a parameter.
`projectsEvaluated(closure)` | Adds a closure to be called when all projects for the build have been evaluated. The project objects are fully configured and are ready to use to populate the task graph. This `Gradle` instance is passed to the closure as a parameter.
`projectsLoaded(closure)` | Adds a closure to be called when the projects for the build have been created from the settings. None of the projects have been evaluated. This `Gradle` instance is passed to the closure as a parameter.
`removeListener(listener)` | Removes the given listener from this build.
`removeProjectEvaluationListener(listener)` | Removes the given listener from this build.
`settingsEvaluated(closure)` | Adds a closure to be called when the build settings have been loaded and evaluated. The settings object is fully configured and is ready to use to load the build projects. The `Settings` object is passed to the closure as a parameter.
`useLogger(logger)` | Uses the given object as a logger. The logger object may implement any of the listener interfaces supported by `Gradle.addListener()`. Each listener interface has exactly one associated logger. When you call this method with a logger of a given listener type, the new logger will replace whichever logger is currently associated with the listener type. This allows you to selectively replace the standard logging which Gradle provides with your own implementation, for certain types of events.

### Method details

#### `void` `addBuildListener`(`BuildListener` buildListener)

Adds a `BuildListener` to this Build instance. The listener is notified of events which occur during the execution of the build.

#### `void` `addListener`(`Object` listener)

Adds the given listener to this build. The listener may implement any of the given listener interfaces:

- `BuildListener`
- `TaskExecutionGraphListener`
- `ProjectEvaluationListener`
- `TaskExecutionListener`
- `TaskActionListener`
- `StandardOutputListener`
- `TestListener`
- `DependencyResolutionListener`

#### `ProjectEvaluationListener` `addProjectEvaluationListener`(`ProjectEvaluationListener` listener)

Adds a listener to this build, to receive notifications as projects are evaluated.

#### `void` `afterProject`(`Closure` closure)

Adds a closure to be called immediately after a project is evaluated. The project is passed to the closure as the first parameter. The project evaluation failure, if any, is passed as the second parameter. Both parameters are optional.

#### `void` `beforeProject`(`Closure` closure)

Adds a closure to be called immediately before a project is evaluated. The project is passed to the closure as a parameter.

#### `void` `buildFinished`(`Closure` closure)

Adds a closure to be called when the build is completed. All selected tasks have been executed. A `BuildResult` instance is passed to the closure as a parameter.

#### `void` `projectsEvaluated`(`Closure` closure)

Adds a closure to be called when all projects for the build have been evaluated. The project objects are fully configured and are ready to use to populate the task graph. This `Gradle` instance is passed to the closure as a parameter.

#### `void` `projectsLoaded`(`Closure` closure)

Adds a closure to be called when the projects for the build have been created from the settings. None of the projects have been evaluated. This `Gradle` instance is passed to the closure as a parameter.

An example of hooking into the projectsLoaded to configure buildscript classpath from the init script.

    //init.gradle
    gradle.projectsLoaded {
      rootProject.configure(rootProject) {
        buildscript {
          repositories {
            //...
          }
          dependencies {
            //...
          }
        }
      }
    }

#### `void` `removeListener`(`Object` listener)

Removes the given listener from this build.

#### `void` `removeProjectEvaluationListener`(`ProjectEvaluationListener` listener)

Removes the given listener from this build.

#### `void` `settingsEvaluated`(`Closure` closure)

Adds a closure to be called when the build settings have been loaded and evaluated. The settings object is fully configured and is ready to use to load the build projects. The `Settings` object is passed to the closure as a parameter.

#### `void` `useLogger`(`Object` logger)

Uses the given object as a logger. The logger object may implement any of the listener interfaces supported by `Gradle.addListener()`. Each listener interface has exactly one associated logger. When you call this method with a logger of a given listener type, the new logger will replace whichever logger is currently associated with the listener type. This allows you to selectively replace the standard logging which Gradle provides with your own implementation, for certain types of events.