使用Kotlin DSL构建Fatjar时出错

时间:2019-05-15 15:21:12

标签: gradle kotlin gradle-kotlin-dsl kotlin-dsl http4k

我正在尝试使用ShadowJar构建胖子。我的应用和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")
}

科林代码

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

1 个答案:

答案 0 :(得分:1)

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

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

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

在http4k方面,生成的异常消息已更改为突出显示此问题。