๐Ÿ˜Gradle๊ณผ ๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ ์„ค๊ณ„(2)

Big0ยท2023๋…„ 6์›” 22์ผ
0

๋ชจ๋“ˆ์ด๋ž€?

  • ์‚ฌ์ „๋งˆ๋‹ค ๊ฑฐ์˜ ์žฌ๊ฐ๊ฐ์œผ๋กœ ์„ค๋ช…์ด ๋˜์–ด์žˆ์ง€๋งŒ, ๊ณตํ†ต์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ๋‚ด์šฉ์œผ๋กœ ์ •๋ฆฌํ–ˆ์„ ๋•Œ ๋ชจ๋“ˆ์ด๋ผ๋Š” ๊ฐœ๋…์€ย ๋…๋ฆฝ์ ์œผ๋กœ ์šด์˜๋  ์ˆ˜ ์žˆ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ๊ตฌ์„ฑ์š”์†Œ ๋‹จ์œ„
    • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋“ˆ์€ ๋…๋ฆฝ์ ์œผ๋กœ ์ปดํŒŒ์ผ, ํŒจํ‚ค์ง€, ํ…Œ์ŠคํŠธ ๋ฐ ๋ฐฐํฌ๋  ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„

๋ชจ๋“ˆ์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋กœ ์ •์˜๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ํฐ ์ฒด๊ณ„์˜ ๊ตฌ์„ฑ์š”์†Œ์ด๊ณ , ๋‹ค๋ฅธ ๊ตฌ์„ฑ์š”์†Œ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์šด์˜๋œ๋‹ค.

์ถœ์ฒ˜: wikipedia

๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ์ด๋ž€?

  • ์˜ค๋ผํด ์ž๋ฐ” ๋ฌธ์„œ์—์„œ๋Š”, ๋ชจ๋“ˆ์„ ํŒจํ‚ค์ง€์˜ ํ•œ ๋‹จ๊ณ„ ์œ„์˜ ์ง‘ํ•ฉ์ฒด์ด๋ฉฐ, ๊ด€๋ จ๋œ ํŒจํ‚ค์ง€์™€ ๋ฆฌ์†Œ์Šค๋“ค์˜ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ฃน์ด๋ผ๊ณ  ์ •์˜

์ผ๋ฐ˜์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ผ ๋•Œ๋Š” ์ฝ”๋“œ์˜ ์ค‘๋ณต์ด ์ƒ๊ธฐ๊ฑฐ๋‚˜ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ด ์ƒ๊ฒผ์„ ๋•Œ๋‹ค.

  • ํšŒ์› ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์œ„์™€ ๊ฐ™์ด ์„ธ ๊ฐ€์ง€์˜ ๋…๋ฆฝ์ ์ธ ํ”„๋กœ์ ํŠธ๊ฐ€ ๋‚˜์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.
  • ์ด๋Ÿฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ์ ์€ ์‹œ์Šคํ…œ์˜ ์ค‘์‹ฌ Domain์ด ๊ฐ€์ ธ์•ผํ•  ๊ตฌ์กฐ์™€ ๊ทœ์น™์„ ๋™์ผํ•˜๊ฒŒ ๋ณด์žฅํ•ด์ฃผ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์—†๋‹ค.
  • ๊ฐœ๋ฐœ์ž๋Š” ๋™์ผํ•œ Member ๋„๋ฉ”์ธ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งค์šฐ ๊ท€์ฐฎ๊ณ  ๋ถˆ์•ˆํ•˜๊ฒŒ ๋ณต์‚ฌ&๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ํ•˜๋ฉฐ ๊ฐœ๋ฐœํ•  ๊ฒƒ์ด๋‹ค.

  • ๊ทธ๋ž˜์„œ ๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ์€ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ชจ์•„๋†“๊ณ  ๊ฐ™์ด ์Šฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ ์‚ฌ์šฉ

1. ๋ชจ๋“ˆ ์ƒ์„ฑ

  • ์˜ˆ์‹œ๋กœ ์‚ฌ์šฉํ•  ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ

  • multi-module-api: API ๊ด€๋ จ ๋ชจ๋“ˆ
  • multi-module-batch: Batch ๊ด€๋ จ ๋ชจ๋“ˆ
  • multi-module-database: multi-module-api์™€ multi-module-batch์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ(์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ํ˜„์žฌ ์˜ˆ์‹œ์—์„œ๋Š” ์—”ํ‹ฐํ‹ฐ)

2. settings.gradle ์„ค์ •

rootProject.name = 'multi-module-project'
include 'multi-module-database'
include 'multi-module-api'
include 'multi-module-batch'
  • rootProject์™€ ๊ทธ ํ•˜์œ„ ํ”„๋กœ์ ํŠธ๋ฅผ includeํ•ด์ค€๋‹ค.

3. rootProject gradle ์„ค์ •

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.9'
}

repositories {
    mavenCentral()
}

bootJar.enabled = false

