[Seatflow] 02-1. MSA Gradle 구조 문제와 전환 과정

Lab/SeatFlow · 2026-06-18

Intro

02편에서 Gradle 멀티모듈 구조를 잡고 6개 서비스를 띄우는 데까지 성공했다. 그런데 구조를 고도화하고 공통모듈들을 따로 만드는 과정에서 빌드가 깨지는 문제가 계속 생겼다. 기존엔 subproject.filter {}로 공통 설정을 주입하는 방식과, 라이브러리 모듈에서 boot 플러그인으로 BOM을 끌어오는 방식을 사용했는데, 오류가 생기면서 찾아보니 이 자체가 Gradle이 권장하지 않는 방식이었다. 그래서 해당 부분을 수정하고 전환하면서 어떻게 바뀌었는지 다뤄보려고 한다.


왜 문제가 생겼을까?

02편에서 쓴 구조는 이랬다.

// 루트 build.gradle.kts
configure(subprojects.filter { it.parent?.name == "services" }) {
    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
    // 공통 의존성...
}

Cross-project Configuration

subprojects.filter { } 로 다른 프로젝트에 설정을 주입하는 방식을 Cross-project Configuration이라고 한다. Gradle이 공식적으로 권장하지 않는 방식이다. 이유는 세 가지다.

  • 프로젝트 격리(Project Isolation) 위반 — 루트가 다른 서브프로젝트에 접근하므로 프로젝트 간 격리가 깨지고, 이는 Configuration Cache 및 향후 최적화(Isolated Projects)와 호환되지 않는다.
  • 병렬 빌드 방해 — 루트가 서브프로젝트 설정 완료를 기다려야 해서 병렬화가 깨진다.
  • 빌드 로직 분산 — 어떤 모듈에 어떤 설정이 적용되는지 루트를 봐야만 알 수 있다.

BOM이 숨겨져 있었다

블로그 구조에서 libs/common-web 같은 라이브러리 모듈에는 두 가지 선택지가 있었다.

방법 1 (02편에서 선택한 방식):

// libs/common-web/build.gradle.kts
plugins {
    id("org.springframework.boot")          // <- boot 플러그인이 BOM을 자동으로 끌어옴
    id("io.spring.dependency-management")
}
tasks.withType<BootJar> { enabled = false } // <- 실행 가능 JAR 비활성화 우회코드
tasks.withType<Jar> { enabled = true }

방법 2:

dependencyManagement {
    imports {
        mavenBom("org.springframework.boot:spring-boot-dependencies:3.5.13") // <- 버전 하드코딩
    }
}

방법 1은 라이브러리인데 boot 플러그인을 달아야 한다는 게 의미적으로 잘못됐다. 방법 2는 버전을 직접 하드코딩해야 해서 루트에서 Spring Boot 버전을 올려도 여기를 따로 바꿔야 한다.

두 방법 모두 타협이다.

payment-service가 convention plugin으로 전환 안 됨

에러가 난 실제 원인이다.

Unresolved reference: implementation

payment-service/build.gradle.kts가 아직 이 상태였다:

plugins {
    id("org.springframework.boot")           // <- 루트에서 apply false로 선언된 것
    id("io.spring.dependency-management")    // <- 이것도 마찬가지
}
dependencies {
    implementation(...)  // <- 이건 맞음
}

implementation은 java 플러그인이 적용돼야 생기는 함수다. 이전 구조에서는 루트의 Cross-project Configuration이 java 플러그인을 밀어 넣어줬기 때문에 동작했다. 그 블록을 걷어내는 순간 java 플러그인을 적용해주는 주체가 사라지고, implementation은 존재하지 않는 함수가 됐다.

Cross-project Configuration이 걷어내진 상태에서 이 플러그인들이 더 이상 아무것도 제공하지 않으니 implementation 같은 함수 자체를 못 찾아서 컴파일 에러가 난 것이다.


새로운 구조

build-logic Convention Plugin

Convention Plugin을 어디에 둘지가 첫 번째 선택지였다. Gradle이 제공하는 방법은 두 가지다.

buildSrc

루트 바로 아래에 두면 Gradle이 자동으로 인식하고 먼저 컴파일한다. 별도 설정 없이 바로 쓸 수 있어서 시작이 간단하다.

build-logic

settings.gradle.kts에 includeBuild("build-logic")으로 등록하는 독립된 Gradle 프로젝트다. Gradle 공식 문서(Best Practices for Structuring Builds)는 build-logic을 명시적으로 권장한다.

