반응형

Gradle 시작하기

Maven 을 대체할수 있는 Groovy DSL 기반 어쩌구 저쩌구 하는데.. 다 됐고! Spring 이 빌드 시스템을 Maven 에서 Gradle 로 변경했다고 한다.

Gradle 이 왜 Maven 보다 좋은지, 왜 사용해야하는지를 알고 싶다면 글 마지막 부분에 있는 여기 를 참고하도록 하자.

1. Installing Gradle

먼저 여기 를 통해 Gradle 을 다운받자. 바이너리를 받으면 되고 이후에는 적당한 폴더에 압축을 풀면 된다. 그 후에는 가이드 를 따라서 환경변수를 등록하면 된다. 내 경우에는 C:\gradle 에 설치를 했다.

// 환경변수 추가
GRADLE_HOME - 'C:\gradle'
Path - 'C:\gradle\bin'

cmd > gradle -v // 터미널에서 Gradle 버전 확인
------------------------------------------------------------
Gradle 1.10
------------------------------------------------------------

Build time:   2013-12-17 09:28:15 UTC
Build number: none
Revision:     36ced393628875ff15575fa03d16c1349ffe8bb6

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:          2.2.0
JVM:          1.7.0_45 (Oracle Corporation 24.45-b08)
OS:           Windows 7 6.1 x86


2. Installing Gradle on STS

이건 더 쉽다. Sping Tool Suite 에서 Dashboard -> Install Extension -> Gradle Support

3. Making Build Script

Gradle BuildProjectTask 라는 단위로 나누어진다. 하나의 Build는 여러개의 Project로 나뉠 수 있고 다시 Project는 하나 이상의 Task 로 나뉠 수 있다. Project는 웹 어플리케이션 배포나, Jar 컴파일등의 작업으로 볼 수 있고 이걸 하기 위해서 진행되는 세부적인 작업이 Task라고 보면 된다. 좀 더 정확한 설명을 위해 원문을 첨부한다.

Everything in Gradle sits on top of two basic concepts: projects and tasks.

Every Gradle build is made up of one or more projects. A project represents some component of your software which can be built. What this means exactly depends on what it is that you are building. For example, a project might represent a library JAR or a web application. It might represent a distribution ZIP assembled from the JARs produced by other projects. A project does not necessarily represent a thing to be built. It might represent a thing to be done, such as deploying your application to staging or production environments. Don’t worry if this seems a little vague for now. Gradle’s build-by-convention support adds a more concrete definition for what a project is.

Each project is made up of one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating javadoc, or publishing some archives to a repository.


그럼 실제 스크립트를 보도록 하자. 프로젝트 내에 build.gradle 파일이 하나씩 존재하며 gradle build처럼 실행시킬 수 있다.build란 단어 대신에 다른task 이름이 올 수 있다. gradle task1 처럼.


// gradle.build 
// created by STS Gradle Extension with Java Quickstart Option

// Java, Eclipse Project
apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
}

// Maven Repository 사용
repositories {
    mavenCentral() 
}

// 사용하는 Maven Repository 지정
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}


gradle test 를 통해 테스트만 실행하거나, gradle build를 통해 테스트를 포함해서 빌드를 실행할 수 있다.


다음번에는 조금 더 자세히 빌드 스크립트에 대해서 알아보겠다.

4. 후기

Gradle 에선 Groovy 를 사용한다는데 중간에 Gradle User Guide 보다가 소름 돋아서 후기를 남긴다. 디렉토리 이름을 받아 존재하는 파일들을 배열로 돌려주는 Groovy 코드.

File[] fileList(String dir) {
    file(dir).listFiles({file -> file.isFile() } as FileFilter).sort()
}

이게 함수형 언어를 사용하는 진짜 이유다. Java 였다면 얼마나 코드가 길어졌을까..

 

반응형
LIST

+ Recent posts