skip to content

Build Scripts and DSL

The scripting layer: Groovy versus Kotlin DSL, what belongs in a settings script versus a build script, and building blocks like buildscript and extra properties. Interviewers start here to see whether you read build scripts as code or as inert configuration.

on this pageshow

explore

questions

85 · 3 sections

In a build.gradle Groovy script, what is a block like dependencies { ... } actually, and how does Gradle execute it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It's a Groovy closure passed to a method named dependencies. Gradle calls that method, runs the closure, and the closure's calls configure the dependencies object.

open as a page

What is the `ext` namespace in a Groovy build script, and why do you need it to define your own custom project properties?

level: juniorimportance: must knowfreq 62%
basics
~20 s

ext is Gradle's extra-properties container. You write ext.myVersion = '1.0' to add a custom property, then read it as myVersion anywhere in the project. It exists because Project only stores arbitrary user properties through this namespace.

open as a page

You're starting to migrate a build.gradle to build.gradle.kts. What are the first mechanical changes you make to the file — the filename and the most common syntax fixes?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Rename build.gradle to build.gradle.kts. Then wrap all strings in double quotes (Kotlin has no single-quote strings), and replace each '=' or Groovy assignment so it follows Kotlin syntax.

open as a page

How do you apply plugins in build.gradle.kts using the plugins {} block, and what makes that block special?

level: juniorimportance: must knowfreq 65%
basics
~10 s

Use the plugins {} block: list plugins by id and version, e.g. id("org.springframework.boot") version "3.x", or use built-in accessors like kotlin("jvm") or java. It applies plugins declaratively at the top of the script.

open as a page

What is build.gradle.kts, and how does it differ from build.gradle when describing a Gradle project?

level: juniorimportance: must knowfreq 70%
basics
~20 s

build.gradle.kts is a Gradle build script written in Kotlin (the Kotlin DSL), instead of build.gradle which uses the Groovy DSL. Both configure the same project; the .kts file is Kotlin source compiled before the build runs.

open as a page

Why must the plugins {} block appear first in a build.gradle(.kts) file, and what happens if you put code before it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The plugins {} block must be the first statement (after any buildscript/pluginManagement). Gradle parses it specially and early to resolve plugins. Putting other code before it causes a build failure.

open as a page

What is the dependencyResolutionManagement {} block, and which Gradle build file does it belong in?

level: juniorimportance: must knowfreq 58%
basics
~10 s

It is a block in settings.gradle(.kts) — not build.gradle — where you centrally declare repositories and version catalogs for the whole build instead of repeating them in each module.

open as a page

What is a Gradle init script, and what are the different ways Gradle discovers and applies one?

level: juniorimportance: must knowfreq 55%
basics
~10 s

An init script runs before the build itself, letting you configure Gradle globally. Gradle applies it from ~/.gradle/init.gradle(.kts), any file in ~/.gradle/init.d/, or via the --init-script command-line flag.

open as a page

What is the pluginManagement {} block and where must it appear in a Gradle build?

level: juniorimportance: must knowfreq 62%
basics
~10 s

It is a block in settings.gradle(.kts) that configures how plugins are resolved. It must be the first block in the settings file, before anything else.

open as a page

What is a script plugin in Gradle, and how do you apply one?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A script plugin is a plain Gradle script (e.g. other.gradle.kts) holding shared build logic. You apply it with apply(from = "other.gradle.kts") in Kotlin DSL or apply from: 'other.gradle' in Groovy.

open as a page

What is the buildscript {} block in a Gradle build script, and what goes inside it?

level: juniorimportance: must knowfreq 55%
basics
~10 s

buildscript {} configures the build script itself: it declares repositories {} and dependencies { classpath(...) } that put plugin and helper jars on the classpath the script needs to compile and run.

open as a page

What is a Gradle extension, and how does adding one give a plugin its own configuration block in the build script?

level: juniorimportance: must knowfreq 70%
basics
~10 s

An extension is an object a plugin registers via project.extensions.create("name", Type::class.java). Gradle then exposes a build-script block named after it (e.g. name { ... }) that configures that object.

open as a page

What are Gradle's extra (ext) properties, and how do you declare and read them in a build script?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Extra properties are arbitrary user-defined key/value pairs attached to a Gradle object. You set them with ext { key = value } (Groovy) or by extra (Kotlin) and read them back by name later in the script.

open as a page

What is the gradle.properties file used for, and how does it differ from passing -P on the command line?

level: juniorimportance: must knowfreq 70%
basics
~20 s

gradle.properties stores key=value project properties persisted in the repo (or ~/.gradle for the machine). -P sets the same kind of project property ad-hoc on one command line. Both surface as project properties; -P overrides the file.

open as a page

Under the configuration cache, how do you replace project.exec, project.javaexec, and project.copy inside a task action?

level: middleimportance: must knowfreq 50%
basics
~10 s

Inject ExecOperations and call execOps.exec/javaexec for processes, and inject FileSystemOperations and call fsOps.copy/sync/delete for files. These replace the project.* calls that aren't allowed at execution time with the configuration cache on.

open as a page