buildSrc는 파일 하나만 수정해도 전체 프로젝트 캐시가 무효화되고, 클래스로더가 분리되지 않아 의존성이 메인 빌드에 그대로 노출되는 단점이 있다. build-logic은 독립된 빌드로 격리되어 이 문제가 없다. build-logic 디렉토리는 buildSrc와 구조가 동일하다. 차이는 Gradle이 자동으로 인식하느냐, settings.gradle.kts에 직접 등록하느냐뿐이다.

seatflow-msa/
ㄴ build-logic/
   ㄴ settings.gradle.kts                 <- build-logic 자체의 프로젝트 선언
   ㄴ build.gradle.kts                    <- build-logic 자체의 빌드 설정
   ㄴ src/main/kotlin/
       ㄴ seatflow.java-base-conventions.gradle.kts   <- 모든 모듈 공통
       ㄴ seatflow.library-conventions.gradle.kts     <- 라이브러리용
       ㄴ seatflow.application-conventions.gradle.kts <- 서비스용

Gradle은 빌드를 시작할 때 build-logic을 먼저 독립적으로 컴파일한다. 컴파일된 결과물은 루트 프로젝트와 모든 서브 모듈의 클래스패스에 올라간다. 그래서 seatflow.library-conventions처럼 이름을 붙인 파일이 버전 없이 플러그인 id로 바로 참조되는 것이다. Cross-project Configuration은 루트가 서브프로젝트를 알아야 했지만, build-logic은 반대다. 각 모듈이 필요한 규약을 직접 선언하고, build-logic은 그 규약이 무엇인지만 정의한다.

위에 설명 했듯이 build-logic은 그 자체로 독립된 Gradle 빌드라, 자기 settings와 build 파일을 가진다. 특히 kotlin-dsl 플러그인이 핵심이다. 이게 빠지면 convention plugin이 생성되지 않아 Plugin 'seatflow.library-conventions' not found 에러가 난다.

// build-logic/settings.gradle.kts
rootProject.name = "build-logic"
// build-logic/build.gradle.kts
plugins {
    `kotlin-dsl`   // 이게 빠지면 convention plugin이 생성되지 않는다
}

repositories {
    mavenCentral()
    gradlePluginPortal()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-gradle-plugin:3.5.13")
    implementation("io.spring.gradle:dependency-management-plugin:1.1.7")
}

세 가지 Convention Plugin 역할

seatflow.java-base-conventions  모든 모듈의 기반

import org.springframework.boot.gradle.plugin.SpringBootPlugin

plugins {
    java
    id("io.spring.dependency-management")
}

java {
    toolchain { languageVersion = JavaLanguageVersion.of(21) }
}

// 핵심: BOM을 명시적으로 import
// boot 플러그인 없이도 버전 관리가 된다
dependencyManagement {
    imports {
        mavenBom(SpringBootPlugin.BOM_COORDINATES) // build-logic에 선언된 3.5.13과 자동 일치
    }
}

dependencies {
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    // ...
}

SpringBootPlugin.BOM_COORDINATES는 buildSrc/build.gradle.kts에 선언한 spring-boot-gradle-plugin:3.5.13 버전과 자동으로 연동된다. 버전을 한 곳(buildSrc/build.gradle.kts)에서만 바꾸면 전체 반영된다.

seatflow.library-conventions 라이브러리 전용

plugins {
    `java-library`
    id("seatflow.java-base-conventions")
}

java-library가 핵심이다. api() / implementation() 구분이 가능해진다.

  • api() — 이 라이브러리를 의존하는 모듈에도 전파됨
  • implementation() — 이 라이브러리 내부에서만 씀

boot 플러그인이 없으므로 bootJar도 없고, 비활성화 우회코드도 없다.

seatflow.application-conventions 서비스 전용

import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
    id("seatflow.java-base-conventions")
    id("org.springframework.boot")     // 서비스에만 boot 플러그인
}

tasks.withType<BootJar> {
    layered { enabled = true }         // 3.5에선 기본값이지만 의도를 명시
}

실행 가능한 JAR가 필요한 서비스에만 boot 플러그인이 붙는다.


각 모듈들의 변경

서비스 모듈 (before -> after)

이전:

// services/payment-service/build.gradle.kts
plugins {
    id("org.springframework.boot")
    id("io.spring.dependency-management")
}
dependencies { ... }

이후:

