TExcelTAD
Home › Java Builds: Maven & Gradle › Gradle: First Build

Gradle: First Build

Maven describes a project with a fixed model and a fixed lifecycle. Gradle starts from a different idea: a build is a graph of tasks, each with declared inputs and outputs, and build scripts are code that configures that graph. Plugins (java, application, maven-publish) add tasks and conventions, so for an ordinary Java project the script stays as short as a POM. The payoff of the task model is speed: Gradle knows exactly what each task read and wrote, so it can skip, cache and parallelise work.

Generating a project

gradle init creates a ready-made build. The options make it non-interactive:

bash · try it yourself
mkdir demo && cd demo
gradle init --type java-application --dsl kotlin --test-framework junit-jupiter --package demo \
  --project-name demo --no-split-project --java-version 21 --no-incubating -q
find . -type f -not -path './.gradle/*' | sort
cat settings.gradle.kts | grep -v -E '^ ?\*|^/\*|^
  


cat app/build.gradle.kts | grep -v -E '^ *//|^ ?\*|^/\*|^
  


cat gradle.properties | grep -v '^#'
./.gitattributes
./.gitignore
./app/build.gradle.kts
./app/src/main/java/demo/App.java
./app/src/test/java/demo/AppTest.java
./gradle.properties
./gradle/libs.versions.toml
./gradle/wrapper/gradle-wrapper.jar
./gradle/wrapper/gradle-wrapper.properties
./gradlew
./gradlew.bat
./settings.gradle.kts
plugins {
    // Apply the foojay-resolver plugin to allow automatic download of JDKs
    id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
}
rootProject.name = "demo"
include("app")
plugins {
    application
}
repositories {
    mavenCentral()
}
dependencies {
    testImplementation(libs.junit.jupiter)
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
    implementation(libs.guava)
}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}
application {
    mainClass = "demo.App"
}
tasks.named<Test>("test") {
    useJUnitPlatform()
}

org.gradle.configuration-cache=true

The pieces of every Gradle build:

File Role
settings.gradle.kts the settings script: names the build, lists its projects (include("app")), configures plugin and dependency repositories for all of them
app/build.gradle.kts a build script for one project: plugins, dependencies, configuration
gradle/libs.versions.toml the version catalog: dependency coordinates and versions in one place, used as libs.guava
gradle.properties build-wide settings: here, the configuration cache is turned on
gradlew, gradlew.bat, gradle/wrapper/* the Gradle Wrapper, the Gradle twin of mvnw

The template also applies the foojay-resolver-convention settings plugin, which lets Gradle download a JDK when the requested toolchain isn't installed. Here Java 21 is installed, so it never has to.

Building and running

bash · try it yourself
mkdir demo && cd demo
gradle init --type java-application --dsl kotlin --test-framework junit-jupiter --package demo \
  --project-name demo --no-split-project --java-version 21 --no-incubating -q
./gradlew build
./gradlew run
./gradlew build
Calculating task graph as no cached configuration is available for tasks: build
> Task :app:compileJava
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses
> Task :app:test
> Task :app:check
> Task :app:build

BUILD SUCCESSFUL in 4s
7 actionable tasks: 7 executed
Configuration cache entry stored.
Calculating task graph as no cached configuration is available for tasks: run
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 758ms
2 actionable tasks: 1 executed, 1 up-to-date
Configuration cache entry stored.
Reusing configuration cache.
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:jar UP-TO-DATE
> Task :app:startScripts UP-TO-DATE
> Task :app:distTar UP-TO-DATE
> Task :app:distZip UP-TO-DATE
> Task :app:assemble UP-TO-DATE
> Task :app:compileTestJava UP-TO-DATE
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
> Task :app:test UP-TO-DATE
> Task :app:check UP-TO-DATE
> Task :app:build UP-TO-DATE

BUILD SUCCESSFUL in 633ms
7 actionable tasks: 7 up-to-date
Configuration cache entry reused.

Gradle's plain console prints one > Task :project:name line per task it touches, followed by an outcome when the task didn't simply run:

Outcome Meaning
(none) the task executed
UP-TO-DATE inputs and outputs unchanged since the last run, so it was skipped
NO-SOURCE nothing to process (there is no src/main/resources)
FROM-CACHE outputs taken from the build cache
SKIPPED excluded (-x test) or disabled

The third command did no work at all: every task was UP-TO-DATE, and the line 7 actionable tasks: 7 up-to-date sums it up. Maven has no equivalent; mvn package always runs the tests again.

Two more lines matter. Calculating task graph as no cached configuration is available and Configuration cache entry stored. come from the configuration cache: the third command printed Reusing configuration cache. and skipped running the build scripts entirely. (run stored an entry of its own: the cache is keyed by the requested tasks.) And the first command of the session started the Gradle Daemon, a background JVM that keeps the build's classes loaded and warm; later commands connect to it, which is why they're faster.

Where things go

Path Contents
app/build/classes/java/main compiled classes (Maven: target/classes)
app/build/libs/app.jar the JAR, named after the project (no version set here)
app/build/reports/tests/test/index.html the HTML test report
.gradle/ (in the project) Gradle's per-project state: file hashes, task history, configuration cache
~/.gradle/caches/ downloaded dependencies and compiled build scripts, shared by all builds

Neither build/ nor .gradle/ is committed; the generated .gitignore already says so.

Test yourself

Which file lists the projects (modules) of a Gradle build?

  1. build.gradle.kts
  2. settings.gradle.kts
  3. gradle.properties
  4. libs.versions.toml

What does UP-TO-DATE next to a task mean?

  1. The task failed
  2. Its inputs and outputs are unchanged since its last execution, so Gradle skipped it
  3. It was downloaded
  4. It has no source files

What is the Gradle Daemon?

  1. A CI server
  2. A long-lived background JVM that runs builds and stays warm between them
  3. The wrapper script
  4. A dependency cache