tools & libs/빌드툴(maven,gradle)

[gradle]build.gradle

  • -
반응형

build.gradle 작성 언어

build.gradle은 gradle의 init task에 의해 생성되는 파일이다. 이 파일은 그루비나 코틀린 기반으로 작성될 수 있는데 기본 파일은 그루비로 작성되어있다. 

그루비는 자바 기반의 스크립트 언어로 자바를 할 수 있다면 매우 손쉽게 배워볼 수 있는 언어이다. 하지만 아무리 쉬워도 겨우 build 하기 위해 언어를 배운다는 것은 부담스러울 수 있다. 그런데 엄밀히 말하면 build.gradle은 그루비를 그대로 사용하는 것은 아니고 그루비 DSL을 사용한다.

DSL은 Domain-specific languages의 약자로 말 그대로 특정 용도에 맞게 쉽게 편집한 언어로 생각하면 된다. 결론은 Groovy를 gradle에서 사용하기 위해 쉽게 만든 것이다. 그래서 대충만 봐도 뭐 하는 내용인지 알 수 있다.

더군다나 우리는 전체 파일을 다 만들 필요가 없고 기본 뼈대를 수정하기만 하니까 걱정할 필요는 전혀 없다.

새롭게 gradle 기반 프로젝트를 만들어보고 build.gradle을 살펴보자.

 

build.gradle

 

gradle은 org.gradle.api.Project에 Gradle 프로젝트 구성에 필요한 내용을 다 선언해 놓았다. 결국 build.gradle 파일은 Project에 선언된 속성을 채워가는 과정이라고 생각하면 쉽다.

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

 

Project (Gradle API 6.8.1)

Returns this project. This method is useful in build files to explicitly access project properties and methods. For example, using project.name can express your intent better than using name. This method also allows you to access project properties from a

docs.gradle.org

 

기본 구조

처음 생성된 build.gradle은 아래의 구조를 갖는다.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

test {
    useJUnitPlatform()
}

group 'org.example'
version '1.0-SNAPSHOT'

build.gradle에 선언된 plugins나 repositories처럼 {}를 가진 것들을 클로저들이다. 클로저 내부의 각 항목들은 Gradle 함수를 호출하는 것인데 Groovy에서는 함수를 사용하면서 파라미터를 생략한다. 

따라서 plugins 는 아래와 같이 사용할 수 있다.

plugins {
    id('java')
}

 

plugins

plugins {
    id 'java'
}

프로젝트를 빌드하기 위해 필요한 작업들을 지원하는 플러그인을 설정한다. 이 플러그인들은 여러 가지 task를 내장하고 있다. 앞에서 실행해보았던 jar task 도 java-library에 포함된 task이다.

또한 plugin은 프로젝트의 구조도 결정짓는다. source 파일의 경로, test 의 경로등도 미리 설정되어있고 이를 바탕으로 프로젝트를 구성한다.

예를 들어 application 을 추가하고 tasks를 살펴벼면 application/run이 추가된 것을 확인할 수 있다.

plugins {
    id 'java'
    id 'application'
}
java만으로 구성되었을 경우   application이 추가된 경우
 

application/run을 실행하기 위해서는 main 메서드를 가진 클래스를 지정해줘야 한다.

자바 파일을 추가한 후

package com.quietjun.gradle;

public class AppMain {
    public static void main(String[] args) {
        System.out.println("Hello Gradle World");
    }
}

build.gradle에 아래와 같이 추가하고 run task를 실행해보자.

application{
    mainClass.set('com.quietjun.gradle.AppMain')
}

 

다음 링크에서 gradle이 미리 설정해 놓은 다양한 plugin들을 알아볼 수 있다.

docs.gradle.org/current/userguide/plugin_reference.html#header

 

Gradle Plugin Reference

This page contains links and short descriptions for all the core plugins provided by Gradle itself.

docs.gradle.org

 

repositories

프로젝트에서 사용되는 종속 라이브러리를 가져오기 위한 저장소를 등록하는 부분이다. 일반적으로 Apache의 Maven 저장소를 사용하기 위해 mvnCentral()을 사용하고 jcenter()라는 저장소를 사용할 수도 있다.

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

 

dependencies

프로젝트에서 필요한 라이브러리를 등록하는 부분이 dependencies이다. 

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

dependencies의 각 항목은 <depencency configuration> <라이브러리 지정>로 구성된다.

 사용되는 dependency configuration은 아래와 같다.

dependency configuration 의미
compileOnly  컴파일 하는데는 필요하지만 런타임 클래스 패스에는 포함되지 않는 종속성을 나타낸다.
implementation  기존의 compile을 대체하며 컴파일과 런타임에 사용된다.
runtimeOnly  기존의 runtime을 대체하며 컴파일 타임이 아닌 런타임에만 사용된다.
testCompileOnly  test에서만 적용되는 compileOnly 속성이다.
testImplementation  test에서만 적용되는 implementation 속성이다.
testRuntimeOnly  test에서만 적용되는 runtimeOnly 속성이다.

 

라이브러리를 지정할 때는 '그룹:이름:버전' 형태를 사용한다. 이 값은 저장소에서 제시하는 값을 그대로 사용하면 된다.

 

간혹 최신의 라이브러리들이 검색되지 않는 경우가 있는데 설정에서 Maven Reposotory의 index를 업데이트 해줘야 한다. 아래는 intellij에서의 인덱스가 업데이트 되지 않은 모습이다.

 

maven repository를 선택 후 update를 클릭해서 최신의 내용을 가져오도록 하자.

 

다음은 implementation 쪽에 의존성을 추가한 예이다. (괄호는 물론 생략할 수 있다.)

dependencies {
    implementation('com.google.code.gson:gson:2.8.6')
    implementation 'com.github.noraui:ojdbc7:12.1.0.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

 

test

test는 테스팅 과정에서 사용할 프레임워크를 지정한다.

useTestNG()나 useJunitPlatform() 등을 사용할 수 있는데 junit이 default이다.

 

기타

이 외에도 소소하게 group이나 version 등의 정보를 설정할 수도 있다.

group 'com.quietjun'
version '1.0-SNAPSHOT'

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.