Create fat jar from kotlin multiplatform project

放肆的年华 提交于 2020-06-15 19:46:49

问题


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) } }
        }

    }

回答1:


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




回答2:


Got it working with the multiplatform plugin in kotlin 1.3.61:

The following works for a main file in src/jvmMain/kotlin/com/example/Hello.kt

Hello.kt must also specify it's package as package com.example

I configured my jvm target in this way:

kotlin {
    targets {
        jvm()

        configure([jvm])  {
            withJava()
            jvmJar {
                manifest {
                    attributes 'Main-Class': 'com.example.HelloKt'
                }
                from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
            }
        }
    }
}



回答3:


Got it to work with a slightly modified version of what luca992 did:

kotlin {
jvm() {
    withJava()
    jvmJar {
        manifest {
            attributes 'Main-Class': 'sample.MainKt'
        }
        from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    }
}
...
}


来源:https://stackoverflow.com/questions/57168853/create-fat-jar-from-kotlin-multiplatform-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!