본문 바로가기
Programming/삽질일기

[Gradle] Spring Boot 특정 Java version으로 실행하기

by kghworks 2024. 1. 2.

문제

$ ./gradlew bootRun

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project '...'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.1.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.1
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.1 was found. The consumer was configured to find a library for use during runtime, compatible 
with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.1 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 runtim
e, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.1 declares a component for use during runtime, and its dependencies declared externally[0;39[22;201H
m:
              - 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 8)
                  - 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.5')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.1 declares a library, packaged as a jar, and its dependencies declare
d 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 runtim
e, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.1 declares a library for use during runtime, packaged as a jar, a
nd its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.1 declares a library for use during runtime, packaged as a jar, and its dependencies declared
              - 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 8)
                  - 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.5')

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

BUILD FAILED in 3s

 

$ ./gradlew bootRun 를 실행하니 위와 같이 에러가 나면서 실행되지 않음

 

Gradle 빌드 시 전역 JAVA_HOME 을 참조하는데 내 PC의 JAVA_HOME은 Java 17이 아니라서 문제였음. 프로젝트마다 자바 버전이 달라 환경 변수 수정을 해도 다른 프로젝트에서 문제가 발생할 상황

 

해결

$ ./gradlew bootRun -Dorg.gradle.java.home=<java 17 경로>

 

스프링 부트 시작 시 참고할 java 경로를 명시해주면 해결됨

 

매번 옵션을 주기 귀찮으면 프로젝트 루트 디렉터리에 gradle.properties를 만들어 아래와 같이 옵션을 추가해주면 된다.

org.gradle.java.home=<java 17 경로>

 

만일 IntelliJ에서 build 시 동일한 오류가 나온다면 Settings > Build, Execution,

Deployment > ... > Gradle에 아래 부분이 Java 17인지 확인해볼 것

 

 

댓글