본문 바로가기

Groovy/gradle

[gradle] buildscript dependencies 와 dependencies 의 차이


gradle을 이용하여 의존성 관리 스크립트를 짜면

보통 다음과 같은 형태의 스크립트로 구성한다.

apply plugin: 'java' repositories { mavenCentral() } dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' testCompile group: 'junit', name: 'junit', version: '4.+' }

출처 - gradle.org 문서의 Example 8.1


하지만 종종 위와 같은 형태가 아닌

repositories{} 와 dependencies{} 를 buildscript{} 로 감싸는 경우가 있다.

import org.apache.commons.codec.binary.Base64

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
    }
}

task encode << {
    def byte[] encodedString = new Base64().encode('hello world\n'.getBytes())
    println new String(encodedString)
}

출처 - gradle.org 문서의 Example 60.7


위의 코드를 보면

외부 라이브러리 의존관계 설정에 compile, 이나 testCompile 등이 아닌 classpath를 사용 하고 있다.


어떤차이가 있을까?

전자의 경우엔 명시된 상황에서만 의존 라이브러리를 참조 할 수 있다.

즉 compile 로 명시된 라이브러리의 경우 자바 컴파일 시에 라이브러리를 참조한다는 이야기이다.(이때는 testCompile의 라이브러리를 참조하지 않는다.)

위 의존관계에 대한 자세한사항은 다음을 참조

http://www.gradle.org/docs/current/userguide/java_plugin.html#tab:configurations


이번엔 후자를 보자.

gradle을 사용할 때 task를 많이 사용 하게 될 것이다. 그리고 종종 스크립트 안에서 바로 외부 라이브러리의 특정 메소드를 사용해야 하는 경우가 있다. 그럴 경우 후자처럼 코드를 작성해야 한다.

예제에서는 apache commn codec 라이브러리의 Base64.encode() 메소드를 사용하기 위한 방법을 보여준다.

즉 후자와 같은 경우는 gradle의 스크립트에서 바로 외부 라이브러리를 참조해야 할 경우에 사용하는 방법이다.



참고자료

http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:external_dependencies

http://stackoverflow.com/questions/13923766/gradle-buildscript-dependencies

http://chimera.labs.oreilly.com/books/1234000001741/ch04.html#_buildscript_dependencies