gpt4 book ai didi

gradle - 使用 Kotlin DSL 构建 Fatjar 时出错

转载 作者:行者123 更新时间:2023-12-02 12:41:31 26 4
gpt4 key购买 nike

我正在尝试使用 ShadowJar 构建一个 fatjar。我的应用程序和 gradle 代码如下。我使用 Gradle 5.0 进行构建。当我运行 ./gradlew run 时,代码有效。当我运行“gradle Shadowjar”,并在“build/lib”文件夹中使用“java -jar”运行 fatjar 时,出现以下错误。

我猜依赖项没有加载到 fatjar 中?我还使用 Groovy 构建 Gradle 文件,也得到了同样的错误。

我没有在 fatjar 文件中包含所有依赖项,这是否正确?如果是这种情况,您知道如何修改 Gradle 文件以确保包含此文件吗?

Gradle Kotlin DSL

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm").version("1.3.21")

id("com.github.johnrengelman.shadow") version "5.0.0"

// Apply the application plugin to add support for building a CLI application.
application
}

repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}

dependencies {
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")

// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")

implementation("org.http4k:http4k-core:3.143.1")
implementation("org.http4k:http4k-server-jetty:3.143.1")
implementation("org.http4k:http4k-client-okhttp:3.143.1")
implementation("org.http4k:http4k-client-apache:3.143.1")

}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

application {
// Define the main class for the application.
mainClassName = "com.benito.AppKt"
}

tasks.withType<ShadowJar> {
archiveBaseName.set("${project.name}-all")
}

Kotlin 代码

import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.routing.bind
import org.http4k.routing.path
import org.http4k.routing.routes
import org.http4k.server.Jetty
import org.http4k.server.asServer

fun main(args: Array<String>) {
println("Starting")


val app: HttpHandler = routes(
"/ping" bind Method.GET to { _: Request -> Response(OK).body("pong!") },
"/greet/{name}" bind Method.GET to { req: Request ->
val path: String? = req.path("name")
Response(OK).body("hello ${path ?: "anon!"}")
}
)
app.asServer(Jetty(8080)).start()
}

错误信息

2019-05-15 11:15:13.925:INFO::main: Logging initialized @287ms to org.eclipse.jetty.util.log.StdErrLog
2019-05-15 11:15:14.039:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 1.8.0_211-b12
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.eclipse.jetty.http.MimeTypes$Type.<init>(MimeTypes.java:103)
at org.eclipse.jetty.http.MimeTypes$Type.<clinit>(MimeTypes.java:58)
at org.eclipse.jetty.http.MimeTypes.<clinit>(MimeTypes.java:191)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:836)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:278)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
at org.eclipse.jetty.server.Server.start(Server.java:418)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:382)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.http4k.server.Jetty$toServer$3.start(jetty.kt:33)
at com.benito.AppKt.main(App.kt:38)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.eclipse.jetty.http.PreEncodedHttpField.<clinit>(PreEncodedHttpField.java:70)
... 14 more

最佳答案

这里的问题是,在将所有 JAR 合并到一个 FatJAR 的过程中,http4k-core JAR 中 META-INF 文件夹的内容(其中包含用于将请求与内容类型)在合并过程中以某种方式被省略或覆盖。

这不是 http4k 特有的,并且是使用 Shadow-jar gradle 插件时的一个常见问题。但只需正确配置插件即可轻松解决此问题,如下所示:

shadowJar {
mergeServiceFiles() // <-- this!
}

从 http4k 端,生成的异常消息已更改以突出显示此问题。

关于gradle - 使用 Kotlin DSL 构建 Fatjar 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com