본문 바로가기
Gradle

build.gradle 이해하기

by worldcenter 2025. 9. 23.

🐘 Gradle 이란?

애플리케이션 빌드 자동화 도구로 Java, Kotlin, Groovy 등 다양한 언어와 플랫폼을 지원하며 JVM 기반 프로젝트(Spring, Android)에서 주로 사용합니다. Gradle을 통해 컴파일, 테스트, 패키징(Jar, War), 의존성 관리, 배포 자동화 작업을 할 수 있습니다.

 

 

build.gradle 파일

build.gradle 파일은 프로젝트 빌드에 필요한 설정들을 선언하고, 플러그인과 의존성을 정의할 수 있습니다. build.gradle은 Groovy 또는 Kotlin 문법으로 작성할 수 있습니다. 본문에서는 Groovy 문법으로 build.gradle을 살펴보려고 합니다.

 

 

Project Object

Springboot Application을 처음 생성하면 아래와 같은 양식으로 build.gradle이 생성됩니다. 각각의 요소를 하나씩 살펴보기 전에 Project Object를 이해해야 합니다.

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.5.3'
	id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.springboot'
version = '0.0.1-SNAPSHOT'

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(17)
	}
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {

    ...
}

tasks.named('test') {
	useJUnitPlatform()
}

 

Project Object 란 Gradle에서 프로젝트를 나타내는 객체로 build.gradle 파일이 로드될 때 gradle은 각 프로젝트마다 하나의 Project 인스턴스를 생성합니다. build.gradle 안에서 쓰는 DSL 구문 대부분은 Project 객체의 메서드나 프로퍼티를 호출하는 것과 같습니다.

build.gradle은 하나의 Project 객체와 연결되고, 멀티모듈을 채택한 경우에도 루트 프로젝트, 서브 모듈별로 각각 Project객체를 갖습니다. 

 

 

Plugins 객체

plugins {...} 블록은 Project 객체가 제공하는 Properties 중 하나로 현재 프로젝트에 적용된 plugin 인스턴스들을 관리하는 컨테이너 역할을 합니다. 아래 build.gradle 에는 java, boot, dependency-management 플러그인이 등록되어 있습니다.

플러그인을 적용하면 새로운 Task, DSL(설정 블록), Convention(기본값 설정) 등이 추가됩니다.

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.5.3'
	id 'io.spring.dependency-management' version '1.1.7'
}

 

 

repositories 객체

repositories 블록은 Project 객체가 제공하는 메소드 중 하나로 gradle이 라이브러리를 다운로드 하는 저장소를 정의하는 블록입니다. Maven 중앙 저장소 외에도 로컬 저장소, 사내 저장소 등을 지정할 수 있습니다.

repositories {
	mavenCentral()
}

 

 

dependencies 객체

Gradle에서 현재 프로젝트가 사용하는 라이브러리 의존성을 정의하는 블록입니다. 즉, repositories {...} 블록에서 정의한 저장소에서 어떤 라이브러리들을 가져올지 정의하는 블록입니다. Project 객체가 제공하는 메소드 중 하나 입니다.

dependencies {

	// querydsl
	implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
	annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
	annotationProcessor "jakarta.annotation:jakarta.annotation-api"
	annotationProcessor "jakarta.persistence:jakarta.persistence-api"

	// lombok
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	// DB
	runtimeOnly 'com.mysql:mysql-connector-j'

	// test
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'com.h2database:h2:2.2.224'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

 

dependencies configuration

configuration Compile 시 필요 Runtime 시 필요 주요 용도
implementation O O 일반적인 의존성 선언
compileOnly O X Lombok, Servlet API 등 실행 환경에 이미 존재하는 경우
runtimeOnly X O JDBC Driver 등 실행 시점에만 필요한 경우
testImplementation O O -
testCompileOnly O X -
testRuntimeOnly X O -
annotationProcessor O X 어노테이션을 해석하는 용도

 

 

Tasks 객체

Project는 Task 객체들의 집합입니다. 각 Task는 컴파일, 테스트 실행, 파일(Jar, War) 압축과 같은 기본적인 작업을 수행합니다. 아래 task는 이미 정의된 'test' Task를 가져와서 설정합니다.

tasks.named('test') {
	useJUnitPlatform()
}

 

 

참고 링크

https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html

 

Project (Gradle API 9.1.0)

container Creates a container for managing named objects of the specified type. The given factory is used to create object instances. All objects MUST expose their name as a bean property named "name". The name must be constant for the life of the object.

docs.gradle.org

https://docs.gradle.org/current/userguide/plugins.html

 

Understanding Plugins

In the example used through this section, the plugin accepts the Project type as a type parameter. However, Gradle supports applying plugins in three distinct contexts: Project, Settings, and Init. Each type of plugin serves a different purpose and is appl

docs.gradle.org

https://docs.gradle.org/current/userguide/declaring_repositories.html

 

3. Declaring repositories

Most enterprise projects establish a binary repository accessible only within their intranet. In-house repositories allow teams to publish internal binaries, manage users and security, and ensure uptime and availability. Specifying a custom URL is useful f

docs.gradle.org

https://docs.gradle.org/current/userguide/dependency_configurations.html

 

2. Dependency Configurations

Every dependency declared for a Gradle project applies to a specific scope. For example, some dependencies should be used for compiling source code whereas others only need to be available at runtime: build.gradle.kts dependencies { implementation("com.goo

docs.gradle.org

https://docs.gradle.org/current/userguide/organizing_tasks.html

 

Organizing Tasks

Lifecycle tasks can be particularly beneficial for separating work between users or machines (CI vs local). For example, a developer on a local machine might not want to run an entire build on every single change. Let’s take a standard app as an example

docs.gradle.org