ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SpringBoot] IntelliJ ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ / ์ดˆ๊ธฐ ์„ค์ •(์ธํ…”๋ฆฌ์ œ์ด 2023Ver)
    ๐Ÿ’ปProgramming/SpringBoot 2023. 9. 17. 23:01

    ์Šคํ”„๋ง ์ด๋‹ˆ์…œ๋ผ์ด์ €(https://start.spring.io/)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ดˆ๊ธฐ ์„ธํŒ…์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค. ์ง€๊ธˆ ๊ณต๋ถ€ํ•˜๋ฉฐ ๊ธ€์„ ์ž‘์„ฑํ•˜๋Š” ์ฑ…(์Šคํ”„๋ง๋ถ€ํŠธ์™€ AWS๋กœ ํ˜ผ์ž ๊ตฌํ˜„ํ•˜๋Š” ์›น ์„œ๋น„์Šค)์€ ์Šคํ”„๋ง๋ถ€ํŠธ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„  build.gradle์— ํ•˜๋‚˜ํ•˜๋‚˜ ์ดˆ๊ธฐ ์„ธํŒ…์„ ํ•ด๊ฐ€๋ฉฐ ์Šคํ”„๋ง๋ถ€ํŠธ ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•ด ๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•˜๊ธฐ ๋•Œ๋ฌธ์— build.gradle์— ํ•˜๋‚˜ํ•˜๋‚˜ ์ž…๋ ฅํ•ด๊ฐ€๋ฉฐ ์ดˆ๊ธฐ setting์„ ํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. ์ด ์ฑ…์ด ์˜›๋‚  ์ฑ…์ด๋ผ ๊ทธ๋Ÿฐ์ง€ ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ gradle ๋ฒ„์ „๋„ ์˜›๋‚ ๊ฑฐ๋ผ ์ข€ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ์–ด์„œ ๊ทธ ๋ถ€๋ถ„๋“ค๋„ ์ˆ˜์ •ํ•ด๋ณด๊ฒ ๋‹ค.

    IntelliJ์—์„œ New Project ์ƒ์„ฑ

    ์–ธ์–ด๋Š” Java๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Java๋กœ ์„ค์ •ํ•ด์ค€๋‹ค. build system์œผ๋ก  gradle, Gradle DSL(Domain Specific Language)๋กœ๋Š” Groovy๋ฅผ ์ด์šฉํ•˜๋„๋ก ํ•œ๋‹ค. ์œ„์˜ ์„ธํŒ…์„ ํ•ด์ฃผ๊ณ  Create๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ž๋ฐ” ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๊ทธ ํ”„๋กœ์ ํŠธ ์•ˆ์˜ build.grale ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์ฝ”๋“œ๋Š” ์ด๋ ‡๋‹ค. ์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ ์ฝ”๋“œ๋งŒ ์ž‘์„ฑ๋˜์–ด ๊ธฐ๋ณธ์ ์ธ ๋ถ€๋ถ„๋งŒ ์„ค์ •๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค.

    plugins {
        id 'java'
    }
    
    group = 'org.example'
    version = '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation platform('org.junit:junit-bom:5.9.1')
        testImplementation 'org.junit.jupiter:junit-jupiter'
    }
    
    test {
        useJUnitPlatform()
    }

    ์œ„์˜ ์ฝ”๋“œ์˜ ๋งจ ์•ž์—

    buildscript {
        ext {
            springBootVersion = '2.1.7.RELEASE'
        }
        repositories {
            mavenCentral()
            jcenter()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

    springBootVersion๋ณ€์ˆ˜์— ์„ค์ •๋œ ๋ฌธ์ž์—ด์€ ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฒ„์ „์„ ์˜๋ฏธํ•˜๋Š”๋ฐ ๊ตณ์ด ์ € ๋ฒ„์ „์œผ๋กœ ์„ค์ •ํ•ด์ค„ ํ•„์š” ์—†๋‹ค. ์™œ๋ƒํ•˜๋ฉด ์ฑ…์ด ์˜›๋‚ ๋ฒ„์ „์ด๋ผ ์ตœ์‹ ๊ฑธ๋กœ ํ•ด๋„ ์ƒ๊ด€์—†๋‹ค.

    ext

    : ext ๋ธ”๋ก์€ build script์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ. springBootVersion์ด๋ž€ ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด์ฃผ๋Š” ์—ญํ• .

    repositories

    : build script์—์„œ ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๋‹ค์šด๋กœ๋“œํ•  ์ €์žฅ์†Œ(repository)๋ฅผ ์ •์˜. ์—ฌ๊ธฐ์„œ mavenCentral()๊ณผ jcenter() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Maven Central Repository์™€ JCentral Repository๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•ด์ค€๋‹ค. ์ด ์ €์žฅ์†Œ๋“ค์€ Maven ์ค‘์•™ ์ €์žฅ์†Œ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. 

    dependencies

    : build script์—์„œ ์‚ฌ์šฉํ•  ์™ธ๋ถ€ ์˜์กด์„ฑ(dependency)๋ฅผ ์ •์˜. classpath(~)์—์„œ Spring Boot Gradle ํ”Œ๋Ÿฌ๊ทธ์ธ์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ •์˜. ${springBootVersion}์€ ์•ž์„œ ext๋ฅผ ํ†ตํ•ด ์ •์˜ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฒ„์ „์„ ์ง€์ •ํ•ด์ค€๋‹ค. ์ด ํ”Œ๋Ÿฌ๊ทธ์ธ์€ ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ์™€ ํŒจํ‚ค์ง•์„ ์ง€์›ํ•œ๋‹ค.

    plugins {
        id 'java'
        id 'eclipse'
        id 'org.springframework.boot'
        id 'io.spring.dependency-management'
    }

    ๋‹ค์Œ์œผ๋กœ๋Š” ์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. (Gradle ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ ์šฉํ•˜๋Š” ๋ถ€๋ถ„)

    ํŠนํžˆ io.spring.dependency-management๋Š” Spring ํ”„๋กœ์ ํŠธ์—์„œ ์ข…์†์„ฑ ๊ด€๋ฆฌ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

    repositories {
        mavenCentral()
    }

    -> ๊ฐ์ข… ์˜์กด์„ฑ๋“ค์„ ์–ด๋–ค ์›๊ฒฉ ์ €์žฅ์†Œ์—์„œ ๋ฐ›์„์ง€๋ฅผ ์„ค์ •

    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    ๋‹ค์Œ์œผ๋กœ๋Š” ์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๊ณ  ์ฑ…์— ๋‚˜์™€์žˆ์ง€๋งŒ, ์ด์ „ ๋ฒ„์ „์˜ Gradle์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ 

    dependencies {
        implementation('org.springframework.boot:spring-boot-starter-web')
        testImplementation('org.springframework.boot:spring-boot-starter-test')
    }

    ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ์„ ์„ ์–ธํ•œ๋‹ค.

    ์ด๋ ‡๊ฒŒ ํ•ด์„œ ์ฑ…๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ–ˆ๋Š”๋ฐ Gradle ๋ฒ„์ „ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ด gradle ํŒŒ์ผ์˜ gradle-wrapper.properties ํŒŒ์ผ์— ๋“ค์–ด๊ฐ€

    distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip

    ์—์„œ ์›๋ž˜ ์˜ค๋ฅ˜ ์—†์ด ์“ฐ๋˜ ๋ฒ„์ „์ธ 8.0 ver๋กœ ๋ฐ”๊พธ์–ด์ฃผ์—ˆ๋‹ค. (์ €๊ธฐ์„œ 8.2๋ผ๊ณ  ๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์—์„œ ์›ํ•˜๋Š” ๋ฒ„์ „์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ๊ณ  ์ฝ”๋ผ๋ฆฌ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋œ๋‹ค.)

    Lombok ์„ค์ •

    compile('org.projectlombok:lombok')

    ์ฑ…์—์„œ๋Š” dependencies ๋ธ”๋ก์— ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ผ๊ณ  ํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ–ˆ๋”๋‹ˆ Test ์ฝ”๋“œ ์‹คํ–‰๋•Œ

    error: variable name not initialized in the default constructor

    ์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

     

    ์ฐพ์•„๋ณด๋‹ˆ ์ตœ์‹  ๋ฒ„์ „์˜ gradle์—์„œ๋Š” annotationProcessor๋„ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ•˜์—ฌ ์ด ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

    implementation('org.projectlombok:lombok')
    annotationProcessor('org.projectlombok:lombok')

    ํ›„์— settings -> Plugins -> MarketPlace์—์„œ Lombok์„ ์„ค์น˜ํ•˜๊ณ  IntelliJ๋ฅผ ์žฌ์‹œ์ž‘ํ•œ๋‹ค. 

    ๋˜ํ•œ settings -> Build, Excution, Deployment -> Compiler -> AnnotationProcessor ์—์„œ Default๋ฅผ ํด๋ฆญ ํ›„ Enable annotation processint ์ฒดํฌ๋ฐ•์Šค์— ์ฒดํฌ๋ฅผ ํด๋ฆญํ•œ๋‹ค.

    Test ๊ฒฐ๊ณผ

    build.gradle ์ „์ฒด ์ฝ”๋“œ

    buildscript {
        ext {
            springBootVersion = '3.0.5'
        }
        repositories {
            mavenCentral()
            jcenter()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    plugins {
        id 'java'
        id 'eclipse'
        id 'org.springframework.boot' version '3.0.5'
        id 'io.spring.dependency-management' version '1.1.0'
    }
    
    group = 'com.sujin.book'
    version = '1.0-SNAPSHOT'
    sourceCompatibility = '17'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation('org.springframework.boot:spring-boot-starter-web')
        testImplementation('org.springframework.boot:spring-boot-starter-test')
        testImplementation('junit:junit:4.12')      // Junit 4.12 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
        implementation('org.projectlombok:lombok')
        annotationProcessor('org.projectlombok:lombok')
    }
    
    test {
        useJUnitPlatform()
    }

    ์ด๋ ‡๊ฒŒ ์ „์ฒด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ ํ›„,

    ์ฝ”๋ผ๋ฆฌ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์Šคํ”„๋ง๋ถ€ํŠธ ๊ฐœ๋ฐœํ™˜๊ฒฝ ์ดˆ๊ธฐ ์„ค์ •์ด ์™„๋ฃŒ๋œ๋‹ค. (Lombok Setting๊นŒ์ง€ ์™„๋ฃŒ๋œ ์ „์ฒด ์ฝ”๋“œ์ด๋‹ค.)

    -> ์ด๋ ‡๊ฒŒ ์šฐ์ธก์˜ ์ฝ”๋ผ๋ฆฌ๋ฒ„ํŠผ -> Dependencies๋ฅผ ํ†ตํ•ด ์Šคํ”„๋ง๋ถ€ํŠธ spring-boot-starter-web๊ณผ spring-boot-starter-test ์˜์กด์„ฑ์ด ์ž˜ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    Reference

    book: ์Šคํ”„๋ง๋ถ€ํŠธ์™€ AWS๋กœ ํ˜ผ์ž ๊ตฌํ˜„ํ•˜๋Š” ์›น ์„œ๋น„์Šค(์ €์ž-์ด๋™์šฑ)

Designed by Tistory.