kotlinjarkotlin-multiplatformuberjar

Create fat jar from kotlin multiplatform project


I recently switched from old 1.2 multiplatform into 1.3. Difference is, there's one one build.gradle file per multiplatform module (I got 5 of them) so a lot less configuration. However I can't seem to be able to configure creating runnable fat jar with all dependencies from jvm platform. I used to use standard "application" plugin in my jvm project and jar task, but that does not work anymore. I found there's "jvmJar" task and I modified it (set Main-class), but created jar doesn't contain dependencies and crashes on ClassNotFoundException. How do I do it?

This is what I have now:

    jvm() {
        jvmJar {
            manifest {
                attributes 'Main-Class': 'eu.xx.Runner'
            }
            from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
        }

    }

Solution

  • I did hit that bump and used this work around.

    1. Restructure your project

    Lets call your project Project.

    create another submodule say subA, which will have the gradle notation Project:subA

    now, subA has your multiplatform code in it (It is the gradle project with apply :kotlin-multiplafrom) in its build.gradle

    2. Add Another submodule

    create another submodule which targets only jvm say subB, which will have the gradle notation Project:subB

    So, subB will have plugins: 'application' and 'org.jetbrains.kotlin.jvm'

    3. Add your module as a gradle dependency (see my build.gradle)

    plugins {
        id 'org.jetbrains.kotlin.jvm' version '1.3.31'
        id "application"
    }
    
    apply plugin: "kotlinx-serialization"
    
    group 'tz.or.self'
    version '0.0.0'
    
    mainClassName = "com.example.MainKt"
    
    sourceCompatibility = 1.8
    
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    
    dependencies {
        implementation project(':subA')
    }
    

    you can proceed and build subB as you would a regular java project or even use the existing plugins, it will work