Tasks and Task Graph
Tasks as the unit of work: how they are declared, how dependsOn, mustRunAfter, and finalizedBy shape the DAG, and how declared inputs and outputs drive up-to-date checks. The core Gradle topic, since everything else is layered on it.
on this pageshowhide
explore
- Task Declaration & Registration36 questions
- register (Lazy) vs create (Eager)5 questions
- Configuration Avoidance API5 questions
- Locating Tasks: named & withType5 questions
- File Tasks: Copy, Sync, Delete5 questions
- Archive & Exec Tasks: Zip, Tar, Exec5 questions
- Ad-hoc & DefaultTask Tasks5 questions
- Build Init6 questions
- Task Dependencies & Ordering27 questions
- Explicit dependsOn5 questions
- Inferred Dependencies via Provider Outputs6 questions
- Ordering: mustRunAfter & shouldRunAfter5 questions
- finalizedBy5 questions
- Dependency Graph Resolution & Cycles6 questions
- Inputs, Outputs & Up-to-Date20 questions
- Declaring Inputs & Outputs5 questions
- Up-to-Date Checking5 questions
- Custom outputs.upToDateWhen5 questions
- Forcing Rerun: --rerun-tasks & --rerun5 questions
- Task Actions & Graph API20 questions
- doFirst & doLast Action Lists5 questions
- gradle.taskGraph & whenReady5 questions
- Requested vs Executed Tasks5 questions
- Conditional Execution: onlyIf5 questions
questions
103 · 4 sectionsWhat does the Gradle `init` task do, and how do you use it to scaffold a new project?
basics
~20 sgradle init is a built-in task from the build-init plugin that scaffolds a new Gradle project: it generates build/settings scripts, a sample source layout, and the Gradle wrapper, prompting you (or via flags) for project type, DSL, and test framework.
What is Gradle's Configuration Avoidance API, and what problem does it solve?
basics
~10 sIt's a set of lazy task APIs (TaskProvider, tasks.named, configureEach) that let Gradle skip creating and configuring tasks you don't run, cutting configuration-time cost.
How do you define a task in Gradle that copies files from one directory into another, and what are the core DSL elements you use?
basics
~10 sRegister a task of type Copy and configure from (source) and into (destination). Gradle copies the matched files into the target directory when the task runs.
What is the difference between doFirst and doLast on a task, and in what order do multiple actions run?
basics
~10 sdoLast adds an action to the end of the task's action list; doFirst adds one to the front. Both run in the execution phase. If you call doFirst twice, the later one runs first.
What is an ad-hoc task in Gradle, and how do you define one?
basics
~10 sAn ad-hoc task is an untyped task of type DefaultTask whose behavior you add inline with doLast { ... }. You register it with tasks.register("name") and put the action in the closure.
What does the dependsOn declaration do for a Gradle task, and what is its effect on the build?
basics
~10 sdependsOn declares that another task must run before this one. Gradle adds the named task to the build's task graph and executes it first whenever this task is requested.
What does finalizedBy do in Gradle, and when would you use it?
basics
~10 sfinalizedBy declares that a finalizer task must run after a given task, even if that task fails. It's used for cleanup or teardown work, like releasing resources or generating a report after tests.
How can you preview the ordered list of tasks Gradle will execute for a given command without actually running them?
basics
~10 sRun the build with the --dry-run (-m) flag, e.g. gradle build --dry-run. Gradle resolves and prints the full task execution order with each task marked SKIPPED, but executes nothing.
What is the difference between an ordering rule (mustRunAfter/shouldRunAfter) and a task dependency in Gradle?
basics
~20 sAn ordering rule only fixes the relative order of two tasks IF both already end up in the task graph. It never adds a task to the graph or forces it to run, unlike a dependency, which both pulls a task in and runs it first.
What are the different ways to specify the argument to dependsOn, and why is passing a TaskProvider preferred over a task name string?
basics
~20 sYou can pass a task name string, a Task or TaskProvider reference, a Provider, a Collection, or a closure. A TaskProvider is preferred because it is lazy — it doesn't force the other task to be created/configured eagerly.
Why would you declare inputs and outputs on an ad-hoc Gradle task using task.inputs and task.outputs?
basics
~10 sDeclaring inputs/outputs lets Gradle skip the task when nothing changed (up-to-date checking) and reuse cached results. Without them Gradle has no fingerprint, so the task always runs.
What does the --rerun-tasks command-line flag do, and when would you reach for it?
basics
~10 s--rerun-tasks tells Gradle to ignore up-to-date checks for the whole build and re-execute every task in the requested task graph, even tasks it considers UP-TO-DATE. Useful to force a clean re-run while debugging.
When you run a Gradle build and see a task labeled UP-TO-DATE in the output, what does that tell you and why did it happen?
basics
~10 sUP-TO-DATE means Gradle skipped running the task because its inputs and outputs haven't changed since the last run, so re-executing would produce identical results.
How do you make a Gradle task always run (never be considered up-to-date), and when would you do that?
basics
~20 sAdd outputs.upToDateWhen { false } to the task. The predicate always returns false, so Gradle never treats the task as up-to-date and runs it every build. Useful for tasks with side effects Gradle can't track.
What is the difference between inputs.property(...) and inputs.file(...) on an ad-hoc task, and when do you use each?
basics
~10 sinputs.file declares a file/path whose contents are fingerprinted. inputs.property declares a non-file value (string, number, flag) compared by its serialized value. Use file for file inputs, property for config values.
In a Gradle task definition, what is the difference between code in the configuration block and code inside doFirst{}/doLast{}, and when does each run?
basics
~10 sConfiguration-block code runs during the configuration phase for every build invocation. doFirst{}/doLast{} are action closures that run only in the execution phase, and only if the task actually executes.
What does task.onlyIf {} do in Gradle, and what happens to a task whose onlyIf predicate returns false?
basics
~10 sonlyIf {} attaches a predicate evaluated right before the task runs. If it returns false, Gradle skips the task's actions and reports it as SKIPPED. If true (or absent), the task executes normally.
What is the difference between the tasks a user requested on the command line and the tasks Gradle actually schedules and executes?
basics
~10 sRequested tasks are the names you type on the command line (e.g. gradle build). Executed tasks are those plus all their dependencies, which Gradle computes and runs in dependency order.
What is gradle.taskGraph.whenReady{} and when does the closure it registers actually run?
basics
~10 swhenReady registers a callback that runs once Gradle has finished building the task execution graph for the build, after configuration but before any task executes.
How do doFirst{} and doLast{} compose into a task's action list, and what is the execution order if you call them multiple times?
basics
~20 sA task holds an ordered list of actions. doLast appends to the end; doFirst prepends to the front. Multiple doFirst calls stack so the last-added doFirst runs first; doLast actions run in the order added.