Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Build Scripts: Groovy and Kotlin DSL

Groovy DSL Basics: Reading build.gradle Files

Read and write build.gradle with confidence: Groovy syntax for strings, closures, method calls without parentheses, maps, plugins and dependencies blocks.

Gradle build scripts are not configuration files; they are programs written in a domain-specific language (DSL). For years the only choice was Groovy, a flexible language for the JVM, and build.gradle files are still everywhere: older projects, many open source libraries, tutorials, Stack Overflow answers. Even if you prefer Kotlin, you must be able to read Groovy. This lesson covers the small subset of Groovy that build scripts use.

Groovy itself cannot run on this site, so the Groovy snippets are read-only. The stand-ins show in Java how Gradle's "closure with a receiver" blocks work.

Strings

Groovy has two kinds of quotes with a real difference:

GROOVY
def name = 'api'                  // single quotes: plain string, no interpolation
def greeting = "Hello, ${name}"   // double quotes: interpolates ${...}
def shortForm = "Hello, $name"    // $name works for simple names

def multi = """
  Several
  lines
"""

Beginners often use double quotes everywhere; that is fine, but single quotes are the usual convention for strings with no interpolation. A string with ${} in double quotes is a GString; Gradle accepts it nearly everywhere a String is expected.

Variables and properties

GROOVY
def javaVersion = 21               // local variable, only visible in this script
ext.springVersion = '3.4.0'         // extra property on the project (old style)
version = '1.0.0'                  // assigns the project property 'version'
group = 'com.example'

def declares a local. Without def, an assignment like version = '1.0.0' sets a property of the project. The old ext { } block is replaced in modern builds by gradle.properties, version catalogs and typed extensions (see Properties).

Method calls without parentheses

Groovy lets you omit parentheses when a call has arguments. These are identical:

GROOVY
apply plugin: 'java'
apply(plugin: 'java')

implementation 'com.google.guava:guava:33.4.0-jre'
implementation('com.google.guava:guava:33.4.0-jre')

implementation 'x' is a method call, not a keyword. That is why Groovy's DSL feels so declarative. With no arguments, you need the parentheses: println().

Maps and lists

GROOVY
def dep = [group: 'org.slf4j', name: 'slf4j-api', version: '2.0.16']   // map
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.16' // named args
def names = ['api', 'core', 'web']                                       // list
names.each { println it }

group: 'x', name: 'y' is a map passed as named arguments. it is the implicit parameter of a one-argument closure.

Closures: the heart of the DSL

A closure is a block of code in braces that can be passed around. Build blocks such as dependencies { ... } and repositories { ... } are method calls whose last argument is a closure:

GROOVY
repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.guava:guava:33.4.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.11.4'
}

dependencies { ... } calls project.dependencies(closure). Gradle sets the closure's delegate to a DependencyHandler, so the method implementation inside the braces is looked up on that handler. That is the whole trick behind every Gradle block: a closure whose methods resolve against a configured object.

Stand-in: a closure with a delegate

The Java program below is not Groovy, but it mimics what Gradle does: it creates a handler object, passes it to your block, and calls implementation(...) on it.

Java Stand-in: dependencies { } as a configure block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

In Groovy you write implementation '...' instead of d.implementation("..."), because the closure delegate makes the d. implicit.

A complete Groovy build file

GROOVY
plugins {
    id 'java'
    id 'application'
}

group = 'com.example'
version = '1.0.0'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.guava:guava:33.4.0-jre'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.11.4'
}

application {
    mainClass = 'com.example.App'
}

tasks.named('test') {
    useJUnitPlatform()
}

tasks.register('hello') {
    doLast {
        println "Hello from ${project.name}"
    }
}

Compare it with the Kotlin equivalent in the next lesson. Differences you already see: id 'java' vs id("java"), and single-quoted strings.

Assignment syntax: = versus the old space form

Older scripts write mainClass 'x' or sourceCompatibility 1.8 (a method call). Modern Gradle prefers real assignment: mainClass = 'x'. Both may work in Groovy for a while, but the = form is the future, and it is what the Kotlin DSL requires. When you see a property without = in an old script, it is usually a method call on a setter-like method.

Common Groovy gotchas

  • Single vs double quotes. '${name}' is the literal text, not the value.
  • Closure vs map braces. [a: 1] is a map, { ... } is a closure.
  • Command chains with trailing commas can hide errors; use parentheses when in doubt.
  • Dynamic typing. A typo in a method name only fails when the line runs. Kotlin catches it earlier.
  • Parentheses matter at line ends. A newline ends a statement, so foo( must keep its arguments on the same line or inside the parentheses.

Stand-in: single vs double quotes

Java has no GString, but we can emulate the difference with a tiny interpolation helper so you see why quote choice matters.

Java Stand-in: interpolation vs literal strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is implementation 'com.example:lib:1.0' in a Groovy build script?

    Groovy allows omitting parentheses. It is the same as implementation('com.example:lib:1.0').
  2. 2. Which string interpolates the value of name?

    Only double-quoted strings interpolate. Single-quoted strings are literal.
  3. 3. What does dependencies { ... } really do?

    The closure is configured so that names such as implementation resolve against the handler.
  4. 4. What is [group: 'x', name: 'y'] in Groovy?

    Square brackets with key: value pairs create a map, often used for named arguments.

Summary

  • build.gradle is a Groovy program; blocks are method calls that take closures.
  • Single quotes are literal, double quotes interpolate ${...}.
  • Parentheses are optional when a call has arguments, so implementation 'x' is a method call.
  • Maps use [key: value]; closures use { }; it is the implicit parameter.
  • Prefer = assignment over old space-separated property syntax.
  • Groovy is dynamic, so typos show up at run time.

Next: Kotlin DSL Basics