본문 바로가기
공부

Gradle 의존성(Dependencies) 주요 공식문서 정리

by nahowo 2025. 2. 6.

 

참고자료

1. Declaring dependencies

2. Dependency Configurations

Producers & Consumers

  • 라이브러리를 빌드하는 주체를 producer, 해당 라이브러리를 사용하는 사람들, 즉 해당 라이브러리에 의존하는 주체를 consumer라고 부른다. consumer는 다른 프로젝트에 의존하는 프로젝트/의존성을 명시하는 configuration이라고 정의할 수 있다.
  • producer
    • Library
    • API
    • SDK
  • consumer
    • web application
    • mobile application
    • cloud application

의존성 추가

  • dependencies {} 블록을 사용해 gradle에 의존성을 추가할 수 있다.
  • 외부 의존성은 configuration 이름을 사용해 명시한다.
    • implementation, compileOnly, testImplementation 등
dependencies {
    // Configuration Name + Dependency Notation - GroupID : ArtifactID (Name) : Version
    configuration('<group>:<name>:<version>')
}
  • gradle은 의존하는 의존성의 의존성도 추가해주는 transitive dependencies를 지원한다.
  • gradle은 컴파일 시간/런타임 시간/특정 테스트 시나리오 등 의존성 별 범위를 제공한다.
  • 빌드 파일에서 어떤 레포지토리에서 의존성을 가져올 것인지 명시할 수 있다.

의존성의 종류

1. Module Dependencies

  • 모듈 의존성은 레포지토리의 모듈을 의미한다.
  • dependencies { implementation 'org.codehaus.groovy:groovy:3.0.5' implementation 'org.codehaus.groovy:groovy-json:3.0.5' implementation 'org.codehaus.groovy:groovy-nio:3.0.5' }

2. Project Dependencies

  • 프로젝트 의존성은 같은 빌드 내의 다른 프로젝트 의존성을 명시할 수 있도록 한다.
  • dependencies { implementation project(':utils') implementation project(':api') }

3. File Dependencies

  • 파일 의존성은 의존성을 공유 드라이브에서 호스트하거나, 버전 관리 시스템에 소스 코드와 함께 보관한다. 이런 의존성은 메타데이터(transitive dependencies, origin, author 등)를 가지지 않는다.
  • dependencies { runtimeOnly files('libs/a.jar', 'libs/b.jar') runtimeOnly fileTree('libs') { include '*.jar' } }

예시로 이해하기

dependencies {
    implementation 'com.google.guava:guava:23.0'
}
  • implementation은 configuration이다.
  • com.google.guava:guava:23.0은 라이브러리의 그룹, 이름, 버전이다.
    • com.google.guava: 그룹 ID
    • guava: artifact ID(이름)
    • 23.0: 버전

Dependency Configuration

  • 모든 의존성은 특정 범위에 적용된다.

Dependency configuration 이해하기

  • 아래의 cofiguration들은 의존성을 선언하기 위해 사용된다.
Cofiguration 설명
api 컴파일, 런타임, 배포 API에 필요한 의존성
implementation 컴파일, 런타임에 필요한 의존성
compileOnly 컴파일에만 필요한 의존성
compileOnlyApi 컴파일과 배포 API에만 필요한 의존성
runtileOnly 런타임에만 필요한 의존성
testImplementation 컴파일과 테스트 실행에 필요한 의존성
testCompileOnly 테스트 컴파일에만 필요한 의존성
testRuntimeOnly 테스트 실행에만 필요한 의존성

Other configuration

  • runtimeClasspath, compileClasspath, apiElements, runtimeElements 등의 configuration도 존재하지만, 위처럼 의존성을 선언하기 위해서 사용되는 것은 아니다.
  • 커스텀 configuration을 만드는 것도 가능하다. 의존성을 분리하고 특정 빌드 프로세스에만 포함되도록 설정할 수 있다.

configurations 조회하기

  • dependencies {} 블록(태스크)는 의존성의 개요를 제공한다. 의존성에 대한 더 자세한 정보는 아래 예시와 같이 —-configuration 파라미터를 사용해 확인할 수 있다.
$ ./gradlew -q app:dependencies --configuration implementation

------------------------------------------------------------
Project ':app'
------------------------------------------------------------

implementation - Implementation only dependencies for source set 'main'.
\\--- com.google.guava:guava:30.0-jre