[Spring Boot] Gradle 빌드 에러 해결 - buildscript {}, pluginManagement {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed
by 무작정 개발Intro
bashCould not compile build file 'C:\Users\hwcot\IdeaProjects\Inflearn-Spring-PlayGround\core\build.gradle'. > startup failed: build file 'C:\Users\hwcot\IdeaProjects\Inflearn-Spring-PlayGround\core\build.gradle': 28: only buildscript {}, pluginManagement {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed
오랜만에 Spring Boot + Gradle 프로젝트를 open 하며 build를 진행하며 위 에러를 만나 해결 방법에 대해 정리하게 되었습니다.

원인
필자는 약 1년 만에 Gradle을 사용해서 뭐지 싶었는데, 원인은 매우 간단하고 해결 방법 또한 간단하였습니다.
Spring Boot 와 빌드 도구를 Maven 대신 [Gradle]을 사용할 때 build.gradle 파일을 수정하면 해결됩니다.
build.gradle 파일을 작성할 때 위 Error 메시지 뜻 그대로 buildscript { } -> plugins { } -> apply plugin 순서를 지켜야 합니다.
또한 plugins { } 앞에는 오직 buildscript { }와 plugins { }만 앞에 작성할 수 있습니다.
[요약]
- build.gradle 파일 작성 시 순서를 지켜줘야 한다.
- 1. buildscript { }
- 2. plugins { }
- 3. apply plugin
해결 방법
위 원인에 대한 해결방법을 build.gradle 파일에 적용하였습니다.
javascript# 기존 [AS-IS] plugins { id 'java' id 'org.springframework.boot' version '2.7.6' id 'io.spring.dependency-management' version '1.0.15.RELEASE' } # 수정 후 [TO-BE] buildscript { repositories { mavenLocal() maven { url 'https://maven.aliyun.com/repository/google/' } maven { url 'https://maven.aliyun.com/repository/public/' } maven { url 'https://maven.aliyun.com/repository/spring/' } maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' } maven { url 'https://maven.aliyun.com/repository/spring-plugin/' } maven { url "https://plugins.gradle.org/m2/" } mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.7.6") classpath "io.spring.gradle:dependency-management-plugin:1.0.15.RELEASE" } } plugins { id 'java' } apply plugin: "org.springframework.boot" apply plugin: "io.spring.dependency-management"
위 방법으로 build.gradle 파일을 수정 후 재 빌드 시 성공적으로 빌드가 되었습니다.
혹여나 위 방법으로 해결되지 않는다면 인텔리제이 설정을 확인해보아야 합니다.
이외 확인 해야할 부분
1. 프로젝트 JDK 설정 확인
![[IntelliJ] Project JDK 설정](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
[Windows 환경]
- 좌측 상단 > File > Project Structure > SDK 확인하기
- 단축키 : Ctrl + Alt + Shift + S
[Mac 환경]
- File > Project Structure (⌘;)
위 이미지에 표시된 JDK 버전이 본인이 설치한 자바 버전이 맞는지 확인해야 합니다.
필자는 Java 11 버전을 사용하여 위 SDK에서도 11 버전으로 설정되어 있습니다.
2. Gradle JDK 설정
![[IntelliJ] Gradle JDK 설정 확인](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
[Windows 환경]
- 좌측 상단 > File -> Settings > Gradle 검색 > Gradle JVM 설정 확인
[Mac 환경]
- Preferences(⌘,)
위 이미지처럼 빨간색으로 표시된 [Gradle JVM] 설정에 본인이 설치한 Java 버전이랑 맞는지 확인합니다.
마지막으로 프로젝트를 모두 닫은 뒤에 다시 해당 프로젝트를 Open 해서 다시 build 합니다.
Reference
위 방법으로 해결되지 않는다면 아래의 링크를 참고하시면 다양한 원인에 대한 해결 방법을 찾을 수 있습니다.
학습 페이지
www.inflearn.com
블로그의 정보
무작정 개발
무작정 개발