(준비) Gradle 설치하기 – Spring4

Spring Framework 4.0을 공부해 보려 한다. 그런데, 이제는 Spring core team에서도 maven이 아닌 gradle로 관리를 한다고 한다. Spring4를 시작하기 전에 Gradle 부터 간단히 알아보고 가보자.

% 이글은 “OS X”에서 작업한 내용입니다.

Gradle 설치

Gradle은 아래 사이트에서 받을 수 있다.

페이지: http://www.gradle.org/downloads
파일: gradle-{version}-bin.zip(ex: gradle-2.2-bin.zip)

다운로드 받은 파일의 압축을 풀고, gradle folder 밑의 bin folder를 path에 추가 한다. 테스트를 위해 command-line에서 아래 명령어를 입력해 본다.

gradle

정상적으로 설치가 되었다면, 아래 메시지를 확인할 수 있을 것이다.

:help
Welcome to Gradle 2.2.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
BUILD SUCCESSFUL

이제 Sample을 다운로드 받아 간단하게 알아 보도록 하자. 이걸 다운로드 받으려면, git이 설치되어 있어야 한다. command-line에서 아래 명령을 실행하도록 한다.

git clone https://github.com/spring-guides/gs-gradle.git

실행결과 gs-gradle이라는 folder가 생길 것이다.

Gradle 훓어보기

gs-gradle folder 밑에 있는 “complete” folder로 이동한다.  아래와 같은 file 및 folder들이 보일 것이다.

gradle
src
build.gradle
gradlew
gradlew.bat

build.gradle이라는 file이 maven의 pom.xml과 같은 것이라고 보면 될 거 같다.  일단, 여기서는 gradle 명령어로 build를 하고 run을 하는 방법까지만 알아 보도록 하자. 우선, src/main/java/hello/HelloWorld.java 코드를 살펴보면,

package hello;
import org.joda.time.LocalTime;

public class HelloWorld {
  public static void main(String[] args) {
    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

org.joda.time.LocalTime 이라는 외부 모듈을 사용하고 있다. maven이라면, 이것을 pom.xml의 dependenies에 이렇게 추가했을 것이다.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.2</version>
</dependency>

이것을 build.gradle file에는 어떻게 기술했는 지를 알아보자. 우선, reposigory를 설정해 두어야 한다. 아래와 같이

repositories { 
    mavenLocal()
    mavenCentral() 
}

이제 앞서 언급한 maven 형식의 dependency를 어떻게 기술하는 지 알아보자. 결과는 이렇다.

dependencies {
    compile "joda-time:joda-time:2.2"
}

compile할 때 사용한다는 의미로 “compile”을 맨 앞에 써주었고, 한 칸 이상 띄운다음

"{groupId}:{artifactId}:{version}"

위와 같이 groupId, artifactId, version을 순서대로 해서, 사이 사이에 “:”을 넣은 것이 되겠다.  build.gradle file의 맨 위를 보면, 아래 내용을 볼 수 있는데,

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = 'hello.HelloWorld'

apply plugin: ‘java’는 java 코드를 build할 수 있게 해주는 것 같고, apply plugin: ‘application’은 java 코드를 실행할 수 있게 해 주는 것 같다. java code를 실행하기 위해서는 mainClassName을 위와 같이 설정해 주어야 한다.

소스코드 build와 java code를 실행하는 명령은 각각 다음과 같다.

gradle build
gradle run

“gradle build”를 실행하면, “build” folder가 생성되는 데, 이것이 바로, 우리가 흔히 보던 “target” folder 즉, 컴파일된 결과물들이 생성되는 folder이다.

Gradle 설치와 관련해서 간단히 알아 보았다. Vert.x 등 최근 Project들이 build tool로 maven보다는 gradle을 선호하는 경향이 보인다. 아직 gradle을 잘 알지는 못하지만, 익혀볼 가치는 있을 것 같다.