将 Spring Boot Gradle 项目导入 Eclipse 时遇到问题

Posted

技术标签:

【中文标题】将 Spring Boot Gradle 项目导入 Eclipse 时遇到问题【英文标题】:Trouble importing Spring Boot Gradle project into Eclipse 【发布时间】:2021-09-03 14:22:55 【问题描述】:

我有一个 Sprint Boot 项目,该项目是在另一个系统上构建的,我正在尝试将其导入到 Eclipse (4.20.0) 的新系统中,但我遇到了一些问题。这是一个 Gradle Spring Boot。我的 build.gradle 文件在下面。我在机器上安装了 JKD 16.0.1 -- Windows 10。

我尝试使用 Gradle buildship 和 STS 插件导入它。当我尝试 Buildship 时,它在导入预览中失败并带有大堆栈转储,顶行:

org.gradle.tooling.BuildException: Could not fetch model of type 'GradleBuild' using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-6.8.3-bin.zip'.

当我尝试 Gradle STS 时,当我点击“构建模型”按钮时,我告诉我出现了错误并检查日志文件。我在那里发现的有趣的东西是:

!ENTRY org.springsource.ide.eclipse.gradle.core 4 0 2021-06-18 17:38:25.575
!MESSAGE Could not determine java version from '16.0.1'.
!STACK 0
java.lang.IllegalArgumentException: Could not determine java version from '16.0.1'.

!MESSAGE Initialize project OrderCreate, Update preview failed due to an error in the referenced Gradle build.
!STACK 0
org.gradle.tooling.BuildException: Could not fetch model of type 'GradleBuild' using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-6.8.3-bin.zip'.

Caused by: org.gradle.internal.exceptions.LocationAwareException: Initialization script 'C:\Workspaces\Java\K2\.metadata\.plugins\org.eclipse.buildship.core\init.d\eclipsePlugin.gradle'
Could not compile initialization script 'C:\Workspaces\Java\K2\.metadata\.plugins\org.eclipse.buildship.core\init.d\eclipsePlugin.gradle'.

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during semantic analysis: Unsupported class file major version 60

java.lang.IllegalArgumentException: Unsupported class file major version 60

!ENTRY org.springsource.ide.eclipse.gradle.core 4 0 2021-06-18 17:45:56.693
!MESSAGE Could not determine java version from '16.0.1'.
!STACK 0
java.lang.IllegalArgumentException: Could not determine java version from '16.0.1'.
!MESSAGE Not properly disposed SWT resource
!STACK 0
java.lang.Error: SWT Resource was not properly disposed

!MESSAGE Not properly disposed SWT resource
!STACK 0
java.lang.Error: SWT Resource was not properly disposed

我还尝试从终端构建它以获取一些线索。这给了我“不支持的类文件主要版本 60”错误。

Could not compile settings file 'C:\Workspaces\Java\K2\OrderCreate\settings.gradle'.
> startup failed:
  General error during semantic analysis: Unsupported class file major version 60

这看起来真是一团糟。我从哪里着手解决这个问题?

plugins 
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'eclipse'
    id "com.diffplug.eclipse.apt" version "3.29.1"


group = 'com.bw'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories 
    mavenCentral()


ext 
    mapstructVersion = "1.4.2.Final"


dependencies 
    implementation 'org.springframework.boot:spring-boot-starter-artemis'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    
    implementation("org.mapstruct:mapstruct:$mapstructVersion")
    compileOnly "org.mapstruct:mapstruct-processor:$mapstructVersion"
    annotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
    testAnnotationProcessor "org.mapstruct:mapstruct-processor:$mapstructVersion"
    
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    testImplementation('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'


test 
    useJUnitPlatform()

来自 Gradle Buildship 的错误:

org.gradle.tooling.BuildException: Could not fetch model of type 'GradleBuild' using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-6.8.3-bin.zip'.
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)

at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:122)
Caused by: org.gradle.internal.exceptions.LocationAwareException: Initialization script 'C:\Workspaces\Java\K2\.metadata\.plugins\org.eclipse.buildship.core\init.d\eclipsePlugin.gradle'
Could not compile initialization script 'C:\Workspaces\Java\K2\.metadata\.plugins\org.eclipse.buildship.core\init.d\eclipsePlugin.gradle'.
    
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
Caused by: org.gradle.groovy.scripts.ScriptCompilationException: Could not compile initialization script 'C:\Workspaces\Java\K2\.metadata\.plugins\org.eclipse.buildship.core\init.d\eclipsePlugin.gradle'.
    at 

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during semantic analysis: Unsupported class file major version 60

java.lang.IllegalArgumentException: Unsupported class file major version 60
    at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:196)
    at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:177)
    at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:163)
    at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:284)
    at org.codehaus.groovy.ast.decompiled.AsmDecompiler.parseClass(AsmDecompiler.java:81)
    at org.codehaus.groovy.control.ClassNodeResolver.findDecompiled(ClassNodeResolver.java:251)
    at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScript(ClassNodeResolver.java:189)

【问题讨论】:

【参考方案1】:

您使用的似乎是 Gradle 6.x,它不支持在 Java 16 上运行。您可以降级到 Java 15 或更早版本或升级到 Gradle 7.x。

如果您降级 Java,您仍然可以使用 Gradle 的工具链支持在 Java 16 上构建和运行您的应用。

如果你升级到 Gradle 7.x,你还应该升级到 Spring Boot 2.5.x,它增加了对 Gradle 7 的支持。

【讨论】:

谢谢,安迪。我降级了Java,但我仍然得到同样的错误。当我尝试导入时,我打开“配置工作区设置”并为编译器选择 Gradle 版本 6.8.3 和 Java 11。我仍然看到无法使用与 Gradle 分发“services.gradle.org/distributions/gradle-6.8.3-bin.zip”的连接来获取“GradleBuild”类型的模型。 那个错误的结局是什么?这会告诉你为什么它无法获取模型。 我将转储(部分)添加到我的 OP 中。最后它有“不支持的类文件主要版本 60”错误。我不确定这意味着什么……但我现在可以在 Eclipse 之外构建项目。 gradlew 构建工作。 主要类版本 60 是 Java 16 编写的 .class 文件的版本,因此看起来 Eclipse 在导入您的项目时仍在使用 Java 16。 我明白了。谢谢,安迪。我使用覆盖选项为我的实际 JDK 11 提供了路径,它终于奏效了。

以上是关于将 Spring Boot Gradle 项目导入 Eclipse 时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 无法同步 Spring Boot 项目

将 Gradle.build 版本导入 Spring Boot

导入eclipse gradle spring boot项目后,Lombok无法在intellij idea 18.1版本中工作

Spring Boot gradle 多模块项目未解决的引用错误

intellij idea 2017 gradle构建spring-boot web项目

在 Spring Boot Gradle 项目中导入 JWT(JSON Web Token)有困难