// subprojects๋Š” settings.gradle์— include๋œ ํ”„๋กœ์ ํŠธ ์ „๋ถ€๋ฅผ ๊ด€๋ฆฌ
subprojects {
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'

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

    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }

    repositories {
        mavenCentral()
    }

    // ๊ด€๋ฆฌํ•˜๋Š” ๋ชจ๋“ˆ์˜ ๊ณตํ†ต dependencies
    dependencies {
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    }

    test {
        useJUnitPlatform()
    }
}

project(':multi-module-api') { // ์ปดํŒŒ์ผ ์‹œ multi-module-database ๋กœ๋”ฉ
    dependencies {
        compileOnly project(':multi-module-database')
    }
}

project(':multi-module-batch') { // ์ปดํŒŒ์ผ ์‹œ multi-module-database ๋กœ๋”ฉ
    dependencies {
        compileOnly project(':multi-module-database')
    }
}

project(':multi-module-database') { 
// multi-module-databse๋Š” bootJar๋กœ ํŒจํ‚ค์ง• ํ•  ํ•„์š” ์—†์Œ
    bootJar { enabled = false }
    jar { enabled = true }
}
  • rootProject์™€ ๊ทธ ํ•˜์œ„์— ์žˆ๋Š” multi-module-database ๋Š” jar ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ์ค‘์ ์ ์œผ๋กœ ๋ด์•ผํ•  ๊ฒƒ์€ subprojects์™€ project()๋‹ค.
    • subprojects๋Š” settings.gradle์— include๋œ ํ”„๋กœ์ ํŠธ ์ „๋ถ€๋ฅผ ๊ด€๋ฆฌ
      • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” SpringBoot์™€ Java์— ์˜์กด์„ฑ์„ ๋‘๊ณ  ์žˆ๊ธฐ์— ๊ด€๋ จ๋œ plugin์„ ๋“ฑ๋ก
    • project์˜ ๊ฒฝ์šฐ ํ•˜์œ„ ํ”„๋กœ์ ํŠธ๊ฐ„์˜ ์˜์กด์„ฑ์„ ๊ด€๋ฆฌ
      • ์œ„ ์˜ˆ์‹œ์—์„œ๋Š” module-api์™€ module-web์€ ๋ชจ๋‘ module-common์— ์˜์กดํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋“ฑ๋ก

์ด์ „์— ์ถ”๊ฐ€ํ–ˆ๋˜ multi-module-database ์— ๋Œ€ํ•œ ์˜์กด์„ฑ์ด multi-module-api์™€ multi-module-batch์— ์ž˜ ์ฃผ์ž…๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

4. ํ•˜์œ„ ํ”„๋กœ์ ํŠธ gradle ์„ค์ •

//multi-module-api
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation project(':multi-module-database')
}

//multi-module-batch
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    testImplementation 'org.springframework.batch:spring-batch-test'
    implementation project(':multi-module-database')
}

//multi-module-database
dependencies {
    runtimeOnly 'com.h2database:h2'
		implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}

5. ํ…Œ์ŠคํŠธ

  • ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์œ„ ์„ค์ •๋“ค์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ
  • Controller๋Š” multi-module-api์— ์กด์žฌํ•œ๋‹ค.
  • Entity๋Š” multi-module-database์— ์กด์žฌํ•œ๋‹ค.

  • ์œ„์™€ ๊ฐ™์ด ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด, 200OK๊ฐ€ ๋œจ๋ฉด์„œ

  • DB์—๊นŒ์ง€ ์ž˜ ์ถ”๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค.

๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด,

1. ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

  • ๊ณตํ†ต๋œ ๋กœ์ง์ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋ฅผ ์šด์˜ํ•˜๊ฒŒ ๋˜์—ˆ์„ ๋•Œ, ๊ณตํ†ต๋œ ๋ถ€๋ถ„์„ ๋ชจ๋“ˆํ™”ํ•˜๊ณ , ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ๊ฐ ๋ชจ๋“ˆ์˜ ๊ธฐ๋Šฅ์„ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฌ์›Œ์ง„๋‹ค.

  • ๊ณตํ†ต์˜ ๊ธฐ๋Šฅ์€ ์˜์กด์„ฑ ์ฃผ์ž…์œผ๋กœ, ๋ชจ๋“ˆ๋ณ„๋กœ ๊ธฐ๋Šฅ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ฝ”๋“œ์˜ ์ดํ•ด๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค.

3. ๋นŒ๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ./gradlew :moduleName:build ์„ ํ†ตํ•ด ๋นŒ๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด์ „์˜ ๊ฒฝ์šฐ์—๋Š” ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ/ํ…Œ์ŠคํŠธ๋ฅผ ๋ณ„๋„๋กœ ๊ฐ๊ฐ ์ง„ํ–‰ํ–ˆ๋˜ ๋ฐ˜๋ฉด์—, ๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ์˜ ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ ๋นŒ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’ก์ฐธ๊ณ  ์ž๋ฃŒ๐Ÿ’ก
https://tecoble.techcourse.co.kr/post/2021-09-06-multi-module/
https://techblog.woowahan.com/2637/
https://jojoldu.tistory.com/123
https://dkswnkk.tistory.com/691

0๊ฐœ์˜ ๋Œ“๊ธ€