Skip to content

Gradle build script structure

A Gradle build script defines the configuration and execution logic for a project, typically written in Groovy or Kotlin DSL within files named build.gradle or build.gradle.kts.^[600-developer-gradle-gradle-build.md] The structure of these scripts revolves around key blocks and APIs that manage dependencies, project properties, and task execution.

Project API

The central entity in a Gradle script is the Project object (implicit in the script), which provides APIs for file manipulation and project configuration.^[600-developer-gradle-gradle-build.md]

File Operations

Scripts can locate and manipulate files using methods provided by the Project API:

  • File Locating: The file() method locates files relative to the current project directory.^[600-developer-gradle-gradle-build.md]
  • File Copying: File copying is managed via the CopySpec interface (e.g., using the copy task), allowing for detailed specification of source files and destination directories.^[600-developer-gradle-gradle-build.md]
  • File Traversal: Scripts can iterate over file structures using FileTreeElement to process directories and contents dynamically.^[600-developer-gradle-gradle-build.md]

Script Blocks

buildscript

The buildscript {} block is used to define the configuration for the build script itself.^[600-developer-gradle-gradle-build.md] This is where repositories and classpath dependencies required for the build logic (e.g., plugins) are declared.

Project Structure

Gradle scripts support specific blocks for structuring multi-project builds:

  • allprojects: Configures the current project and all its sub-projects.^[600-developer-gradle-gradle-build.md]
  • subprojects: Configures only the sub-projects of the current project.^[600-developer-gradle-gradle-build.md]

Task Definition

Tasks are the fundamental units of work in a Gradle build.

  • Creation: Tasks are defined using the task keyword (e.g., task myTask).^[600-developer-gradle-gradle-build.md]
  • Lifecycle Actions: Logic can be attached to the execution phase using doFirst and doLast blocks.^[600-developer-gradle-gradle-build.md]
  • Dependencies: The order of execution is controlled using dependsOn, ensuring a specific task runs only after its dependencies complete.^[600-developer-gradle-gradle-build.md]
  • Inputs and Outputs: Tasks can declare inputs and outputs, which Gradle uses to determine if a task is up-to-date and can be skipped (UP-TO-DATE check).^[600-developer-gradle-gradle-build.md]

External Configuration

Build scripts can be modularized by applying logic from external files or other projects.

  • Apply from: External script files can be applied to the current build using apply from: file("filename.[Gradle](<./gradle.md>)").^[600-developer-gradle-gradle-build.md]
  • Project References: Specific modules or sub-projects can be manually imported and configured using the syntax project(':moduleName') { ... }.^[600-developer-gradle-gradle-build.md]
  • [[Dependency Management]]
  • [[Software Build Automation]]

Sources

^[600-developer-gradle-gradle-build.md]