본문 바로가기
Spring

Gradle 이용한 Spring Boot 프로젝트 생성

by 매트(Mat) 2020. 8. 28.

Gradle을 이용한 Spring Boot 프로젝트 생성

참고서적 : 스프링부트와 AWS로 혼자 구현하는 웹서비스

개발 환경

  • intellij community version
  • java 1.8
  • gradle 4.10.2

앞서 스프링 이니셜라이저(start.spring.io/)를 통해 프로젝트 생성을 해도 되지만, build.gradle의 코드가 무슨 역할을 하는지, 이니셜라이저 외에 추가가 필요하면 어떻게 해야할지 등을 알기 위해서입니다.

build.gradle

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

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

group 'com.minsu'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
  • ext : build.gradle에서 사용하는 전역변수를 설정하겠다는 의미
  • io.spring.dependency-management 플러그인 : 스프링 부트의 의존성들을 관리해주는 플러그인(필수)
  • repositoires : 각종 의존성(라이브러리)들을 어떤 원격 저장소에서 받을지를 정합니다.
  • jcenter() : 라이브러리 업로드를 간단하게 합니다. (요즘 많이 사용)
  • dependencies : 프로젝트 개발에 필요한 의존성을 선언

intellij에서 github 사용하기

목적 : 깃 명령어로 하는 것도 좋지만, 인텔리제이에서 바로 커밋과 푸쉬를 할 수 있도록 합니다.

1. 먼저 깃허브 회원가입을 합니다.

2. Ctrl + Shift + A를 눌러 share project on github 검색

정보를 입력한 후에 Share 버튼을 누릅니다.

주의 : .idea 디렉토리는 커밋하지 않습니다. (체크 해제 꼭!!)
그 이유는 인텔리제이에서 프로젝트 실행시 자동으로 생성되는 파일들이기 때문에 깃허브에는 불필요합니다.

3. 앞으로 .idea 폴더를 모든 커밋에서 제외시키도록 하기 위한 설정

먼저, Ctrl + Shift + A > .ignore 검색 후 install & restart

메인 프로젝트 오른쪽 버튼 > new > .ignore file > gitignore file > Generate > 코드 추가

4. Ctrl + K : 커밋 후 Ctrl + Shift + K : 푸쉬


Gradle 프로젝트 생성시 junit4 컴파일 에러시

Gradle 버전이 맞지 않는 문제로, 다음 사이트를 참고할 것!
https://github.com/jojoldu/freelec-springboot2-webservice

alt + F12 터미널창에서 다음 코드 입력

gradlew wrapper --gradle-version 4.10.2

이는 반드시 스프링 부트 버전 2.1.x 버전이어야 함을 아시면 됩니다.
(스프링 부트 버전 2.2.x는 안됩니다.)

댓글