2

我正在尝试使用 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
4

1 回答 1

4

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

这不是 http4k 特定的,并且是(相当)使用 shadow-jar gradle 插件的常见问题。但只需正确配置插件即可轻松解决,如下所示:

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

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

于 2019-09-16T07:12:02.193 回答