gradle build 에러
Mac 터미널에서 gradle build 를 하는데 에러가 발생했다.
makefile 코드는 다음과 같다.
# backend-build: Build the backend
backend-build:
@echo "Building backend..."
cd backend/Graphizer-Global && gradle build
makefile이 있는 곳에서 backend-build를 실행했는데
다음과 같은 에러가 발생했다.
taekgyu@taekgyuui-MacBookAir DT_P_Graphizer_Global % make backend-build
Building backend...
cd backend/Graphizer-Global && gradle build
> Task :Common-Service:compileJava FAILED
warning: unknown enum constant When.MAYBE
reason: class file for javax.annotation.meta.When not found
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Common-Service:compileJava'.
> java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid'
* 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.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.10.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 1s
3 actionable tasks: 3 executed
make: *** [backend-build] Error 1
현재 java와 gradle에 대한 버전과 위치 정보는 다음과 같다.
taekgyu@taekgyuui-MacBookAir Graphizer-Global % java -version
java version "17.0.12" 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.12+8-LTS-286, mixed mode, sharing)
taekgyu@taekgyuui-MacBookAir Graphizer-Global % which java
/usr/bin/java
taekgyu@taekgyuui-MacBookAir Graphizer-Global % gradle -version
------------------------------------------------------------
Gradle 8.10.2
------------------------------------------------------------
Build time: 2024-09-23 21:28:39 UTC
Revision: 415adb9e06a516c44b391edff552fd42139443f7
Kotlin: 1.9.24
Groovy: 3.0.22
Ant: Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM: 23.0.1 (Homebrew 23.0.1)
Daemon JVM: /opt/homebrew/Cellar/openjdk/23.0.1/libexec/openjdk.jdk/Contents/Home (no JDK specified, using current Java home)
OS: Mac OS X 14.5 aarch64
taekgyu@taekgyuui-MacBookAir Graphizer-Global % which gradle
/opt/homebrew/bin/gradle
지금 상황에서 에러를 해결하기 위한 조치를 해줬다.
1. 첫 번째 조치
해당 에러를 검색해본 결과 해결책으로
build.gradle 파일에
implementation 'javax.annotation:javax.annotation-api:1.3.2'
해당 코드를 넣고 gradle을 reload 했다.
그리고 다시 build.gradle 이 있는 곳에서 gradle build 명령어를 했으나
동일한 에러가 발생했다.
2. 두 번째 조치
brew uninstall gradle 이후
brew install gradle@7 설치
다시 gradle build 명령어를 했으나
동일한 에러가 발생했다.
3. 세 번째 조치
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
해당링크에 설명한대로
MacOS에서 JAVA_HOME 환경 변수를 Java 11의 경로로 설정했다.
https://mincanit.tistory.com/33
[intellij idea] gradle build failed 해결방법
카테캠 실강을 따라하던 중 인텔리제이에서 빌드 할 경우, 계속 아래와 같은 오류가 나왔다. ✘ jiminkkk@jimin-ui-MacBookPro ~/Desktop/KakaoTechCam/lecture/kakao-6th-deploy main ± ./gradlew clean build > Task
mincanit.tistory.com
이렇게 하고
build.gradle 파일이 있는 곳에서
gradle build 명령어를 입력하니 정상적으로 작동했다!
해당 에러는 Java 컴파일러 버전과 Gradle 빌드 설정 간의 호환성 문제 때문에 발생했다.
특히 java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid' 오류는 Gradle이 내부적으로 Java 컴파일러를 사용할 때, 자바 버전 간의 차이로 인해 특정 클래스나 필드를 찾지 못해서 생기는 문제이다.
JAVA_HOME 경로를 수정했을 때 gradle build 가 성공한이유
이 문제는 Gradle이 Java 17 대신 Java 11 컴파일러를 사용할 때 해결된 것이다.
MacOS에서 JAVA_HOME을 Java 11 경로로 설정함으로써, Gradle은 Java 11을 사용하게 되었고, 이로 인해 호환성 문제가 해결됐다.
Java 17과 Gradle의 특정 버전 조합에서 javax.annotation과 같은 일부 API나 컴파일러 내부 구현 차이가 발생할 수 있는데, Java 11로 JAVA_HOME을 설정하면 Gradle이 더 안정적으로 빌드를 수행할 수 있다. 이를 Java 11 경로를 설정해서 호환이 되도록 했다.