IntelliJ - Spring boot java version 에러

BK·2024년 4월 29일

삽질기록

목록 보기
1/2

이전까지 2.7.x 버전의 spring boot만 사용하다가 (사실 java version 때문에) spring initializr에서 2.7.x 버전을 지원하지 않아 spring boot 3.2.5, 그리고 java 17을 사용하게 되었다.
Spring initializr에서 java 17과 spring boot 3.2.5를 선택하고 IntelliJ에서 프로젝트를 open했는데 gradle build중 에러가 났다.

에러 로그

A problem occurred configuring root project 'demo'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.5.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.5
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.5 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.4' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.5 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.4')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.5 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.4')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.5 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.4')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.5 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.4')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.5 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.4')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.5 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.4')

* 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.

환경

기존에는 java 8, 11 version을 사용해왔고, zulu8과 openjdk11을 사용하고 있었으며, window에 등록된 환경변수의 JAVA_HOME은 openjdk11이었다.
위 에러 메세지는 java17을 써야하는데 java11을 사용하기 때문에 나오는 메세지이고, 해결 방법은 java11 version을 지원하는 spring boot 2.7.x 버전을 사용하거나, jdk 17을 사용하는 것이다.
나는 jdk17을 사용할 생각으로 jdk17을 받고, IntelliJ에서 project sdk를 jdk17 버전으로 변경하였는데도 위와 같은 에러 메세지가 나왔다

해결 방법

위에서 말했듯 spring boot 3부터는 jdk 17 아래 version을 지원하지 않기에
1. Spring boot version을 2로 다운그레이드해서 project를 재 생성. Spring initializr에서 spring boot 2점대 version을 찾을 수는 없지만 잘 찾아보면 이전 spring boot version을 가지고 올려두신 블로그도 있다.
2. JDK version을 17으로 변경

위 두가지 방법을 통해 문제를 해결할 수 있으나, 2 방법을 위해 IntelliJ project의 SDK version을 변경했음에도 에러가 났던 것이다.

이와 같은 에러가 발생한 이유는, 기존 JDK 11기반으로 gradle이 동작하는데 이를 바꿔주지 않았기 때문이다.

IntelliJ에서 File > Settings > Build > Build Tools > Gradle에 Gradle JDK version 을 17으로 변경하면 된다. (아마도)

0개의 댓글