kotlinintermediate

Kotlin Gradle DSL — Build Configuration

Configure Gradle builds with Kotlin DSL: dependencies, tasks, plugins, and multi-module setup.

kotlin
// build.gradle.kts — root project

plugins {
    kotlin("jvm") version "1.9.22"
    kotlin("plugin.serialization") version "1.9.22"
    application
    id("com.github.ben-manes.versions") version "0.51.0" // dependency updates
}

group = "com.example"
version = "1.0.0"

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // Kotlin
    implementation(kotlin("stdlib"))
    implementation(kotlin("reflect"))

    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.8.0")

    // Serialization
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")

    // HTTP
    implementation("io.ktor:ktor-client-core:2.3.8")
    implementation("io.ktor:ktor-client-cio:2.3.8")
    implementation("io.ktor:ktor-client-content-negotiation:2.3.8")
    implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.8")

    // Logging
    implementation("io.github.microutils:kotlin-logging-jvm:3.0.5")
    runtimeOnly("ch.qos.logback:logback-classic:1.4.14")

    // Testing
    testImplementation(kotlin("test"))
    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")
    testImplementation("io.mockk:mockk:1.13.9")
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
}

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

tasks {
    test {
        useJUnitPlatform()
        testLogging {
            events("passed", "skipped", "failed")
            showStandardStreams = true
        }
    }

    jar {
        manifest {
            attributes(
                "Main-Class" to "com.example.MainKt",
                "Implementation-Version" to project.version
            )
        }
    }

    // Fat JAR
    register<Jar>("fatJar") {
        archiveClassifier.set("all")
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        from(sourceSets.main.get().output)
        dependsOn(configurations.runtimeClasspath)
        from({
            configurations.runtimeClasspath.get()
                .filter { it.name.endsWith("jar") }
                .map { zipTree(it) }
        })
        manifest {
            attributes("Main-Class" to "com.example.MainKt")
        }
    }

    // Custom task
    register("printVersion") {
        group = "help"
        description = "Prints the project version"
        doLast {
            println("Version: ${project.version}")
        }
    }
}

// ============================================
// settings.gradle.kts (multi-module)
// ============================================
/*
rootProject.name = "my-app"

include(
    ":core",
    ":api",
    ":web"
)

pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            version("kotlin", "1.9.22")
            version("ktor", "2.3.8")
            version("coroutines", "1.8.0")

            library("ktor-core", "io.ktor", "ktor-client-core").versionRef("ktor")
            library("coroutines", "org.jetbrains.kotlinx", "kotlinx-coroutines-core").versionRef("coroutines")
        }
    }
}
*/

Use Cases

  • Kotlin project build configuration
  • Multi-module Gradle setup
  • Custom task and fat JAR creation

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.