Gradle tasks and DSL
What you will learn
Gradle builds a graph of tasks from settings, project scripts and plugins. Kotlin DSL (.gradle.kts) and Groovy DSL (.gradle) describe the same build model using different languages.
- Distinguish initialization, configuration and execution.
- Declare a task dependency and select a qualified task path.
- Explain the wrapper pin, Java toolchain and compiler release.
Prerequisite: Java packages, classpath and basic terminal compilation. Gradle CLI examples are static and not executed in this batch. The project is local and fictional; use only reviewed source and approved cached dependencies.
Two tasks, one dependency
plugins { base }val prepare = tasks.register("prepareDemo") { doLast { println("Prepare") }}tasks.register("demo") { dependsOn(prepare) doLast { println("Demo") }}Complete root build script in the bundled project. Predicted gradle --offline :demo prints Prepare then Demo (among Gradle logs). Task execution not observed.
From settings to task graph
Settings names the root and includes subprojects. During initialization Gradle discovers participating projects; configuration evaluates needed build logic and constructs the graph; execution runs selected tasks and prerequisites. A plugin adds behavior and conventions; it is executable code. Maven selects a lifecycle phase and its bindings, while Gradle selects tasks and their dependencies. Do not equate Maven package with an arbitrary Gradle task. tasks.register returns a lazy provider; doLast runs an action during execution, not configuration. dependsOn adds a prerequisite; mustRunAfter only orders tasks already selected. File-producing tasks should wire declared outputs to consuming inputs, not rely on ordering alone.
Version and trust boundary
This example selects historical Gradle 9.2.0, documented to run on Java 21 (and Java 25); Gradle 8.5 was the first release supporting running on Java 21. Local tests use JDK 21.0.12.1. Toolchain selects the compiler and relevant execution tools; options.release limits language, classfile and platform APIs. It does not choose the JVM running Gradle. Kotlin DSL configures Java even when no Kotlin application code exists. Groovy DSL is an alternative, not a second file to apply concurrently.
Wrapper and offline execution
Use a reviewed distribution, scripts and wrapper JAR; pin the distribution URL and SHA-256 from the official checksum list, independently verify the wrapper JAR, and preprovision an approved installation. The included properties are instructional only: no wrapper scripts or binary are included. Offline mode is not a sandbox and does not prevent wrapper bootstrapping from downloading a missing distribution. Never execute arbitrary build scripts or unreviewed init scripts. Historical version is a compatibility example, not a security recommendation; review current advisories before approving any version.
Choose projects
Complete settings file: project paths :core and :app. Merely including a project does not add its classes to another project.
Groovy alternative
// Alternative build.gradle (not used alongside build.gradle.kts)tasks.register('hello') { doLast { println 'Hello' }}Standalone alternative script. Predicted hello task prints Hello. Do not copy into the bundled Kotlin DSL project.
Pin a distribution
distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-bin.zipdistributionSha256Sum=df67a32e86e3276d011735facb1535f64d0d88df84fa87521e90becc2d735444zipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsOfficial historical binary distribution SHA-256; no distribution or wrapper JAR is bundled or downloaded. Verify the approved tool and script independently.
Common mistakes
Putting println outside doLast makes it configuration behavior, not an action. dependsOn selects a prerequisite; mustRunAfter alone does not. Do not assume offline mode or the wrapper checksum makes arbitrary build logic safe.
Practice
Draw the graph for :demo, :prepareDemo and :app:build. Explain which tasks are selected by :demo and which remain unrelated. Describe the three separate JDK/version controls.
Use the manual practice hints; explain your reasoning before consulting the private review solution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gradle tasks and DSL — knowledge check
Gradle model and build engineering knowledge check; source-reviewed, not Gradle-executed.
What does settings.gradle.kts primarily describe?
When does a doLast action run?
What does mustRunAfter do alone?
Which control constrains Java platform APIs?
Does offline mode prevent all wrapper downloads?