Mudasar Cheema
01/06/2025, 12:22 PMbuild.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?
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.")
}
}
}
Mudasar Cheema
01/06/2025, 12:23 PMbuild.gradle.kts
file:
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.")
}
}
}
Vampire
01/06/2025, 12:29 PMCopy
- if *th*ere is any - to additionally copy the hook files, if they are run at all.Mudasar Cheema
01/06/2025, 12:43 PMMudasar Cheema
01/06/2025, 12:46 PMregister<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
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 itVampire
01/06/2025, 3:53 PMhooks
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 🤷♂️Sk Niyaj Ali
01/07/2025, 2:09 PMVampire
01/07/2025, 2:31 PM