프로젝트가 복잡해지고 모듈화되면서, 반복되는 dependencies
를 어떻게 처리할지에 대한 얘기가 나오기 시작했습니다.
그중, dependencies
라이브러리 아티팩트를 BuildSrc를 통해 관리하는 방법이 대세가 되긴했습니다.
다만 아래와 같은 문제 때문에 꺼리는 사람도 존재했습니다.
첫번째 문제점은 Plugin과 Third-Party Library를 통해 해결이 가능했습니다만,
두번쨰 문제점은 BuildSrc를 사용하면, const로 사용하는 상수들이 inline 형태로 동작하기때문에 모든 프로젝트 코드를 rebuild 해야합니다.
(플러그인을 하나 수정했음에도 전부 rebuild 시켜버린다.)
A change in buildSrc causes the whole project to become out-of-date. Thus, when making small incremental changes, the --no-rebuild command-line option is often helpful to get faster feedback. Remember to run a full build regularly or at least when you’re done, though.
BuildSrc의 변경은 모든 프로젝트를 구식으로 만들어버린다.
이것을 피하려면 --no-rebuild 를 종종 사용해줘야한다.
버전의 작은 수정에도 프로젝트를 증분이나 빌드캐시없이 rebuild
하기에 기존의 방식보다 거의 3배 가량 높았기 때문에 고민이 많았었는데 , 드디어 Gradle 7.0 버전부터 Version Catalog
를 통해 해결이 되었습니다.
A version catalog is a list of dependencies, represented as dependency coordinates, that a user can pick from when declaring dependencies in a build script.
버전 카탈로그는 사용자가 빌드 스크립트에서 종속성을 선언할 때 선택할 수 있는 종속성 목록입니다.
버전 카탈로그에 SettingGradle
에 선언되어 Bundle
처럼 작동합니다.
또한 BuildSrc와 다르게 함수 호출의 형태이기 때문에 버전 변경이 일어난다고 해서 리빌드를 하지않습니다.
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
library("groovy-core", "org.codehaus.groovy:groovy:3.0.5")
library("groovy-json", "org.codehaus.groovy:groovy-json:3.0.5")
library("groovy-nio", "org.codehaus.groovy:groovy-nio:3.0.5")
library("commons-lang3", "org.apache.commons", "commons-lang3").version {
strictly("[3.8, 4.0[")
prefer("3.9")
}
}
}
}
해당 Library
를 등록하면 모듈 어디에서든, libs
라는 안정 접근자 안에서 자동완성이 가능합니다.
dependencies {
//자동 완성이 가능하다.
implementation(libs.groovy.core)
implementation(libs.groovy.json)
implementation(libs.groovy.nio)
}
Vesion.toml
파일을 통해, 아티팩트를 관리할수도 있습니다.
TOML 파일은 총 4가지 섹션으로 나뉘는데요.
[versions]섹션은 종속성이 참조할 수 있는 버전을 선언하는 데 사용됩니다
[libraries]섹션은 별칭을 선언하는 데 사용됩니다
[bundles]섹션은 종속성 번들을 선언하는 데 사용됩니다
[plugins]섹션은 플러그인을 선언하는 데 사용 됩니다
아래와 같이 파일을 작성하면 자동으로 카탈로그가 등록됩니다.
[versions]
groovy = "3.0.5"
checkstyle = "8.37"
[libraries]
groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } }
[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]
[plugins]
jmh = { id = "me.champeau.jmh", version = "0.6.5" }
여기서group
과 name
은 함께 써야하며, org.apache.commons:commons-lang3
일경우
이런식으로 라이브러리를 작성하면 됩니다.
gradle에 Version.toml를 작성할 경우, 알아서 gradle이 인식합니다.
만약 다른 위치에 있을경우, 아래와 같이 사용하여 위치와 libs
였던 안정 접근자 명칭을 변경이 가능합니다.
versionCatalogs {
//lis
create(woosung) {
files("파일의 위치")
}
}
Version- Catalog Update라는 서드파티 플러그인를 통해 업데이트가 가능합니다.
해당 플러그인을 통해 선언되어있는 아티팩트를 Version.toml으로 옮기는것도 가능하며, 옵션을 통해 참조된 라이브러리를 업데이트 시키지 않는것도 가능합니다.
읽어주셔서 감사합니다 <3