SpringBoot/Boot

[Gradle] 외부 파일로 Dependency 버전 관리 하기

younsik 2022. 4. 14. 16:25

일반적으로 spring boot를 사용하는 경우 io.spring.dependency-management 플러그인으로 dependency 관리를 한다

 

일부 몇몇 외부 라이브러리 사용하는 경우 다음과 같이 build.gradle 내에서 buildscript에 선언하여 version을 명시할 수 있다

// build.gralde

buildscript {
    ext {
        resilience4jVersion = '1.7.1'
    }
}

 

 

그러나 가끔 멀티 모듈을 사용할 경우 중복되는 라이브러리들의 버전은 별도로 관리하고 싶은 경우가 있다

 

방법 1. 별도 gradle 파일 이용

// versions.gradle 파일을 생성하고 다음과 같이 작성한다

ext.versions = [:]

versions.spring_boot = '2.6.6'
versoins.spring_cloud_deps = '2021.01'
versions.resilience4j = '1.7.1'

ext.versions = versions
// build.gradle에서 versions.gradle 사용

buildscript {
    apply from: "${rootProject.getRootDir()}/versoins.gradle"
    
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$versions.spring_boot"
    }
}

subprojects {

    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'java-library'

    sourceCompatibility = '11'
    
    dependencies {
        implementation "io.github.resilience4j:resilience4j-spring-boot2:$versions.resilience4j"
    }
}

방법 2. toml 사용

// libs.version.toml 작성

[versions]
springBoot = "2.6.6"
bootDependency = "1.0.11.RELEASE"
springCloud = "2021.0.1"

[libraries]
# bundles boot-starter
boot-starter = {module = "org.springframework.boot:spring-boot-starter"}

# bundles boot-tester
boot-test = {module = "org.springframework.boot:spring-boot-starter-test"}

# bundles jackson
jackson-databind = {module = "com.fasterxml.jackson.core:jackson-databind"}
jackson-jsr310 = {module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"}

# bundles common
commons-codec = { module = "commons-codec:commons-codec", version = "1.15" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = "3.12.0" }

# libs
fusionauth-jwt = { group = "io.fusionauth", name = "fusionauth-jwt", version = "4.3.1" }

[bundles]
boot-starter = ["boot-starter"]
jackson = ["jackson-databind", "jackson-jsr310"]
common = ["commons-lang3", "commons-codec"]
boot-test = ["boot-test"]

[plugins]
spring-boot = { id = "org.springframework.boot", version.ref = "springBoot" }
boot-dependency = { id = "io.spring.dependency-management", version.ref = "bootDependency" }
// settings.gradle 다음 내용 추가

dependencyResolutionManagement {
    versionCatalogs {
        libs {
            from(files("${rootDir}/libs.versions.toml"))
        }
    }
}
// build.gradle 작성 (rootDir)

plugins {
    id 'java-library'
    alias(libs.plugins.spring.boot) apply false
    alias(libs.plugins.boot.dependency)
}

subprojects {

    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'java-library'

    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11

    repositories {
        mavenCentral()
    }

    dependencies {
        
        implementation libs.bundles.boot.starter

        // jackson
        implementation libs.bundles.jackson

        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'

        // test
        testImplementation libs.bundles.boot.test
    }
}
// build.gradle 작성 (module)

dependencies {

    //common
    implementation libs.bundles.common

    // libs
    implementation libs.fusionauth.jwt
}