본문 바로가기
Spring/java & Spring

[Spring boot] Intellij Spring boot 프로젝트 생성 및 gradle 설정

by domo7304 2022. 4. 4.

이동욱님의 '스프링 부트와 AWS로 혼자 구현하는 웹 서비스' 를 읽으며 새롭게 공부한 내용에 대해 정리합니다.

2020.12.16. 기준 도구들의 버전 

  • Spring Boot 2.4.1
  • Gradle 6.7.1
  • Junit5

https://jojoldu.tistory.com/539

 

(2020.12.16) 스프링 부트와 AWS로 혼자 구현하는 웹 서비스 최신 코드로 변경하기

작년 11월 말에 스프링 부트와 AWS로 혼자 구현하는 웹 서비스를 출판 하였습니다. Spring Boot가 2.1 -> 2.4로, IntelliJ IDEA가 2019 -> 2020으로 오면서 너무 많은 변화가 있다보니, 집필할 때와 비교해 실습

jojoldu.tistory.com

위처럼 적용하려 하였으나 당장 chap2 에서 lombok 적용부터 버전차이로 문제가 발생해서, 버전 차이를 익히는 연습도 할 겸 최신 버전으로 교체하여 진행하기로 했습니다.

  • Spring Boot 2.6.4
  • Gradle 7.2

build.gradle 설정

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group 'com.eddie'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

    // lombok
    implementation 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // test
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

 

1. plugin 블럭에 대해서

https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#getting-started

 

Spring Boot Gradle Plugin Reference Guide

To manage dependencies in your Spring Boot application, you can either apply the io.spring.dependency-management plugin or use Gradle’s native bom support. The primary benefit of the former is that it offers property-based customization of managed versio

docs.spring.io

프로젝트를 할 때 항상 쓰여있지만 저 plugin 블럭이 어떤 일을 하는지 모르고, 항상 있기에 그냥 가져다 썼었다. 이번 기회에 알자 싶어 공식 문서를 참고하였다.
Spring boot plugin 은 Gradle 환경에서 제공되며, 실행가능한 jar나 war 를 패키징할 수 있게 하거나, Spring boot 를 실행하거나, spring-boot-dependencies 에 의해 제공되는 의존성 관리를 이용할 수 있도록 해준다.

2022.04.04. 기준 Spring boot Gradle plugin 은 6.8, 6.9, 7.x 버전을 필요로 한다고 한다. 현시점에서 Gradle 프로젝트를 생성하면 Gradle 버전은 자동으로 7.2 로 잡히게 된다.

Spring boot application 의 의존성을 관리하기 위해, io.spring.dependency-management 플러그인을 이용할 수 있으며 해당 플러그인의 역할은

첫 번째 이미지와 같이 라이브러리 의존성에 대해 버전을 명시하지 않더라도, 두 번째 이미지와 같이 현재 내가 사용중인 스프링부트의 버전에 따라 적절한 의존성을 자동으로 가져오게 된다.

io.spring.dependency-management 의 역할

Spring boot 와 dependency-management 의 버전의 경우 최신버전 중, 비교적 많이 쓰이고 있는 버전을 사용하였다.

Sping boot 는 2.6.4 선택
Dependency Management Plugin 은 1.0.11 선택

 

2. repositories 에 대해서

각종 의존성(라이브러리)들을 어떤 원격 저장소에서 받을지를 명시하는 곳이다. 구글에 'mavencentral jcenter' 이라고 검색하면 둘을 비교하는 정말 많은 글들이 있고, 공통적으로 라이브러리 업로드 난이도 때문에 jcenter 를 많이 사용했다고 한다.

https://blog.gradle.org/jcenter-shutdown

https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/

하지만 더 이상 지원을 중단하고 read-only 상태로 유지 중이기 때문에, 지금은 고민할 것 없이 mavenCentral() 을 사용하면 된다.

 

3. junit5 의존성에 대해서

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4-Release-Notes

 

GitHub - spring-projects/spring-boot: Spring Boot

Spring Boot. Contribute to spring-projects/spring-boot development by creating an account on GitHub.

github.com

Sping boot 2.4 이후부터는 'spring-boot-starter-test' 에서 JUnit5의 Vintage Engine 이 아예 삭제되었기 때문에 Vintage Engine 을 exclude 할 필요 없이, 아래처럼 한 줄만 써주면 된다.

testImplementation 'org.springframework.boot:spring-boot-starter-test'

 

또한, JUnit4 와 비교하여 이름이 달라진 어노테이션이 많기 때문에 작성에 주의하여야 한다.

 https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4

 

JUnit 5 User Guide

Although the JUnit Jupiter programming model and extension model will not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and cus

junit.org

 

gradle 설정을 완료하였다면, Intellij 의 알람이 나오는데, 말그대로 변경 사항이 있으니 Reload 하라는 의미이므로 눌러서 최신화 해주면 된다.

'Spring > java & Spring' 카테고리의 다른 글

[Spring] IoC, DIP 와 DI 이해하기  (0) 2022.06.25
[Spring boot] 테스트코드의 필요성  (0) 2022.04.05

댓글