[Spring] Spring - build.gradle

2024. 8. 14. 21:48Backend/Spring

 

 

 

작업하면서 깨닫는 build.gradle 의 중요성

 

회사에서 다루고 있는 모듈을 서버에 배포하는 과정을 위해, 관련 작업을 하고 있다. 백엔드 개발자가 되는 길목에서, 서버에 배포하기 위하여 Gitlab, Jenkins을 사용해볼 수 있다는 점이 즐거운 시간이다. (물론, 강의를 수강하면서 만들어가는 중이지만)

 

CI/CD 작업중 SCM 을 위한 Jenkinsfile을 작성을 하였고, 빌드가 짜잔~ 바로 완성이 되어 서버에 배포가 될 줄 알았다!

 

하지만, 역시나 어디선가 알 수 없는 에러들이 발생하였고, 해당 에러들을 해결해가면서 클린한 빌드 과정이 될 수 있도록 수정 작업중이다.

 

그 때 build.gradle의 중요성을 많이 깨달았고, 나중에 참고하기 위하여 간단하게라도 build.gradle의 구성요소들을 적어두려고 한다. 

 

Gradle

 

Gradle User Manual

Gradle Build Tool is a fast, dependable, and adaptable open-source build automation tool with an elegant and extensible declarative build language. In this User Manual, Gradle Build Tool is abbreviated Gradle.

docs.gradle.org

Gradle Build Tool is a fast, dependable, and adaptable open-source build automation tool
with an elegant and extensible declarative build language.

 

공식 사이트에서 Gradle Build Tool이란 우아하고 확장할 수 있는 '선언적인' 빌드 언어를 가지고 빠르고 신뢰할 수 있고 적응성이 있는 오픈 소스 빌드 자동화 툴이라고 소개한다.

 

Build.gradle 스크립트 예시

 

build.gradle 은 Gradle 빌드 도구에서 프로젝트의 빌드 설정, 플러그인, 의존성 관리 등을 기록하는 스크립트 파일이라고 할 수 있다.

 

Buildscript {		// Gradle 빌드 스크립트 설정 정의
	ext {			// extra properties, 사용자 정의 변수
    	springBootVersion = '3.x.x'
    }
    repositories {	// buildscript에 필요한 의존성
    	flatdir {	// 로컬
        	dirs 'libs'
        }
        maven {		// maven repository
            credentials {
                username = ~~~
                password = ~~~
            }
            url '~~~~~'
        }
        ivy {		// Apache Ive repository
        
        }
        mavenCentral()	// maven central repository
    }
    dependencies {		// buildscript에서 사용되는 의존성 지정
    	classpath '~~~~~'
    }
}

plugins {			// 특정 플러그인
	id 'java'
    ~~~
}

group = 'com.exapmle.gradle'// 그룹 ID
version = '1.0.0'			// 프로젝트 버전
sourceCompatibility = 17	// 소스 코드가 JAVA 어떤 버전으로 컴파일 되는가?
targetCompatibility = 17	// 바이트 코드가 JAVA 어떤 버전에서 실행 되는가?

ext {					// extra properties, 사용자 정의 변수
	
}

repositories {			// 프로젝트에 필요한 의존성
    	flatdir {
        	dirs 'libs'
        }
        maven {
            credentials {
                username = ~~~
                password = ~~~
            }
            url 'https://repo.spring.io/milestone'
        }
        ivy {
        
        }
        mavenCentral()
}

dependencies {			// 프로젝트에서 사용되는 의존성 지정

	implementaion '~~~~'
    compileonly '~~~~'
    runtimeonly '~~~~'
    testImplementation '~~~~'
    
}

dependencyManagement {	// Maven의 BOM(Bill of Materials)와 비슷한 역할, 버전 관리
	imports {
    	mavenBom '~~~~'
    }
}

publishing {			// 빌드된 아티팩트를 배포
	publications {
    	from component.java 	// jar
        //from component.web 	// war
        groupId = 'com.example'
        artifactId = 'gradle'
        version = '1.0.0'
        pom {			// 프로젝트 메타 데이터
        	name = 
            /* 이하 생략 */
        }
    }
    repositories {		// 아티팩트를 배포할 Maven repository
    	maven {
        	url = '~~~~~'
            credentials {
                username = ~~~
                password = ~~~
            }
        }
    }
}