我遵循Ktor文档,但当我运行时
/gradlew:fatjar:shadowJar
在IntelliJ的终端上,就像它说我得到以下错误一样:
FAILURE:生成失败,出现异常。
- 哪里出了问题:在根项目"com.menucabinet.ktor menu cabinet"中找不到项目"fatjar">
我一直在四处寻找,但关于这个问题的文档似乎有限。。。
这是我的成绩:
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
val kmongo_version: String by project
val koin_version: String by project
plugins {
application
kotlin("jvm") version "1.6.10"
id("org.jetbrains.kotlin.plugin.serialization") version "1.6.10"
id("com.github.johnrengelman.shadow") version "7.0.0"
}
group = "menu_cabinet"
version = "0.0.1"
application {
mainClass.set("io.ktor.server.netty.EngineMain")
project.setProperty("mainClassName", mainClass.get())
}
repositories {
mavenCentral()
}
tasks {
shadowJar {
manifest {
attributes(Pair("Main-Class", "io.ktor.server.netty.EngineMain"))
}
}
}
dependencies {
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-websockets:$ktor_version")
implementation("io.ktor:ktor-serialization:$ktor_version")
implementation("io.ktor:ktor-server-sessions:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
// KMongo
implementation("org.litote.kmongo:kmongo:$kmongo_version")
implementation("org.litote.kmongo:kmongo-coroutine:$kmongo_version")
// Koin core features
implementation("io.insert-koin:koin-core:$koin_version")
implementation("io.insert-koin:koin-ktor:$koin_version")
implementation("io.insert-koin:koin-logger-slf4j:$koin_version")
}
我是Ktor和后端的新手,我能做些什么来解决这个问题?
更新:
我发现如果我
- 将以下插件添加到gradle-id("com.github.johnrengelmanshadow"(版本中;7.0.0〃
- 打开IntelliJ右上角的渐变菜单,找到并选择Tasks/shadow/shadowJar
- 这将构建jar文件,并将其放置在项目文件中的build/libs下
- 然后,我可以使用java从计算机的终端运行jar-jar生成的JarFile的名称
因此,由于这是有效的,我相信我的问题与Kotlin DSL中的Gradle任务有关,尽管我可能错了。
shadowJar
任务之前添加一个子项目名称:
./gradlew :fatjar:shadowJar
对于独立项目,此命令如下所示:
./gradlew shadowJar
对于独立项目,从终端运行它应该在lib文件夹中构建jar文件,这里是一个工作示例https://github.com/hariinfo/ktor-learn/tree/main/ktor-exposed-demo