Spring Boot Gradle Plugin Reference Guide
Opened this issue · 0 comments
Pyohwan commented
https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/gradle-plugin/reference/html/
Spring Boot Gradle Plugin Reference Guide
1. Introduction
- package executable jar or war archives 제공
spring-boot-dependencies
로 종속성 관리- Gradle 5.x or 6.x 버전이 필요 (4.10은 제거 예정이니 쓰지 마라)
2. Getting started
- plugin 이 필요하다
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
}
- 플러그인이 감지되면, 자동으로 반응 함
- 예를 들어
java
플러그인이 있으면 executable jar task 가 자동 구성 된다
- 예를 들어
- Spring Boot 에서는
grooby
,java
,org.jetbrains.kotlin.jvm
플러그인과io.spring.dependency-management
플러그인 처럼 최소화 해서 몇개만 씀
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
3. Managing dependencies
io.spring.dependency-management
플러그인을 apply 하면,spring-boot-dependencies
bom 에서 스프링부트 버전을 자동으로 가져 옴- 버전 생략 가능
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}
3.1. Customizing managed versions
- spring-boot-dependencies bom 에서 디펜던시 버전을 컨트롤 한다.
- 속성 목록은 여기 참고
- 버전을 커스터마이징 하고 싶다면, property 설정 하면 됨
ext['slf4j.version'] = '1.7.20'
- 각 Spring Boot 릴리즈는 써트파티 버전을 명시해서 테스트 하고 디자인했다. 버전을 재정의하면 호환성 문제가 발생할 수 있으므로 조심해라
3.2. Using Spring Boot’s dependency management in isolation
- Spring Boot 플러그인 없이 프로젝트에서 의존성 관리도 가능하다
- 먼저, 스프링 부트 플러그인 apply false 함
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE' apply false
}
- Spring Boot 플러그인 없어도, 자동으로 버전 관리 해주는건 똑같다
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
4. Packaging executable archives
- executable archives (jar files and war files) 로 생성 가능하다
java -jar
로 실행 가능
4.1. Packaging executable jars
bootJar
로 Executable jars 생성 가능java
플러그인이 있다면 자동으로bootJar
task 생성 됨
assemble
task 는bootJar
에 의존하므로,assemble
수행하면bootJar
도 함께 수행
4.2. Packaging executable wars
bootWar
로 Executable wars 생성 가능java
플러그인이 있다면 자동으로bootWar
task 생성 됨
assemble
task 는bootWar
에 의존하므로,assemble
수행하면bootWar
도 함께 수행
4.2.1. Packaging executable and deployable wars
java -jar
로 실행하고 외부 컨테이너에 배포할 수 있도록 war 패키지 가능- 이를 위해, 임베디드 서블릿 컨테이너 디펜던시에
providedRuntime
설정 추가가 필요하다
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
- 이렇게 하면
WEB-INF/lib-provided
디렉터리에서 패키지 함 (외부 컨테이너와 충돌 나지 않도록) providedRuntime
는 Gradle 의compileOnly
설정보다 우선되고,compileOnly
데펜던시가 test classpath 에 없다면 테스트 실패된다
4.3. Packaging executable and normal archives
- 기본적으로
bootJar
orbootWar
task 가 구성되어 있다면,jar
orwar
task 는 disable 된다jar
orwar
task 로 executable archive 와 normal archive 설정이 가능하다
jar {
enabled = true
}
- executable archive 와 normal archive 가 동일한 위치에 쓰이는걸 피하려면, 한쪽은 다른 위치를 사용해야 한다
bootJar {
classifier = 'boot'
}
4.4. Configuring executable archive packaging
BootJar
andBootWar
태스크는 Gradle 의 Jar, War 태스크의 subclasses 라서 jar, war 패키징 할때 사용 가능한 표준 구성 옵션을 사용 할 수 있다
4.4.1. Configuring the main class
- 기본적으로 executable archive 의 main class 는
public static void main(String[])
메소드가 있는 클래스를 찾아서 자동 구성 한다 - 명시적으로 구성 가능
bootJar {
mainClassName = 'com.example.ExampleApplication'
}
- Spring Boot DSL 의
mainClassName
property 를 써서 project-wide 하게 구성 가능
springBoot {
mainClassName = 'com.example.ExampleApplication'
}
application
플러그인을 사용중이라면mainClassName
는 무조건 해줘야 함
mainClassName = 'com.example.ExampleApplication'
Start-Class
를 manifest 에 구성 가능
bootJar {
manifest {
attributes 'Start-Class': 'com.example.ExampleApplication'
}
}
4.4.2. Excluding Devtools
- 기본적으로 Spring Boot’s Devtools 인
org.springframework.boot:spring-boot-devtools
는 executable jar or war 에는 제외되는데, 굳이 포함 시키고 싶다면 excludeDevtools property to false 로 해라
bootWar {
excludeDevtools = false
}
4.4.3. Configuring libraries that require unpacking
- nested jar 파일들을 직접 구성 가능
bootJar {
requiresUnpack '**/jruby-complete-*.jar'
}
4.4.4. Making an archive fully executable
- 리눅스 데몬처럼 stop / start 되고, 서버 시작시 자동 시작할 수 있는 셀 스크립트를 추가함
- Spring Boot default launch script 를 아카이브에 추가 함
bootJar {
launchScript()
}
- properties 정의 가능
bootJar {
launchScript {
properties 'logFilename': 'example-app.log'
}
}
- 커스텀 스크립트 수행 가능
bootJar {
launchScript {
script = file('src/custom.script')
}
}
4.4.5. Using the PropertiesLauncher
PropertiesLauncher
로 executable jar or war 를 시작하기 위해 필요한 설정
bootWar {
manifest {
attributes 'Main-Class': 'org.springframework.boot.loader.PropertiesLauncher'
}
}
5. Publishing your application
5.1. Publishing with the maven
plugin
maven
플러그인을 적용하면Upload
Task 에서 Maven repository 로 자동으로 올려준다
uploadBootArchives {
repositories {
mavenDeployer {
repository url: 'https://repo.example.com'
}
}
}
5.2. Publishing with the maven-publish plugin
- Spring Boot jar or war 를 publication 하려면 MavenPublication 에 artiface 메소드를 사용하면 됨
publishing {
publications {
bootJava(MavenPublication) {
artifact bootJar
}
}
repositories {
maven {
url 'https://repo.example.com'
}
}
}
5.3. Distributing with the application plugin
application
플러그인을 사용중이면, mainClassName property 가 꼭 필요함
6. Running your application with Gradle
- archive 없이 애플리케이션을 실행 하려면
bootRun
task 를 수행
$ ./gradlew bootRun
- 기본적으로 task 의 classpath 에서
public static void main(String[])
메소드가 있는 클래스를 찾음 - 개발중에 launch for faster startup 하고 싶으면,
optimizedLaunch
property 를 써라
bootRun {
optimizedLaunch = false
}
6.1. Passing arguments to your application
- Gradle 4.9 or later 부터
bootRun
시--args='<arguments>'
로 전달 가능
$ ./gradlew bootRun --args='--spring.profiles.active=dev'
6.2. Reloading resources
- devtools 를 사용하면 변경 사항을 애플리케이션에 자동 적용 해줌. static resources 위치를 넣을 수 있다
bootRun {
sourceResources sourceSets.main
}
7. Integrating with Actuator
7.1. Generating build information
- Spring Boot Actuator 의
info
endpoint 는META-INF/build-info.properties
파일이 있다면 자동으로 publish 한다. 이 파일을 생성하기 위해BuildInfo
태스크를 제공 함
springBoot {
buildInfo()
}
- 기존 제공 정보도 있고 커스터마이징 가능
springBoot {
buildInfo {
properties {
artifact = 'example-app'
version = '1.2.3'
group = 'com.example'
name = 'Example application'
}
}
}
8. Reacting to other plugins
- Spring Boot 플러그인은 다른 플러그인을 만나면 알아서 반응하고 적용함
8.1. Reacting to the Java plugin
- Spring Boot 플러그인과
java
플러그인이 만나면BootJar
로 executable jar 를 만들고, main source set 를 런타임 클래스 패스에 포함. 클래스는BOOT-INF/classes
에 있고, jars 는BOOT-INF/lib
에 있음bootJar
에 의존해서assemble
태스크 구성jar
태스크 비활성화- 애플리케이션을 실행하는
BootRun
태스크 만듦 - artifact 를 포함하는 bootArchives 구성 작성 함
JavaCompile
태스크는UTF-8
안 씀JavaCompile
태스크 에서-parameters
로 컴파일 아규먼트 넣음
8.2. Reacting to the Kotlin plugin
- Spring Boot 플러그인과 Kotlin 플러그인이 만나면
- Spring Boot 의존성 관리에 사용된 코틀린 버전을 플러그인 버전에 일치 시킴.
kotlin.version
만 적어주면 됨 KotlinCompile
태스크 에서-java-parameters
로 컴파일 아규먼트 넣음
- Spring Boot 의존성 관리에 사용된 코틀린 버전을 플러그인 버전에 일치 시킴.
8.3 Reacting to the war plugin
- Spring Boot 플러그인과 war 플러그인이 만나면
BootWar
가 executable war 만들고, 모든 패키징 외에도providedRuntime
구성이WEB-INF/lib-provided
로 들어감bootWar
에 의존해서assemble
태스크 구성war
태스크 비활성화- artifact 를 포함하는 bootArchives 구성 작성 함
8.4. Reacting to the dependency management plugin
io.spring.dependency-management
플러그인을 사용하면 자동으로spring-boot-dependencies
BOM 을 가져옴
8.5. Reacting to the application plugin
- Spring Boot 플러그인과 application 플러그인이 만나면
java -jar
로bootArchives
를 실행하는CreateStartScripts
태스크 생성boot
라는 새 배포 작성 하고 lib 디렉터리 내bootArchives
에 있는 artifact 와 시작 스크립트에 포함 시킴bootRun
시mainClassName
를 기본 규칙으로 사용하도록 함bootRun
시applicationDefaultJvmArgs
를jmvArgs
를 사용하도록 함bootJar
andbootWar
시mainClassName
를 manifest Start-Class 에서 사용하도록 함
8.6. Reacting to the Maven plugin
- Spring Boot 플러그인과 application 플러그인이 만나면, uploadBootArchives Upload 태스크를 구성하고, 생성되는 pom 에 종속성이 declared 되게 함