Dropwizard + Gradle fatJar for project with subprojects pulls in gradle-api-5.4.1.jar deps

Dropwizard + Gradle fatJar for project with subprojects pulls in gradle-api-5.4.1.jar deps

我有一个 Gradle 项目,作为一个简单的例子看起来与此类似:

root
|
|build.gradle.kts
|settings.gradle.kts
|config.yaml
|web/
    |build.gradle.kts
    |src/
        |main/
             |java....(etc)

Web 项目是一个准系统的 Dropwizard 应用程序,除了声明一个配置和一个使用它的应用程序之外什么都不做,以便服务器可以启动。

在我的 root 路径中,我在 settings.gradle.kts 文件中定义了我的子项目(在本例中为 :web)。在我的 root build.gradle.kts 中,我声明了一个 implementation(project(":web")) 依赖项。

我有一个 run 任务和一个 run/build 这个应用程序的 fatJar 任务。


val run by tasks.getting(JavaExec::class) {
    args("server", "config.yaml")
}

val fatJar = task("fatJar", type = Jar::class) {
    manifest {
        attributes["Implementation-Title"] = "Some API"
        attributes["Main-Class"] = "my.package.web.HelloWorldApplication"
    }
    from(configurations.runtimeClasspath.get().map({ if (it.isDirectory) it else zipTree(it) }))
    with(tasks.jar.get() as CopySpec)
}

tasks {
    "build" {
        dependsOn(fatJar)
    }
}

我可以构建 fatJar,它包含似乎是我期望的所有 classes(根依赖项、web 依赖项、它们的传递依赖项等)。

但是,当我 运行 ./gradlew run 时,由于在 class 路径上发现多个日志记录绑定,我在启动 Dropwizard 应用程序时遇到错误。错误如下:

./gradlew run
Starting a Gradle Daemon, 1 busy and 1 incompatible and 3 stopped Daemons could not be reused, use --status for details

> Task :run
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/me/.gradle/caches/5.4.1/generated-gradle-jars/gradle-api-5.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/me/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext]
Exception in thread "main" java.lang.IllegalStateException: Unable to acquire the logger context
        at io.dropwizard.logging.LoggingUtil.getLoggerContext(LoggingUtil.java:46)
        at io.dropwizard.logging.BootstrapLogging.bootstrap(BootstrapLogging.java:52)
        at io.dropwizard.logging.BootstrapLogging.bootstrap(BootstrapLogging.java:41)
        at io.dropwizard.Application.bootstrapLogging(Application.java:38)
        at io.dropwizard.Application.<init>(Application.java:26)
        at my.package.web.HelloWorldApplication.<init>(HelloWorldApplication.java:15)
        at my.package.web.HelloWorldApplication.main(HelloWorldApplication.java:17)

> Task :run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 25s
13 actionable tasks: 1 executed, 12 up-to-date

我知道我可以从 Dropwizard 中删除 logback,但我真的很想弄清楚为什么 gradle-api-5.4.1.jar 被捆绑到我的应用程序中。我这辈子都想不通,也不明白为什么它会被拉进来。

如有任何指示,我们将不胜感激。

谢谢

您必须在 build.gradle 文件的依赖项部分添加以下内容。

dependencies {
  compile('io.dropwizard:dropwizard-core:1.2.2') {
     exclude group: 'org.slf4j', module: 'slf4j-api' 
  }

}

所以我问这个问题和我正在做的事情的错误是我没有提到或意识到我在我的根脚本中意外地应用了 kotlin-dsl 插件。一旦我删除它,一切都按我预期的那样工作。