Eat Read Code

 

build.gradle은 Gradle(그레이들)의 기본 빌드 스크립트이다.

스프링 부트에서 빌드 도구로 그레이들을 선택하면 이 파일을 자주 보게 되는데, 때로는 여기서 설정을 잘못해서 오류가 발생하기도 한다. 그레이들을 깊이 공부할 필요까진 없다고 생각하지만(물론 하면 좋다), 최소한 build.gradle만 제대로 알아놔도 나중에 큰 도움이 된다.

 

인텔리제이에서 Spring Initializr로 새 프로젝트를 생성하고 빌드 도구를 그레이들로 설정하면 다음과 같은 build.gradle 파일이 프로젝트 최상단에 생성된다. dependency는 Spring Web, Spring Data JPA, MySQL Driver, H2 Database를 선택했다. 그레이들 버전은 6.3이다.

 

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

 

이제 하나하나 살펴보자.

 

 

1. 플러그인

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

 

위의 코드는 Spring Boot 플러그인, Dependency Management 플러그인, Java 플러그인을 적용한다.

 

그레이들에서 구체적인 작업들은 대부분 플러그인으로 구현되어 핵심 기능과 분리되어 있다. 특히 여기서 Java 플러그인은 Task, Source set, Convention properties 등을 포함하여 다양한 기능을 제공하고 있다. 자세한 내용은 공식 문서를 참고하자(읽을 가치가 있다!).

 

2. 속성

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

 

group

  • The group of this project. Gradle always uses the toString() value of the group. The group defaults to the path with dots as separators.
  • 프로젝트의 그룹을 지정한다. 기본값이 있기 때문에 이 부분을 지워도 동작한다.

 

version

  • The version of this project. Gradle always uses the toString() value of the version. The version defaults to unspecified.
  • 프로젝트의 버전을 지정한다. group과 마찬가지로 이 부분을 지워도 동작한다.

 

sourceCompatibility

  • The source compatibility used for compiling Java sources.
  • 컴파일에 사용할 자바 버전을 지정한다.

 

3. 저장소

repositories {
    mavenCentral()
}

 

dependency 저장소로 메이븐 중앙 저장소를 선택한다.

 

4. 의존 라이브러리

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

 

프로젝트의 의존 라이브러리를 지정한다. implementation, runtimeOnly, testImplementation 등의 차이는 이름만으로도 대충 짐작이 가능할 것이다. 자세한 내용은 공식 문서를 참고하자.

 

참고로 compile은 deprecated이므로 implementation을 사용하는 것이 좋다. 그리고 맨 아랫줄에서 spring-boot-starter-test를 불러올 때 junit-vintage-engine 모듈을 exclude하는 것을 볼 수 있는데, 이는 JUnit 5의 Vintage 모듈이 JUnit 3과 JUnit 4 기반으로 작성된 테스트를 실행하기 위한 테스트 엔진을 제공하기 때문이다. 즉, 새 프로젝트를 만들고 JUnit 5 기반의 테스트를 작성한다면 해당 모듈이 필요하지 않기 때문에 제외하는 것이다.

 

5. 테스트

test {
    useJUnitPlatform()
}

 

위의 코드는 그레이들이 JUnit Platform 모듈(JUnit 5)로 테스트를 실행하게 한다.

 

 

이외에도 태스크, 빌드 등 알면 좋은 것들이 많다. 공식 문서가 아주 깔끔하게 잘 되어 있으니 참고하자. 또한 『Gradle 철저 입문』(와타비키 타쿠마 등, 길벗)도 괜찮은 책이다. 다만 2015년에 출간되어 무려 2.5 버전을 사용한다. 그래도 그레이들을 아주 꼼꼼하게 다루고 있어서 읽을 가치가 있다.