Kotlin 1.3+打破SLF4J / log4j

时间:2019-01-12 13:33:13

标签: gradle kotlin log4j slf4j

我正在尝试将项目升级到Kotlin 1.3,但是我遇到了麻烦,因为我们使用HikariCP以及许多其他使用SLF4J / log4j的库,并且由于Kotlin 1.3及更高版本,它们全部由于某种原因而损坏

仅通过更改Kotlin版本就会出现此问题。为什么Kotlin的变化会影响SLF4J / log4j行为?

java.lang.NoSuchMethodError: org.apache.log4j.Level.isGreaterOrEqual(Lorg/apache/log4j/Priority;)Z
    at org.apache.log4j.Category.isDebugEnabled(Category.java:736)
    at org.slf4j.impl.Log4jLoggerAdapter.debug(Log4jLoggerAdapter.java:251)
    at com.zaxxer.hikari.HikariConfig.attemptFromContextLoader(HikariConfig.java:901)
    at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:474)
    at ps.eden.server.game.system.mysql.SQLManager.init(SQLManager.java:86)

这是我们使用的gradle构建配置,唯一的区别是版本是1.2.71而不是1.3.11

buildscript {
    ext.kotlin_version = '1.3.11'

    repositories {
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.2'
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = "ps.eden.server.Eden"

group 'ps.eden'
version '1.4.0'

targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

eclipse {
    classpath {
        containers 'org.jetbrains.kotlin.core.KOTLIN_CONTAINER'
    }
}

compileKotlin {
    kotlinOptions.suppressWarnings = true
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    main.java.srcDirs += 'src/main/java'
}

repositories {
    jcenter()
    flatDir { dirs 'lib' }
    maven { url 'https://jitpack.io' }
    maven { url "https://dl.bintray.com/kotlin/kotlinx/" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-common', version: "$kotlin_version"
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-jvm', version: "$kotlin_version"
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-jvm-host', version: "$kotlin_version"

    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
    compile group: 'joda-time', name: 'joda-time', version: '2.10.1'
    compile(group: 'com.zaxxer', name: 'HikariCP', version: '3.3.0') {
        exclude module: 'slf4j-api'
    }
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.5'
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.18'
    compile(group: 'org.apache.hadoop', name: 'hadoop-common', version: '3.1.1') {
        exclude module: 'slf4j-api'
        exclude module: 'slf4j-log4j'
        exclude module: 'slf4j-log412j'
    }
    compile group: 'org.apache.ant', name: 'ant', version: '1.10.5'
    compile group: 'it.unimi.dsi', name: 'fastutil', version: '8.2.2'
    compile group: 'io.netty', name: 'netty-all', version: '4.1.32.Final'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
    compile group: 'com.github.salomonbrys.kotson', name: 'kotson', version: '2.5.0'
    compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
    compile 'com.github.rockaport:alice:0.9.0'
    compile group: 'at.favre.lib', name: 'bcrypt', version: '0.6.0'
    compile group: 'net.openhft', name: 'zero-allocation-hashing', version: '0.8'
    compile('net.dv8tion:JDA:3.8.1_437') {
        exclude module: 'opus-java'
    }
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
}

compileJava {
    options.encoding = "UTF-8"

    //enable compilation in a separate daemon process
    options.fork = true

    //enable incremental compilation
    options.incremental = true
}

1 个答案:

答案 0 :(得分:4)

您正在使用kotlin脚本库,尤其是kotlin-scripting-jvm-host,它会拉出kotlin编译器jar。您可以尝试使用kotlin-scripting-jvm-host-embeddable代替:它取决于编译器jar的“可嵌入”版本,其中捆绑的3rd Party类被“阴影化”。

编辑:kotlin-scripting-jvm-host-embeddable从Kotlin版本1.3.20 开始(在撰写本文时-仅以预发行形式提供)