Gradle Concepts
External
- DSL reference https://docs.gradle.org/current/dsl/
Internal
Overview
Gradle is a general-purpose build tool, which can build pretty much anything its configuration scripts declare. It is primarily used to build Java and Groovy, but it can build other languages as well.
Build Lifecycle
Each build run results in the instantiation of one or more of these core Gradle types: the build starts with instantiation of a Settings and a Gradle instance, then of one or more Projects. A Gradle execution results in instantiation and execution of task. Depending on the actual configuration of the build, many others objects are instantiated and used. All Gradle core types are listed here.
The lifecycle of a Gradle build consists in an initialization, configuration and an execution phase. Code can be written to react to a build's lifecycle events:
Initialization
The build lifecycle starts with the initialization phase, when Gradle creates a Settings instance, then executes the settings script, which updates the state of its Settings delegate. In this phase, Gradle determines which projects are going to take part in the build and creates a project representation for each of the projects, essentially defining the build's project hierarchy. The sub-projects are declared using the "include" or "includeFlat" Settings methods in settings.gradle.
The corresponding Project instances are created and arranged in the corresponding project hierarchy. Note that at the initialization phase, Project instances are not accessible from the settings script. Project data can be accessed via a ProjectDescriptor interface instead.
Configuration
The build continues with a configuration phase. Gradle executes the build.gradle configuration script against the root project instance and its sub-projects. As part of the configuration sequence, plugins declared with apply(...) in the build scripts are downloaded and initialized in their corresponding Project instances. Note that prior to Gradle 1.4, all projects that are part of the hierarchy are configured. Gradle 1.4 and newer introduces configuration on demand, that results in configuring only relevant projects.
Execution
The execution phase requires that one or more tasks is specified on command line. Gradle computes a task Directed Acyclic Graph of to be executed in order to fulfill the specified tasks, and then executes them honoring inter-task dependencies and insuring the fact that a task is executed only once.
Configuration Scripts
The configuration scripts are written in the Gradle build language, which is a DSL and a Groovy extension. A build is configured by three types of configuration scripts, which together express build requirements and inform the build runtime: build scripts, a settings script and an init script.
Each of those scripts contains executable statements and script blocks, which are a special case of executable statement, consisting in a method invocation on a closure. The statements and script blocks are executed in the order in which they are declared in the script. The logic expressed in a specific script applies to its delegate object. Though syntax is similar for all three scripts, specific blocks must make sense with the delegate object. The corresponding delegate objects for all three types of configuration scripts will be described below.
A configuration script may contain statements, script blocks, and any elements allowed in a Groovy script, such as method and class definitions.
A statement may be a method call, property assignment and local variable definition. One of the simplest possible statements, which will be executed in the order in which is found in the script, is:
println 'I am here'
If the delegate object has an accessor that follows Java Beans conventions, then the result of the accessor invocation can be obtained simply specifying the corresponding variable name. For example, the root project instance returned by:
public interface Settings ... {
ProjectDescriptor getRootProject();
}
can be accessed from the corresponding settings.gradle script with rootProject.
A script block is method call that takes a configuration closure as an argument. Executing the script block results in modification of the associated delegate object, based on the content of the closure. Equivalent terminology, often used in documentation, is to delegate the closure against the target object. This means that the closure will be executed in scope of the class of the target instance. For all Groovy closures, it is the default parameter passed to the closure. A build configuration that does not declare any plugin has a set of standard script blocks. Plugins may, and usually do, add new script blocks.
Each configuration script implements the Script interface, which exposes a number of useful methods and properties.
Settings Script and Setting Instance
Build Script and Project Instance
Init Script
Artifacts and Artifact Publishing
A Gradle project produces artifacts, which are the files the project provides to the outside world. An artifact may be built locally by a plugin such as the Application plugin, which in turn delegates the artifact building part to Distribution plugin, or by other plugins. This creates the artifact locally, in the project build area. However, in most cases, we want these artifacts to be pushed to external repositories, as described here:
Projects and Multi-Project Builds
A Project is the main API to use to interact with Gradle. All top level statements within a build script are delegated to the corresponding Project instance. Multi-project builds consist of multiple Project instances.
The Gradle Object Instance
Task
A Task represents a single atomic piece of work for a build. A build consists in executing a sequence of tasks in succession, after the task have been arranged in a directed acrylic graph. Understanding how the directed acyclic graph is built and how tasks are scheduled for execution is key to understanding how gradle works. These concepts are explained here:
Variables and Properties
Module
A module is a piece of software that evolves over time. Every module has a name, and each release of a module is identified by a module version. Modules can be hosted in repositories. Modules usually are described by module metadata, which is information associate with a module. Metadata includes coordinates for locating the module in a repository (group ID, artifact name and version), information about the module's transitive dependencies, authors, etc. In Maven, the metadata file is called POM.
A module version is a label that designates a distinct set of changes released as a unit. The most widely-used versioning strategy is semantic versioning.
Dependencies and Dependency Configurations
Configuration, in this context, does not refer to a configuration file, as in a settings configuration or a build configuration. It refers to named set of dependencies or artifacts, and it can probably thought of as being a concept similar to a Maven scope.
Repository
Plugins
Extensions
A Project implements ExtensionAware. Some plugins create extensions. For example, maven-plugin creates a "publishing" extension, the Distribution plugin creates a "distribution" extension.
Software Component
A software components may be added by a plugin. For example the Java plugin adds a "java" component, accessible via components.java. The artifact associated with the Java component is the generated JAR file, and the dependencies are those of the runtime configurations. The War plugin adds a "web" software component whose artifact is the generated WAR file, with no dependencies.
The available components can be displayed with:
task util {
components.each { println it.name }
}
The Gradle Wrapper
Gradle Wrapper is the recommended way to execute a Gradle build. The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary.
./gradlew ...
The wrapper caches the appropriate Gradle runtime under $USER_HOME/.gradle/wrapper/dists/. Using the wrapper effectively locks a specific Gradle version to the project. Per-project graddle wrapper infrastructure can be created with gradle init.
One of the advantages of using the Gradle wrapper is that other people that attempt to build the project do not have to have Gradle installed on their system, everything Gradle needs to run comes with the wrapper.
The Gradle Daemon
Configured with "org.gradle.daemon" and "org.gradle.daemon.idletimeout" Gradle property. On by default.