【问题标题】:Spring Boot 2 & Kotlin 1.2.1: outdated wersion of runtimeSpring Boot 2 和 Kotlin 1.2.1:运行时的过时版本
【发布时间】:2018-06-06 13:21:59
【问题描述】:

问题

我正在尝试使用最新的 spring boot (2.0.0.M7) 运行最新的 kotlin 版本 (1.2.10)。 在本地环境中,一切都很好,但是 -boottime intellij 哭了:

我见过Outdated Kotlin Runtime,但在我看来它没有连接所以......这是可疑的,因为我的以下build.gradle,其中只有一个版本的kotlin(只有一个)[前端也是gradle build but node.js one - angular, no kotlin deps]:

buildscript {
  ext {
    kotlinVersion = '1.2.10'
    springBootVersion = '2.0.0.M7'
  }
  repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "http://repo.spring.io/libs-milestone-local" }
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
    classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    classpath 'org.springframework:springloaded:1.2.6.RELEASE'
  }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'

group = 'com.witkups'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8


idea {
  module {
    inheritOutputDirs = false
    outputDir = file("$buildDir/classes/main/")
  }
}

compileKotlin {
  kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
  kotlinOptions.jvmTarget = "1.8"
}

repositories {
  mavenCentral()
  maven { url "https://repo.spring.io/snapshot" }
  maven { url "https://repo.spring.io/milestone" }
  maven { url "http://repo.spring.io/libs-milestone-local" }
}


dependencies {
  compile project(":frontend")

  compile('org.springframework.boot:spring-boot-starter-actuator')
  compile('org.springframework.boot:spring-boot-starter-data-jpa')
  compile('org.springframework.boot:spring-boot-starter-security')
  compile('org.springframework.boot:spring-boot-starter-webflux')
  compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
  runtime('org.springframework.boot:spring-boot-devtools')
  runtime('com.microsoft.sqlserver:mssql-jdbc')
  compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
  compile('com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2')
  compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
  compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
  compile("io.jsonwebtoken:jjwt:0.7.0")

  testCompile('com.h2database:h2')
  testCompile('org.springframework.security:spring-security-test')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile('io.projectreactor:reactor-test')
}

这让我很担心,因为构建中的警告(并且我猜测试失败了):

w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    /home/travis/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.2.10/19bc012f8c4cd6b705bd6512263777cc19bcf259/kotlin-reflect-1.2.10.jar (version 1.2)
    /home/travis/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre8/1.2.10/71c98c16b4bed3f754c6c398f9da8300ecb0a669/kotlin-stdlib-jre8-1.2.10.jar (version 1.2)
    /home/travis/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jre7/1.1.61/59dfce93b1995717338435dd974884007d8e8474/kotlin-stdlib-jre7-1.1.61.jar (version 1.1)
    /home/travis/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.1.61/fa7813a26c548c9c412dd2d42fb466cfcd8dcf3c/kotlin-stdlib-1.1.61.jar (version 1.1)
w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath or use '-Xskip-runtime-version-check' to suppress this warning
:spring-boot-server:compileTestJava NO-SOURCE
:spring-boot-server:processTestResources
:spring-boot-server:testClasses
:spring-boot-server:testPicked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
com.witkups.carsharing.CarSharingApplicationTests > contextLoads FAILED
    java.lang.IllegalStateException
        Caused by: java.lang.IllegalStateException
            Caused by: java.lang.NoClassDefFoundError
                Caused by: java.lang.ClassNotFoundException
1 test completed, 1 failed
:spring-boot-server:test FAILED

测试:

package com.witkups.carsharing

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner

@RunWith(SpringRunner::class)
@SpringBootTest
class CarSharingApplicationTests {

    @Test
    fun contextLoads() {
    }

}

任何帮助将不胜感激:) PS:JDK 8


解决方案

我在build.gradle 脚​​本中添加了以下几行:

compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib-jre7:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")

并且像魅力一样工作。 更换后测试停止失败

@RunWith(SpringRunner::class)

@ExtendWith(SpringExtension::class)

【问题讨论】:

  • 您的 IntelliJ 项目是如何创建的?您是否按照建议进行了操作,并看到了 Kotlin 的dependencyInsight?你看过模块和库定义吗?我怀疑这是因为您使用 Gradle 的 Idea 插件来生成项目,并且它在库路径中添加了旧版本的 Kotlin。我建议将 Gradle 项目直接导入 IntelliJ,不要再使用 Gradle 的 idea 插件。

标签: spring-boot kotlin


【解决方案1】:

您的一个依赖项本身依赖于kotlin-stdlib-jre7。据 Gradle 所知,kotlin-stdlib-jre7kotlin-stdlib-jre8 完全不相关,因此它将两者都放在类路径中,这会导致冲突。

尝试./gradlew dependencyInsight kotlin-stdlib-jre7 以查看依赖项的来源和exclude it。例如。如果是jackson-module-kotlin,写

compile('com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2') {
    exclude group: "org.jetbrains.kotlin"
}

另见https://docs.gradle.org/current/userguide/dependency_management.html#sub:version_conflicts

【讨论】:

  • 我只是将 deps 添加到这些库(1.2.1)的实际版本中,因为即使 kotlin-stdlib-jre8 依赖于 jre7...但测试仍然失败。不过谢谢你:)
  • @azbesciak。这解决了你的问题吗?如果不是,那么它不应该是公认的答案。如果确实如此,请更新一些内容,以明确这如何解决您的问题以帮助他人。
【解决方案2】:

根据What's New in Kotlin 1.2,Java 7 和 8 有新的依赖项,例如。 G。 kotlin-stdlib-jdk8 而不是旧的kotlin-stdlib-jre8。使用此功能可自动修复 kotlin-stdlib-jdk7 的临时导入,但不幸的是不适用于 kotlin-stdlib。还是1.1.61

./gradlew dependencyInsight --dependency kotlin-stdlib

> Task :dependencyInsight 
org.jetbrains.kotlin:kotlin-stdlib:1.1.61 (selected by rule)

org.jetbrains.kotlin:kotlin-stdlib:1.2.10 -> 1.1.61
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.10
|         \--- compileClasspath
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.10 (*)

org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.10
     \--- compileClasspath

org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.10 (selected by rule)

org.jetbrains.kotlin:kotlin-stdlib-jdk8: -> 1.2.10
\--- compileClasspath

(*) - dependencies omitted (listed previously)

您还可以使用显式版本声明 kotlin-stdlib 以修复警告。

compile("org.jetbrains.kotlin:kotlin-stdlib:1.2.10")
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

【讨论】:

  • @Azbesciak 请注意细微差别(jdk 与 jre)。你没有提到这一点,我想,这可以帮助别人。
  • 总是最好说清楚 :) 你是对的,我没有。
  • 我做了一些进一步的研究。旧的依赖版本来自插件'io.spring.dependency-management'。如果我暂时删除插件(这不是一个完整的构建配置),我会得到“org.jetbrains.kotlin:kotlin-stdlib”的正确版本,而无需明确定义。
猜你喜欢
  • 2020-12-21
  • 1970-01-01
  • 2019-12-26
  • 2017-05-28
  • 2018-01-05
  • 2015-03-19
  • 2022-07-25
  • 2019-10-09
  • 1970-01-01
相关资源
最近更新 更多