컴파일할 때만 사용
compileOnly로 의존성을 추가하면 jar에 포함되지 않아 가벼워진다.
런타임에만 사용 (ex. db-connector, mysql, h2)
컴파일할 때는 필요없는 라이브러리 의존성 추가할 때 사용한다.
어노테이션 프로세서 사용하는 라이브러리인 경우 사용, 컴파일에만 적용 (ex. lombok)
컴파일 + 런타임
컴파일에도 필요하고 런타임에도 필요할 때 사용
api로 추가한 라이브러리에서 의존하고 있는 라이브러리에 의존하고자 할 때 사용 (컴파일 + 런타임)
권장하지 않는다!
a가 b 라이브러리를 api로 추가하면 a는 b에서 사용 중인 라이브러리 c를 사용할 수 있다.
이 방법은 조심해야 하는데 b에서 사용하고 있는 라이브러리 c를 d로 바꾼다면? a가 사용하는 c가 사라졌기 때문에 문제가 발생할 수도 있다.
테스트 코드 컴파일할 때만 사용
테스트 런타임에 사용
테스트 컴파일 + 런타임에 사용
build.gradle에서 사용하는 implementation과 api는 모두 라이브러리를 적용시키는 키워드이다. 오래된 Gradle 버전에는 implementation이 없어서 compile을 사용했고, 새로운 버전(6.x 이후 버전)에서 compile이 deprecated 되면서 compile 대신에 api 키워드가 사용되게 되었다. 이 때문에 오래된 프로젝트들의 build.gradle 파일들을 보면 api와 compile을 implementation 대신 쓰는 것을 볼 수 있다.
즉, api와 compile은 같은 역할을 한다
공식문서2
implementation
Implementation only dependencies.
compileOnly
Compile time only dependencies, not used at runtime.
compileClasspath
extends compileOnly, implementation
Compile classpath, used when compiling source. Used by task compileJava.
annotationProcessor
Annotation processors used during compilation.
runtimeOnly
Runtime only dependencies.
runtimeClasspath
extends runtimeOnly, implementation
Runtime classpath contains elements of the implementation, as well as runtime only elements.
testImplementation
extends implementation
Implementation only dependencies for tests.
testCompileOnly
Additional dependencies only for compiling tests, not used at runtime.
testCompileClasspath
extends testCompileOnly, testImplementation
Test compile classpath, used when compiling test sources. Used by task compileTestJava.
testRuntimeOnly
extends runtimeOnly
Runtime only dependencies for running tests.
testRuntimeClasspath
extends testRuntimeOnly, testImplementation
Runtime classpath for running tests. Used by task test.