Spring Boot Mono Repo with Multi Module (MSA)

송형근·2024년 9월 6일
0

TIL

목록 보기
31/43
post-thumbnail

MSA 프로젝트 환경세팅을 하던 중 Multi Repo와 Mono Repo 중 어떤 방식을 채택할 지 고민을 하다가 Multi Module을 활용한 Mono Repo 구성을 해보는게 어떠냐는 조언을 듣고 프로젝트에 한번 적용해보기로 했음

Root Project build.gradle 최초 작성 예시

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.3'
    id 'io.spring.dependency-management' version '1.1.6'
}

repositories {
    mavenCentral()
}

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

    group = 'com.sparta3tm'
    version = '1.0-SNAPSHOT'

    repositories {
        mavenCentral()
    }

    ext {
        set('springCloudVersion', "2023.0.3")
    }

    dependencies {
        testImplementation platform('org.junit:junit-bom:5.10.0')
        testImplementation 'org.junit.jupiter:junit-jupiter'
    }

    test {
        useJUnitPlatform()
    }

}
  • MSA 프로젝트다 보니 각 프로젝트에 공통적으로 적용되는 dependencies가 적음

Submodule 생성 ( 공통 모듈 )

  • New - Module
  • Module 생성
  • 공통 모듈의 경우 각 프로젝트에서 공통으로 자주 쓰이는 부분만 작성할 예정이므로 build.gradle 외의 기본 생성 파일을 삭제
  • build.gradle에 공통 모듈에서 사용할 의존성 추가
    dependencies {
    
        // lombok
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    
        // DB
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    
        // zipkin
        implementation 'io.micrometer:micrometer-tracing-bridge-brave'
        implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
    
        // prometheus
        runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
    
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-web'
    
        implementation 'org.springframework.boot:spring-boot-starter'
    
    }
    
  • config, handler, auditing 등 프로젝트들에 필요한 class들 작성

Root Project settings.gradle 설정 예시

rootProject.name = '3TM'
include 'common'
  • include를 통해 RootProject의 SubModule로 공통 모듈을 추가

Submodule 생성 ( API 서버 )

  • 생성 예시는 생략

  • settings.gradle 삭제 ( rootProject가 아님 )

  • build.gradle 작성 (공통 모듈에 들어가는 부분 제외하고 설정)

    dependencies {
        // Swagger
        implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.2.0'
    
        // Lombok
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    
        // DB
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        runtimeOnly 'org.postgresql:postgresql'
        implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    
        // Eureka
        implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    
        // OpenFeign
        implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    
        // Zipkin
        implementation 'io.github.openfeign:feign-micrometer'
        implementation 'io.micrometer:micrometer-tracing-bridge-brave'
        implementation 'io.zipkin.reporter2:zipkin-reporter-brave'
    
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
    }
    
    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }
    

Root Project settings.gradle, build.gradle 수정

  • settings.gradle
    rootProject.name = '3TM'
    include 'common'
    include 'company-server'
  • build.gradle
    // 기존 build.gradle 아래 부분에 추가
    
    project(':company-server'){
        dependencies {
            implementation project(':common')
        }
    }

Gradle Reload

  • Root Project의 Submodule로 common과 company-server가 들어 있는 모습 확인

P.S.

  • 만들다 보니 Eureka Server대신 API Server를 먼저 만들어둔 상황이라, Eureka Server 부터 빠르게 만들 예정
profile
기록을 남겨보자

2개의 댓글

comment-user-thumbnail
2025년 5월 2일

루트 subprojects 에서 plugins 사용하고 서브프로젝트의 build.gradle 에서 plugins 사용하면 build.gradle 의 plugins 이 우선돼 subprojects 의 구문이 전부 무시됩니다 ㅠ

1개의 답글