// services/payment-service/build.gradle.kts
plugins {
    id("seatflow.application-conventions")  // 한 줄로 끝
}
dependencies { ... }

라이브러리 모듈 (before -> after)

이전:

// libs/common-web/build.gradle.kts
plugins {
    id("org.springframework.boot")           // 라이브러리인데 boot 플러그인
    id("io.spring.dependency-management")
}
tasks.withType<BootJar> { enabled = false }  // 우회코드
tasks.withType<Jar> { enabled = true }
dependencies { ... }

이후:

// libs/common-web/build.gradle.kts
plugins {
    id("seatflow.library-conventions")  // 한 줄로 끝, 우회코드 없음
}
dependencies { ... }

새 common 모듈 추가하는 법

새 공통 라이브러리가 필요할 때 두 가지만 하면 된다.

 settings.gradle.kts에 모듈 선언

include(
    "libs:common-web",
    "libs:common-events",
    "libs:common-kafka",
    "libs:common-redis",
    "libs:common-outbox",
    "libs:common-xxx",       // <- 새로 추가할 모듈
    "services:auth-service",
    // ...
)

디렉토리와 build.gradle.kts 생성

libs/
ㄴ common-xxx/
    ㄴ build.gradle.kts
    ㄴ src/main/java/com/seatflow/common/xxx/
// libs/common-xxx/build.gradle.kts
plugins {
    id("seatflow.library-conventions")   // 이게 전부
}

dependencies {
 // 필요한 모듈들
}

이게 전부다. Java toolchain, Lombok, 테스트 설정, BOM은 모두 자동 상속된다.

api() vs implementation() 언제 쓰나

상황사용
이 라이브러리의 타입이 공개 API에 노출됨api()
내부 구현에서만 쓰고 외부에 노출 안 됨implementation()
이 모듈을 쓰는 서비스도 해당 의존성이 필요함api()

 전체 구조 한눈에 보기

seatflow-msa/
ㄴ settings.gradle.kts          모듈 목록 + 리포지토리 중앙화 + includeBuild("build-logic")
ㄴ build.gradle.kts             group/version만 (빌드 로직 없음)

ㄴ build-logic/
   ㄴ settings.gradle.kts      rootProject.name = "build-logic"
   ㄴ build.gradle.kts         Spring Boot 3.5.13, dependency-management 버전 선언
   ㄴ src/main/kotlin/
       ㄴ seatflow.java-base-conventions.gradle.kts
          → java, toolchain(21), BOM import, lombok, test
       ㄴ seatflow.library-conventions.gradle.kts
          → java-library + base
       ㄴ seatflow.application-conventions.gradle.kts
           → base + org.springframework.boot + layered bootJar

ㄴ libs/
   ㄴ common-web/              library-conventions + web starter
   ㄴ common-events/           library-conventions + 이벤트 DTO
   ㄴ common-kafka/            library-conventions + spring-kafka
   ㄴ common-redis/            library-conventions + data-redis
   ㄴ common-outbox/           library-conventions + jpa + common-kafka

ㄴ services/
    ㄴ auth-service/            application-conventions + 서비스별 의존성
    ㄴ user-service/
    ㄴ reservation-service/
    ㄴ seat-service/
    ㄴ payment-service/
    ㄴ show-service/
// settings.gradle.kts (루트)
rootProject.name = "seatflow-msa"

includeBuild("build-logic")   // 이 줄이 없으면 build-logic이 인식되지 않는다

dependencyResolutionManagement {
    repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
    repositories { mavenCentral() }
}

include(
    "libs:common-web",
    // ... 나머지 모듈
)

의존성 흐름

services (application-conventions)
    ㄴ libs (library-conventions)
            ㄴ Spring BOM (java-base-conventions에서 import)
                    ㄴ Spring Boot 3.5.13 (buildSrc/build.gradle.kts)

버전은 build-logic/build.gradle.kts 한 곳에서만 관리된다.


새 서비스 추가 시:

  • settings.gradle.kts에 "services:new-service" 추가
  • services/new-service/build.gradle.kts에 id("seatflow.application-conventions") 선언
  • 필요한 의존성만 dependencies { } 블록에 추가
  • @SpringBootApplication 진입점 클래스 생성

새 라이브러리 추가 시:

  • settings.gradle.kts에 "libs:common-xxx" 추가
  • libs/common-xxx/build.gradle.kts에 id("seatflow.library-conventions") 선언
  • 공개 API는 api(), 내부 구현은 implementation()으로 의존성 선언