[英]Why is Intellij not creating a build.gradle.kts for a new kotlin project?
[英]how to set up a gradle task to create fatJar in Kotlin(1.4) Multiplatform project in build.gradle.kts
我正在尋找一種創建 jar 的方法,它包含我需要擁有一個包含前端和后端的完整工作應用程序所需的一切。 fatJar 是解決方案,但是如何在創建此 fatJar 的 kotlin/多平台項目中創建 gradle 任務? 我找不到開箱即用的解決方案。 我不得不修改和擴展找到的任務以獲得我想要的結果。
所以這是對我有用的任務:
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
import org.gradle.jvm.tasks.Jar
val kotlinVersion = "1.4.0"
val serializationVersion = "1.0.0-RC"
val ktorVersion = "1.4.0"
plugins {
kotlin("multiplatform") version "1.4.0"
application //to run JVM part
kotlin("plugin.serialization") version "1.4.0"
id("com.github.johnrengelman.shadow") version "6.1.0"
}
group = "com.bantolomeus"
version = "0.1"
repositories {
maven { setUrl("https://dl.bintray.com/kotlin/kotlin-eap") }
mavenCentral()
jcenter()
maven("https://kotlin.bintray.com/kotlin-js-wrappers/") // react, styled, ...
}
kotlin {
jvm {
withJava()
}
js {
browser {
binaries.executable()
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
// implementation("com.github.jengelman.gradle.plugins:shadow:6.1.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-serialization:$ktorVersion")
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("ch.qos.logback:logback-classic:1.2.3")
implementation("io.ktor:ktor-websockets:$ktorVersion")
implementation("org.litote.kmongo:kmongo-coroutine-serialization:4.1.1")
}
}
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion") //include http&websockets
//ktor client js json
implementation("io.ktor:ktor-client-json-js:$ktorVersion")
implementation("io.ktor:ktor-client-serialization-js:$ktorVersion")
implementation("org.jetbrains:kotlin-react:17.0.1-pre.148-kotlin-1.4.21")
implementation("org.jetbrains:kotlin-react-dom:17.0.1-pre.148-kotlin-1.4.21")
implementation(npm("react", "17.0.1"))
implementation(npm("react-dom", "17.0.1"))
}
}
}
}
application {
mainClassName = "ServerKt"
}
// include JS artifacts in any JAR we generate
tasks.getByName<Jar>("jvmJar") {
val taskName = if (project.hasProperty("isProduction")) {
"jsBrowserProductionWebpack"
} else {
"jsBrowserDevelopmentWebpack"
}
val webpackTask = tasks.getByName<KotlinWebpack>(taskName)
dependsOn(webpackTask) // make sure JS gets compiled first
from(File(webpackTask.destinationDirectory, webpackTask.outputFileName)) // bring output file along into the JAR
}
tasks {
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
distributions {
main {
contents {
from("$buildDir/libs") {
rename("${rootProject.name}-jvm", rootProject.name)
into("lib")
}
}
}
}
// Alias "installDist" as "stage" (for cloud providers)
tasks.create("stage") {
dependsOn(tasks.getByName("installDist"))
}
tasks.getByName<JavaExec>("run") {
classpath(tasks.getByName<Jar>("jvmJar")) // so that the JS artifacts generated by `jvmJar` can be found and served
}
// tasks to create an executable jar with all components of the app
tasks.getByName<ShadowJar>("shadowJar") {
val webpackTask = tasks.getByName<KotlinWebpack>("jsBrowserProductionWebpack")
dependsOn(webpackTask) // make sure JS gets compiled first
from(File(webpackTask.destinationDirectory, webpackTask.outputFileName)) // bring output file along into the JAR
archiveBaseName.set("shadow")
mergeServiceFiles()
manifest {
attributes["Main-Class"] = "ServerKt"
}
}
// task to create executable jar during build task
//tasks {
// build {
// dependsOn(shadowJar)
// }
//}
我注釋掉了最后一部分,在構建過程中並不總是創建 shadowJar (fatJar),因為這只是需要時間,在我的情況下不需要。 相關任務是最后一項未被淘汰的任務。 我還發布了我的整個 build.gradle.kts 文件,因為您需要一些導入、插件和依賴項(與 jengelman 的 shadowJar 相關的所有內容)。
我希望其他有同樣問題的人覺得這很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.