# Chapter 44. Initialization Scripts

Gradle provides a powerful mechanism to allow customizing the build based on the current environment. This mechanism also supports tools that wish to integrate with Gradle.

## 44.1. Basic usage

Initialization scripts (a.k.a. init scripts) are similar to other scripts in Gradle. These scripts, however, are run before the build starts. Here are several possible uses:

- Set up properties based on the current environment (such as a developer's machine vs. a continuous integration server).

- Supply personal information about the user to the build, such as repository or database authentication credentials.

- Define machine specific details, such as where JDKs are installed.

- Register build listeners. External tools that wish to listen to Gradle events might find this useful.

- Register build loggers. You might wish to customise how Gradle logs the events that it generates.

One main limitation of init scripts is that they cannot access classes in the buildSrc project (see Section 43.3, “Build sources in the `buildSrc` project” for details of this feature).

There are two ways to use init scripts. Either put a file called `init.gradle` in ``USER_HOME`/.gradle`, or specify the file on the command line. The command line option is `-I` or `--init-script` followed by the path to the script. The command line option can appear more than once, each time adding another init script. If more than one init script is found they will all be executed. This allows for a tool to specify an init script and the user to put home in their home directory for defining the environment and both scripts will run when gradle is executed.

## 44.2. Writing an init script

Similar to a Gradle build script, an init script is a groovy script. Each init script has a `Gradle` instance associated with it. Any property reference and method call in the init script will delegate to this `Gradle` instance.

Each init script also implements the `Script` interface.

Below sample shows how to perform extra configuration from an init script after the project is evaluated. One of the teams was using this feature to configure a certain repository only for their local environments. They didn't want to check-in this repository configuration into the VCS so they kept it configured in an init script.

Example 44.1. Using init script to perform extra configuration after project is evaluated

`build.gradle`

    repositories {
        mavenCentral()
    }

    task showRepos << {
        println "All repos:"
        println repositories.collect { it.name }
    }

`init.gradle`

    afterProject { project ->
        project.repositories {
            mavenLocal()
        }
    }

Output of `gradle --init-script init.gradle -q showRepos`

    > gradle --init-script init.gradle -q showRepos
    All repos:
    [MavenRepo, MavenLocal]

## 44.3. External dependencies for the init script

In Section 43.5, “External dependencies for the build script” is was explained how to add external dependencies to a build script. Init scripts can similarly have external dependencies defined. You do this using the `initscript()` method, passing in a closure which declares the init script classpath.

Example 44.2. Declaring external dependencies for an init script

`init.gradle`

    initscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath group: 'org.apache.commons', name: 'commons-math', version: '2.0'
        }
    }

The closure passed to the `initscript()` method configures a `ScriptHandler` instance. You declare the init script classpath by adding dependencies to the `classpath` configuration. This is the same way you declare, for example, the Java compilation classpath. You can use any of the dependency types described in Section 35.3, “How to declare your dependencies”, except project dependencies.

Having declared the init script classpath, you can use the classes in your init script as you would any other classes on the classpath. The following example adds to the previous example, and uses classes from the init script classpath.

Example 44.3. An init script with external dependencies

`init.gradle`

    import org.apache.commons.math.fraction.Fraction

    initscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath group: 'org.apache.commons', name: 'commons-math', version: '2.0'
        }
    }

    println Fraction.ONE_FIFTH.multiply(2)

Output of `gradle --init-script init.gradle -q doNothing`

    > gradle --init-script init.gradle -q doNothing
    2 / 5