当前位置: 首页 > 知识库问答 >
问题:

使用 Kotlin DSL 构建 Fatjar 时出错

姬高扬
2023-03-14

我正在尝试使用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")
}

科特林代码

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个答案

贝自怡
2023-03-14

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

这不是http4k特定的,并且是使用影子jar gradle插件的常见问题。但是它可以通过正确配置插件轻松解决,如下所示:

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

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

 类似资料:
  • 问题内容: 我已使用内置的Gradle支持在IntelliJ(2019.2)中设置了OpenJDK 12项目。为了设计使用JavaFX 12的GUI,我已经阅读并阅读了多次安装指南,在IDE中运行程序没有问题,问题是当我尝试构建要运行的发行版的.jar文件时陷入问题。到目前为止,我还没有找到一个可行的解决方案,而我搜索了很多QUITE,几乎为此扯掉了头发。当前,当我尝试使用java -jar“ j

  • 我试着把我的主类移到一个不扩展Application的单独的类中,这就是导致上述错误的原因。如果不移动我的主类,我会得到一个不同的错误。 My Build.Gradle当前看起来如下所示: 如果我让我的main方法扩展应用程序,那么我会得到一个错误,说明找不到我的main类,尽管我可以看到它存在于生成的.jar文件中。 编辑 我试过指南的模块化部分。这样做,我有超过100个错误时,试图建立。它们都

  • 我目前正在按照这里的说明使用bazel从源代码构建tenorflow。 设置配置并尝试构建配置后,出现以下错误: Cuda配置错误:将C:/Program Files/NVIDIA GPU Computing读取到olkit/Cuda/v9时出错。0/include/cudnn。h:爪哇。伊奥。IOException:错误:src/main/native/win-dows/processes jn

  • 我使用gradle创建了一个简单的java项目,从“gradle init——类型java应用程序”开始。 主java文件的内容-“App.java”: 文件“build.gradle”的内容: gradle似乎已成功下载log4j jar文件: 在~/中有一组log4j jar/pom文件。gradle目录。当我尝试生成时,出现以下生成错误: 我对gradle/java是新手。任何帮助都将不胜感

  • 我一直在遵循“Django by example”的教程,该教程介绍了Solr和Haystack,但遇到了一个问题。我已在settings.py中对已安装的_应用程序进行了必要的更改,并添加了以下内容: 然后我就跑 我得到这个错误 这是我的search_index.py文件 当我运行django shell时,我可以很好地导入haystack,但当我运行以下命令时: 进入第二行后,我得到了完全相同

  • mvn-版本 Apache Maven 3.6.1(d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555;2019-04-04T21:00:29 02:00)Maven主页:C:\Apps\Apache-Maven-3.6.1\bin。。Java版本:11.0.13,供应商:Oracle Corporation,运行时:C:\Apps\jdk-11.0.13_windo