Gradle dependency 관련해서 검색을 하다보면,
어떤 글에서는 implementation을 사용하고 어떤 글에서는 compile을 사용하는 경우가 있다.
사실 어떻게 사용해도 돌아가긴 해서, 음... 무슨 차이지?하고 알아보기로 했다.
공식 문서에는 꽤 간단하게 설명되어 있지만, 정말 바로 알아들을 수 없..었..다...
(추가적으로 댓글에 정말 정말 좋은 내용을 남겨주셨다! api에 관해 더 궁금하신 분들은 해당 댓글을 확인해주시면 좋을 것 같다!!)
The dependencies required to compile the production source of the project which are part of the API exposed by the project. For example the project uses Guava and exposes public interfaces with Guava classes in their method signatures.
The dependencies required to compile the production source of the project which are not part of the API exposed by the project. For example the project uses Hibernate for its internal persistence layer implementation.
음.. 그래서 그림을 그려 보았다!
아래에 열심히 (본인이) 수작업한 그림을 보자.
A를 의존하고 있는 모듈이 B, C라고 가정하자. 또한 모듈의 구조는 Example of modules와 같다고 가정하자.
A라는 모듈을 수정하게 되면, 이 모듈을 직접 혹은 간접 의존하고 있는 B와 C는 모두 재빌드 되어야 한다.
A라는 모듈을 수정하게 되면, 이 모듈을 직접 의존하고 있는 B만 재빌드한다.
* 맥락에서 재빌드(rebuild)라는 표현을 썼지만 recompile이랑 동일한 표현으로 사용되었다.
그런데, Gradle 3.0부터는 Compile이 deprecated되었다고 한다.
(참고로 현재 버전이 4.8.1부터 6.2까지 documentation이 제공되고 있다!!!)
(아래 '중요 참고' 내용을 꼭 참고하시길!!)
1. 빠르다.
이건 사실 당연하다.
구조만 봐도 연결된 dependency가 확 줄어들고, change가 발생하더라도 recompile을 적게 하니 소요 시간도 적다.
2. API의 노출
API의 노출이 무슨 상관이지? 라고 생각할 수 있는데,
Design Pattern에서 흔히 강조하듯, Transparency는 중요하다.
User가 이상한 행동(?)을 하지 않게 필요한 API만 노출하는 것이 중요하고,
또한 User 입장에서도 간편하게 사용할 수 있기 때문에 잘 관리하는 것이 중요하다.(Facade 패턴이 그래서..!)
그런데, compile을 사용하게 되면 연결된 모든 모듈의 API가 exposed(노출)된다고 한다.
솔직히 반길 일은 아니라고 할 수 있다.
implementation을 사용하게 되면 이런 일이 없어진다.
compile 키워드가 deprecated되었다고 하더라도,
compile과 같은 방식으로 작동했으면 할 수 있지 않은가?
그래서 'api'라는 키워드를 사용하여 똑같이 사용할 수 있다!
(이와 관련된 내용은 아래 댓글에 더 세세하게 정리해주신 분이 있다! 👍)
compile | implementation |
testCompile | testImplmentation |
debugCompile | debugImplementation |
androidTestCompile | androidTestImplementation |
1. Gradle 공식 문서
Managing Dependencies of JVM Projects
How does Gradle know where to find the files for external dependencies? Gradle looks for them in a repository. A repository is a collection of modules, organized by group, name and version. Gradle understands different repository types, such as Maven and I
docs.gradle.org
2. StackOverFlow
What's the difference between implementation and compile in Gradle?
After updating to Android Studio 3.0 and creating a new project, I noticed that in build.gradle there is a new way to add new dependencies instead of compile there is implementation and instead of
stackoverflow.com
@Transactional 파헤치기 (0) | 2022.02.15 |
---|---|
Jib란? (+ M1 Mac에서 빌드하기 & Kotlin DSL) (0) | 2021.09.06 |
[Java][Maven][IntelliJ] java.lang.NoClassDefFoundError (0) | 2020.04.23 |
[Gradle] implementation vs compile (4) | 2020.02.25 |
스프링 부트와 AWS로 혼자 구현하는 웹서비스 문제 해결 (2) | 2020.02.19 |
댓글 영역
이해가 쏙쏙됐습니다 감사합니다.
본문에서 api와 compile이 동일한 것이라고 설명하셨습니다. 이 두개가 동일하다는 것은 어디에서 알게되신건가요?
Java Gradle 공식 문서에서 동일하다는 내용을 찾기 힘들어 질문드립니다.
혹시 저처럼 혼란스러워 하시는 분들을 위해,, 제가 조사한 바를 공유 드립니다. 잘못 된 부분 있으면 알려주세요!
- api, implementation의 구분은 Gradle 3.4에서 처음 생긴 java-library plugin 에 있음
-- apply plugin: 'java-library' 해야 쓸 수 있고
-- apply plugin: 'java' 하면 api 못씀 (Could not find method api() 에러 발생)
- Android Plugin 에서는 3.0부터 구분 됨 (https://developer.android.com/studio/build?utm_source=android-studio#new_configurations)
- 공식문서 (https://docs.gradle.org/6.8.1/userguide/building_java_projects.html) 에서는 compile이 implementation이다, 혹은 compile이 api 이다 라는 명시적인 내용 없음
-- implementation (supersedes compile) <- implementation이 compile을 대체한다.
-- compile ... is now deprecated ... because it doesn’t distinguish between dependencies that impact the public API of a Java library project and those that don’t. <- compile 쓰지마라. 공개할 API랑 implementation이랑 구분을 하지 않는다!
-- https://youtu.be/7ll-rkLCtyk?t=1808 에서 발표자가 "Now, API is basically the equivalent of the old compile" 이라고 말함. 근데 이건 Android Gradle임.
먼저, 첫 번째 링크는 간략하게 JVM 프로젝트들의 종속성 관리의 예제로 Java Library Plugin을 예시로 들며, api와 implementation에 대해 적혀 있습니다.
두 번째 링크는 위에서 적어주신 것과 같이 compile이 deprecated 되었으며 api와 implementation을 선택적으로 사용하되, implementation을 권장한다는 내용을 담고 있습니다. 또한 api 키워드에 대한 설명이 나와있는데요, api 키워드가 작동하는 방식에 대한 서술을 보면 사실상 compile 방식과 동일함을 알 수 있습니다. 꼭!! 2번째 링크에서 api 키워드에 관한 내용을 읽어보시는 걸 권장합니다.
말씀하신 것처럼 plugin에 따라 종속성 관리의 방식이 달라질 수 있습니다! 좋은 댓글 감사합니다.
1. https://bit.ly/36jfDM3
2. https://bit.ly/3cmDlLh