Hi! In my `build.gradle.kts` i have a task for cop...
# kotlin-dsl
m
Hi! In my
build.gradle.kts
i have a task for copy file from the project into
.git/hooks
folder, but it wont work? Can anyone explain me what i am doing wrong here?
Copy code
tasks {

    withType<KotlinCompile>().configureEach {
        dependsOn("ktlintFormat")
    }

    withType<ShadowJar>().configureEach {
        enabled = true
        archiveFileName.set("app.jar")
        manifest {
            attributes["Main-Class"] = "no.nav.sokos.prosjektnavn.ApplicationKt"
        }
        finalizedBy(koverHtmlReport)
    }

    ("jar") {
        enabled = false
    }

    withType<Test>().configureEach {
        useJUnitPlatform()

        testLogging {
            showExceptions = true
            showStackTraces = true
            exceptionFormat = FULL
            events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
        }

        reports.forEach { report -> report.required.value(false) }
    }

    withType<Wrapper> {
        gradleVersion = "8.11"
    }

    withType<Copy> {
        val preCommitHook = file("./.git/hooks/pre-commit")
        if (!preCommitHook.exists()) {
            println("⚈ ⚈ ⚈ Running Add Pre Commit Git Hook Script on Build ⚈ ⚈ ⚈")
            from(file("./.scripts/pre-commit"))
            into(file("./.git/hooks"))
            println("✅ Added Pre Commit Git Hook Script.")
        }
    }
}
My whole
build.gradle.kts
file:
Copy code
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "2.1.0"
    kotlin("plugin.serialization") version "2.1.0"
    id("com.github.johnrengelman.shadow") version "8.1.1"
    id("org.jlleitschuh.gradle.ktlint") version "12.1.2"
    id("org.jetbrains.kotlinx.kover") version "0.9.0"
}

group = "no.nav.sokos"

repositories {
    mavenCentral()
    maven { url = uri("<https://maven.pkg.jetbrains.space/public/p/ktor/eap>") }
}

val ktorVersion = "3.0.3"
val logbackVersion = "1.5.15"
val logstashVersion = "8.0"
val micrometerVersion = "1.14.2"
val kotlinLoggingVersion = "3.0.5"
val janionVersion = "3.1.12"
val natpryceVersion = "1.6.10.0"
val kotestVersion = "5.9.1"
val kotlinxSerializationVersion = "1.7.3"
val mockOAuth2ServerVersion = "2.1.10"
val mockkVersion = "1.13.14"

dependencies {

    // Ktor server
    implementation("io.ktor:ktor-server-call-logging-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-call-id-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-netty-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-swagger:$ktorVersion")

    // Ktor client
    implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
    implementation("io.ktor:ktor-client-apache-jvm:$ktorVersion")

    // Security
    implementation("io.ktor:ktor-server-auth-jvm:$ktorVersion")
    implementation("io.ktor:ktor-server-auth-jwt-jvm:$ktorVersion")

    // Serialization
    implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:$kotlinxSerializationVersion")

    // Monitorering
    implementation("io.ktor:ktor-server-metrics-micrometer-jvm:$ktorVersion")
    implementation("io.micrometer:micrometer-registry-prometheus:$micrometerVersion")

    // Logging
    implementation("io.github.microutils:kotlin-logging-jvm:$kotlinLoggingVersion")
    runtimeOnly("org.codehaus.janino:janino:$janionVersion")
    runtimeOnly("ch.qos.logback:logback-classic:$logbackVersion")
    runtimeOnly("net.logstash.logback:logstash-logback-encoder:$logstashVersion")

    // Config
    implementation("com.natpryce:konfig:$natpryceVersion")

    // Test
    testImplementation("io.ktor:ktor-server-test-host-jvm:$ktorVersion")
    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
    testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
    testImplementation("io.mockk:mockk:$mockkVersion")
    testImplementation("no.nav.security:mock-oauth2-server:$mockOAuth2ServerVersion")
}

// Vulnerability fix because of id("org.jlleitschuh.gradle.ktlint") version "12.1.2"
configurations.ktlint {
    resolutionStrategy.force("ch.qos.logback:logback-classic:$logbackVersion")
}

sourceSets {
    main {
        java {
            srcDirs("${layout.buildDirectory.get()}/generated/src/main/kotlin")
        }
    }
}

kotlin {
    jvmToolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

tasks {

    withType<KotlinCompile>().configureEach {
        dependsOn("ktlintFormat")
    }

    withType<ShadowJar>().configureEach {
        enabled = true
        archiveFileName.set("app.jar")
        manifest {
            attributes["Main-Class"] = "no.nav.sokos.prosjektnavn.ApplicationKt"
        }
        finalizedBy(koverHtmlReport)
    }

    ("jar") {
        enabled = false
    }

    withType<Test>().configureEach {
        useJUnitPlatform()

        testLogging {
            showExceptions = true
            showStackTraces = true
            exceptionFormat = FULL
            events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
        }

        reports.forEach { report -> report.required.value(false) }
    }

    withType<Wrapper> {
        gradleVersion = "8.11"
    }

    withType<Copy> {
        val preCommitHook = file("./.git/hooks/pre-commit")
        if (!preCommitHook.exists()) {
            println("⚈ ⚈ ⚈ Running Add Pre Commit Git Hook Script on Build ⚈ ⚈ ⚈")
            from(file("./.scripts/pre-commit"))
            into(file("./.git/hooks"))
            println("✅ Added Pre Commit Git Hook Script.")
        }
    }
}
v
"but it don't work" is to unspecific. But besides that, you do not have such a task. You configure all tasks of type
Copy
-
if *th*ere is any - to additionally copy the hook files, if they are run at all.
m
I'm sorry, but can you please explain me what you mean? I did not catch what you are trying to say..
i added
Copy code
register<Copy>("gitHooks") {
        val preCommitHook = file("./.git/hooks/pre-commit")
        if (!preCommitHook.exists()) {
            println("⚈ ⚈ ⚈ Running Add Pre Commit Git Hook Script on Build ⚈ ⚈ ⚈")
            from(file("./.scripts/pre-commit"))
            into(file("./.git/hooks"))
            println("✅ Added Pre Commit Git Hook Script.")
        }
    }
and added
Copy code
withType<ShadowJar>().configureEach {
        enabled = true
        archiveFileName.set("app.jar")
        manifest {
            attributes["Main-Class"] = "no.nav.sokos.prosjektnavn.ApplicationKt"
        }
        finalizedBy(koverHtmlReport)
        dependsOn("gitHooks")
    }
then the file is copied over. Correct me if this is the wrong way to do it
v
Well, "wrong way" is a quite stretchable term. At least it is questionable. 😄 For example, you make the whole
hooks
directory an output of the task. It would be cleaner to not have the type
Copy
but have without type and then declare the input file and output file explicitly and using
copy { ... }
within a
doLast { ... }
action. Besides that it is questionable that the shadow jar task depends on that task as it does not really depend on it. Besides that the task is only executed if that specific task is executed. You might instead maybe want to make all tasks depend on that task using
tasks.configureEach { ... }
? Still imho not really the right way to do it, imho the build tool should never depend on the build running from inside a specific VCS. For example if in 5 years you switch to a different VCS and then check out this old commit and try to run the build, it will fail and so on. But 🤷‍♂️
v
Well, that too has most of the problems I just mentioned 🙂