Trying to create a runnable jar for a kotlin multiplatform project which includes a ktor server component, building with Kotlin Gradle DSL.
I have seen several questions including Create fat jar from kotlin multiplatform project which asks and answers how to create the gradle build file in Groovy, but how do you do it in kotlin dsl?
The groovy code that is reported to work is:
kotlin {
jvm() {
withJava()
jvmJar {
manifest {
attributes 'Main-Class': 'sample.MainKt'
}
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
...
}
How would this translate to Kotlin DSL? I have tried many variations, some of which compile and run, but don't create the desired output... a runnable jar.
@andylamax answer is pretty close but leads to the error that @cfnz was seeing
To fix that you need to add doFirst
as in this example:
val jvm = jvm() {
withJava()
val jvmJar by tasks.getting(org.gradle.jvm.tasks.Jar::class) {
doFirst {
manifest {
attributes["Main-Class"] = project.ext["mainClass"]
}
from(configurations.getByName("runtimeClasspath").map { if (it.isDirectory) it else zipTree(it) })
}
}
}
It is working as expected in here with gradle jvmJar && java -jar build/libs/laguna-jvm.jar