Skip to content

[Gradle] compile vs compileOnly

kimhanui edited this page Nov 17, 2020 · 1 revision

2.3 롬복 소개 및 설치하기 단계에서..

롬복 플러그인을 설치하기 위해
build.gradle의 dependency에 compile('org.projectlombok:lombok')을 추가해야된다.

그런데 나는 이미 compileOnly로 시작하는 롬복 설치 코드가 있었다.
처음 프로젝트 생성할 때 파일 구조를 하나하나 만들어가며 익히는 책의 의도가 나한텐 좀 지루해서 바로 spring initializer + Gradle Project로 생성했었는데 중간에 같이 설치할 lib를 고르라해서 롬복도 넣었더니 비스무리한 코드가 있었다.

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compileOnly 'org.projectlombok:lombok'       <-- 여기
    testCompile('org.springframework.boot:spring-boot-starter-test')
    
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

compile vs compileOnly

https://developer.android.com/studio/build/dependencies?hl=ko (한글판 doc)

그런데 왜 하나는 compile로 시작하고 다른건 compileOnly로 시작할까...? 똑같이 쓰면 안되나???

궁금해져서 찾아봤다.
(gradle, maven 둘다 문법?을 익히긴 좀 어려운 것 같다. 직접 찾아보지 않는 이상 알 기회가 별로 없는 것 같다;;)

compile

컴파일은 말 그대로 컴파일 할 때 쓰는 것 같은데 아래 주제에서 알았지만 나중에 api, implementation으로 나뉘었다.

compileOnly

This is where you should declare dependencies which are only required at compile time,
but should not leak into the runtime. This typically includes dependencies which are shaded when found at runtime.

컴파일시에만 필요하고 런타임시에는 쓰지 않는 종속성... 여기에는 일반적으로 런타임에 발견될 때
shadow 처리되는 종속성이 포함된다..고 함.

implementation vs compile vs api도 찾았다

https://hack-jam.tistory.com/13 https://jjhwqqq.tistory.com/231

Gradle 3.0부터 기존에 사용하던 compile -> api, implementation으로 나뉘어졌다.

  • api: 모듈의 dependency를 compile시점부터 공유.
    • A(api) <- B <- C 일 때, C 에서 A 를 접근할 수 있음
    • A 수정시 B 와 C 모두 재빌드
  • implementation: runtime시점부터 공유.
    • A(implementation) <- B <- C 일 때, C 에서 A 를 접근할 수 없음
    • A 수정시 B 까지 재빌드

java에서 api,implementation을 사용하려면 apply plugin:'java-library'를 추가해야한다는데
java-library대신 java는 있는데,,, 이거라도 써서 쓸 수 있는건가,..??

정리

대강 이해한 바로는.... configuration을 담당하는 build.gradle에선 java lib plugin을 관리할 때 아래와 같은 키워드를 쓴다.

https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph

  • api - 참조하고있던 의존성이 수정되면 직접+간접 참조하던 의존성까지 재빌드됨
  • implementation(+test) - 참조하고있던 의존성이 수정되면 직접 참조하던 의존성만 재빌드됨
  • compileOnly(+test) - 컴파일시에만 사용되는 dependency
  • runtimeOnly(+test) - 런타임시에만