Dev/Spring Boot
[Gradle] finalizedBy 개념
코딩하는 백구
2022. 4. 4. 13:59
Springboot API 문서도구로 Spring Rest Docs를 이용하면서 build.gradle 설정에서 주의할 점을 finalizedBy 개념과 함께 알아보자.
[ 기존 build.gradle ]
build {
dependsOn copyDocs
}
task copyDocs {
dependsOn asciidoctor
from file ("build/asciidoc/html")
into file ("src/main/resources/static/docs")
}
build 를 시작하면, dependsOn 에 의해 copyDocs 부터 실행이 된다. copyDocs는 build 폴더에 있는 Spring Rest Docs snippets 문서를 src/main/resources~ 폴더로 복사하는 작업이다. 여기서 문제가 되는 것은 copyDocs 태스크를 실행하기 위해서 build 폴더가 존재해야한다는 점이다. 그러나 build 폴더는 build를 수행한 뒤에야 생성되기 때문에 위 파일은 논리적으로 틀린 build.gradle 파일이라고 할 수 있다.
[ 변경 후 build.gradle ]
build {
finalizedBy copyDocs
}
task copyDocs {
dependsOn asciidoctor
from file ("build/asciidoc/html")
into file ("src/main/resources/static/docs")
}
dependsOn 대신 finalizedBy 를 이용한다. 두 차이는 아래에 설명하겠다. finalizedBy 에 의해 build 폴더가 생성된 후 copyDocs 태스크가 수행된다.
[ 태스크 체이닝 방법 2가지 ]
- dependsOn : 특정 태스크와 함께 동작한다.
- finalizedBy : 특정 태스크 이후에 동작한다.
[ gradle 동작 순서 ]
1) Initialization
2) Configuration - 보통 task 는 이 단계에서 동작한다.
3) Execution - task 에서 doFirst(), doLast() 프로퍼티를 설정할 시 이 단계에서 동작한다.
[ 샘플 ]