Gradle
01 / 02

Build Scripts, Dependencies & Tasks

Build Scripts, Dependencies & Tasks

Project Structure

// settings.gradle.kts — defines project topology, runs BEFORE any build.gradle.kts
rootProject.name = "my-app"
include(":app", ":core", ":network")

// app/build.gradle.kts
plugins {
    id("org.jetbrains.kotlin.jvm") version "1.9.22"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":core"))            // dependency on a sibling module
    implementation("com.google.guava:guava:33.0.0-jre")
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

application {
    mainClass.set("com.example.MainKt")
}

Dependency Configurations

dependencies {
    // Available at compile + runtime, NOT exposed transitively to consumers
    implementation("com.squareup.okhttp3:okhttp:4.12.0")

    // Part of this module's public API — exposed transitively to consumers
    api("com.google.code.gson:gson:2.10.1")

    // Compile-time only — e.g. annotation processors not needed at runtime
    compileOnly("org.projectlombok:lombok:1.18.30")

    // Runtime-only — e.g. a JDBC driver never referenced directly in code
    runtimeOnly("org.postgresql:postgresql:42.7.1")

    testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

// ./gradlew dependencies          — full resolved dependency tree
// ./gradlew dependencyInsight --dependency guava   — trace WHY a version won

Custom Tasks

// Lazy registration — configured only if actually needed for the requested build
tasks.register("generateChangelog") {
    doLast {
        File("CHANGELOG.md").appendText("\n- ${project.version}: build ${System.currentTimeMillis()}")
    }
}

// Wire it into the existing build lifecycle
tasks.named("build") {
    dependsOn("generateChangelog")
}

// ./gradlew tasks              — list all available tasks
// ./gradlew build              — compile, test, assemble
// ./gradlew assemble           — package outputs, skip tests
// ./gradlew clean              — delete build/ output
// ./gradlew test --tests "com.example.MyTest"

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free