从Kotlin跨平台项目创建胖子 [英] Create fat jar from kotlin multiplatform project
问题描述
我最近从旧的1.2多平台切换到了1.3.区别在于,每个多平台模块只有一个build.gradle文件(我有5个),因此配置少得多. 但是我似乎无法配置从jvm平台创建的具有所有依赖项的可运行胖罐. 我曾经在jvm项目和jar任务中使用标准的应用程序"插件,但这不再起作用.我发现有一个"jvmJar"任务,并对其进行了修改(设置Main-class),但是创建的jar不包含依赖项,并且在ClassNotFoundException上崩溃.我该怎么办?
这就是我现在拥有的:
jvm() {
jvmJar {
manifest {
attributes 'Main-Class': 'eu.xx.Runner'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
我确实碰到了这个障碍,并使用了这项工作.
1.重组您的项目
让我们将您的项目命名为Project
.
创建另一个子模块,例如subA
,该子模块将具有gradle表示法Project:subA
现在,subA
的build.gradle中包含您的多平台代码(这是gradle项目,带有apply:kotlin-multiplafrom)
2.添加另一个子模块
创建另一个仅针对jvm说subB
的子模块,该子模块将具有gradle表示法Project:subB
因此,subB
将具有插件:'application'
和'org.jetbrains.kotlin.jvm'
3.将模块添加为gradle依赖项(请参阅我的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')
}
您可以像常规的Java项目一样继续构建subB,甚至可以使用现有的插件,
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) } }
}
}
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
这篇关于从Kotlin跨平台项目创建胖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!