在 IDEA 中使用 Gradle 编译项目报错:Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, ...

满仓在家 / 2024-10-07 / 原文

一、问题

在使用 IDEA 编译 Gradle 项目时出现以下报错:

A problem occurred evaluating root project 'spring-security-samples'.
> Could not create task ':runAllTests'.
   > A problem occurred configuring project ':reactive:rsocket:hello-security'.
      > Could not resolve all files for configuration ':reactive:rsocket:hello-security:classpath'.
         > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT.
           Required by:
               project :reactive:rsocket:hello-security > org.springframework.boot:org.springframework.boot.gradle.plugin:3.4.0-SNAPSHOT:20240930.233924-386
            > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT:20240930.233924-386 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.3' but:
                - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT declares a library, packaged as a jar, and its dependencies declared externally:
                    - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
                    - Other compatible attribute:
                        - Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
                - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT declares a component for use during runtime, and its dependencies declared externally:
                    - Incompatible because this component declares documentation and the consumer needed a library
                    - Other compatible attributes:
                        - Doesn't say anything about its target Java version (required compatibility with Java 11)
                        - Doesn't say anything about its elements (required them packaged as a jar)
                        - Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
                - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
                    - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
                    - Other compatible attribute:
                        - Doesn't say anything about org.gradle.plugin.api-version (required '8.3')
                - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.4.0-SNAPSHOT declares a component for use during runtime, and its dependencies declared externally:
                    - Incompatible because this component declares documentation and the consumer needed a library
                    - Other compatible attributes:
                        - Doesn't say anything about its target Java version (required compatibility with Java 11)
                        - Doesn't say anything about its elements (required them packaged as a jar)
                        - Doesn't say anything about org.gradle.plugin.api-version (required '8.3')

二、分析

关键报错是:Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11,翻译过来就是:组件使用 Java 17 编译的,但是运行时使用的 Java 11 环境。

三、解决

调整 IDEA 中 Gradle 的运行环境(IDEA 版本 2024.2.0.2)。Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM -> JDK 17
Gradle 的 JVM 设置

四、参考

https://github.com/redhat-developer/vscode-java/issues/3094