Dropwizard + Gradle fatJar用于带有子项目的项目会引入gradle-api-5.4.1.jar deps

时间:2019-06-07 05:43:41

标签: java gradle dropwizard gradle-kotlin-dsl

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

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

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

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

我有一个run任务和一个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,它包含似乎是我期望的所有类(根目录的依赖关系,Web的依赖关系,它们的传递性依赖关系等)。

但是,当我运行./gradlew run时,由于在类路径上找到了多个日志记录绑定,因此启动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删除登录,但是我真的很想弄清楚为什么gradle-api-5.4.1.jar被捆绑到我的应用程序中。我无法为自己的生活弄清楚,也不知道为什么会把它拉进来。

任何方向将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:1)

因此,在问这个问题和正在做的事情时,我的错误是我没有提及或意识到我偶然在根脚本中使用了kotlin-dsl插件。一旦删除此选项,一切都会按预期进行。

谢谢大家

答案 1 :(得分:0)

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

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

}