kotlin multiplatform shadow fat jar and runShadow task?

490 Views Asked by At

I have a fairly standard kotlin multiplatform (mpp) build.gradle.kts that uses application plugin and com.github.johnrengelman.shadow

I succeeded to create an java -jar executable fat jar:

new solution:

EDIT: I extended a line to @Pylyp Dukhov solution:

    classpath += files("$buildDir/processedResources/jvm/main")
tasks {
    named<JavaExec>("run") {
        standardInput = System.`in`
        classpath += objects.fileCollection().from(
            named("compileKotlinJvm"),
            configurations.named("jvmRuntimeClasspath")
        )
        classpath += files("$buildDir/processedResources/jvm/main")
    }
    shadowJar {
        manifest { attributes["Main-Class"] = theMainClass }
        archiveClassifier.set("fat")
        val jvmJar = named<org.gradle.jvm.tasks.Jar>("jvmJar").get()
        from(jvmJar.archiveFile)
        configurations.add(project.configurations.named("jvmRuntimeClasspath").get())
    }
}

old solution:

application {
    mainClass.set(theMainClass)
}

tasks {
    val shadowCreate by creating(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar::class) {
        manifest { attributes["Main-Class"] = theMainClass }
        archiveClassifier.set("fat")
        mergeServiceFiles()
        from(kotlin.jvm().compilations.getByName("main").output)
        configurations = mutableListOf(kotlin.jvm().compilations.getByName("main").compileDependencyFiles as Configuration)
    }
    val build by existing {
        dependsOn(shadowCreate)
    }
}

but now I struggle on how to get the application gradle run task respectively the shadow gradle runShadow task to execute correctly.

Anybody Any Ideas (kotlindsl) ?

1

There are 1 best solutions below

1
On BEST ANSWER

I'm using the following:

tasks.named<JavaExec>("run") {
    classpath += objects.fileCollection().from(
        tasks.named("compileKotlinJvm"),
        configurations.named("jvmRuntimeClasspath")
    )
}