Writing Settings Files
The settings file is the entry point of every Gradle build.
Early in the Gradle Build lifecycle, the initialization phase finds the settings file in your project root directory.
When the settings file settings.gradle(.kts)
is found, Gradle instantiates a Settings
object.
One of the purposes of the Settings
object is to allow you to declare all the projects to be included in the build.
Settings Scripts
The settings script is either a settings.gradle
file in Groovy or a settings.gradle.kts
file in Kotlin.
Before Gradle assembles the projects for a build, it creates a Settings
instance and executes the settings file against it.
As the settings script executes, it configures this Settings
.
Therefore, the settings file defines the Settings
object.
There is a one-to-one correspondence between a Settings instance and a settings.gradle(.kts) file.
|
The Settings
Object
The Settings
object is part of the Gradle API.
Many top-level properties and blocks in a settings script are part of the Settings API.
For example, we can set the root project name in the settings script using the Settings.rootProject
property:
settings.rootProject.name = "application"
Which is usually shortened to:
rootProject.name = "application"
rootProject.name = 'application'
Standard Settings
properties
The Settings
object exposes a standard set of properties in your settings script.
The following table lists a few commonly used properties:
Name | Description |
---|---|
|
The build cache configuration. |
|
The container of plugins that have been applied to the settings. |
|
The root directory of the build. The root directory is the project directory of the root project. |
|
The root project of the build. |
|
Returns this settings object. |
The following table lists a few commonly used methods:
Name | Description |
---|---|
|
Adds the given projects to the build. |
|
Includes a build at the specified path to the composite build. |
Settings Script structure
A Settings script is a series of method calls to the Gradle API that often use { … }
, a special shortcut in both the Groovy and Kotlin languages.
A { }
block is called a lambda in Kotlin or a closure in Groovy.
Simply put, the plugins{ }
block is a method invocation in which a Kotlin lambda object or Groovy closure object is passed as the argument.
It is the short form for:
plugins(function() {
id("plugin")
})
Blocks are mapped to Gradle API methods.
The code inside the function is executed against a this
object called a receiver in Kotlin lambda and a delegate in Groovy closure.
Gradle determines the correct this
object and invokes the correct corresponding method.
The this
of the method invocation id("plugin")
object is of type PluginDependenciesSpec
.
The settings file is composed of Gradle API calls built on top of the DSLs. Gradle executes the script line by line, top to bottom.
Let’s take a look at an example and break it down:
pluginManagement { (1)
repositories {
gradlePluginPortal()
}
}
plugins { (2)
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = "simple-project" (3)
dependencyResolutionManagement { (4)
repositories {
mavenCentral()
}
}
include("sub-project-a") (5)
include("sub-project-b")
include("sub-project-c")
pluginManagement { (1)
repositories {
gradlePluginPortal()
}
}
plugins { (2)
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = 'simple-project' (3)
dependencyResolutionManagement { (4)
repositories {
mavenCentral()
}
}
include("sub-project-a") (5)
include("sub-project-b")
include("sub-project-c")
1 | Define the location of plugins |
2 | Apply settings plugins. |
3 | Define the root project name. |
4 | Define dependency resolution strategies. |
5 | Add subprojects to the build. |
1. Define the location of plugins
The settings file can manage plugin versions and repositories for your build using the pluginManagement
block.
It provides a way to define which plugins should be used in your project and from which repositories they should be resolved.
pluginManagement { (1)
repositories {
gradlePluginPortal()
}
}
pluginManagement { (1)
repositories {
gradlePluginPortal()
}
}
2. Apply settings plugins
The settings file can optionally apply plugins that are required for configuring the settings of the project. These are commonly the Develocity plugin and the Toolchain Resolver plugin in the example below.
Plugins applied in the settings file only affect the Settings
object.
plugins { (2)
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
plugins { (2)
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
3. Define the root project name
The settings file defines your project name using the rootProject.name
property:
rootProject.name = "simple-project" (3)
rootProject.name = 'simple-project' (3)
There is only one root project per build.
4. Define dependency resolution strategies
The settings file can optionally define rules and configurations for dependency resolution across your project(s). It provides a centralized way to manage and customize dependency resolution.
dependencyResolutionManagement { (4)
repositories {
mavenCentral()
}
}
dependencyResolutionManagement { (4)
repositories {
mavenCentral()
}
}
You can also include version catalogs in this section.
5. Add subprojects to the build
The settings file defines the structure of the project by adding all the subprojects using the include
statement:
include("sub-project-a") (5)
include("sub-project-b")
include("sub-project-c")
include("sub-project-a") (5)
include("sub-project-b")
include("sub-project-c")
You can also include entire builds using includeBuild
.
Settings File Scripting
There are many more properties and methods on the Settings
object that you can use to configure your build.
It’s important to remember that while many Gradle scripts are typically written in short Groovy or Kotlin syntax, every item in the settings script is essentially invoking a method on the Settings
object in the Gradle API:
include("app")
Is actually:
settings.include("app")
Additionally, the full power of the Groovy and Kotlin languages is available to you.
For example, instead of using include
many times to add subprojects, you can iterate over the list of directories in the project root folder and include them automatically:
rootDir.listFiles().filter { it.isDirectory && (new File(it, "build.gradle.kts").exists()) }.forEach {
include(it.name)
}
This type of logic should be developed in a plugin. |
Next Step: Learn how to write Build scripts >>