본문 바로가기
스프링

[Gradle] Build Lifecycle

by kdohyeon (김대니) 2023. 2. 16.
반응형

Gradle 이 빌드되는 과정이 궁금하여 찾아보던 중 공식 문서를 접하게 되었고 번역을 해두면 좋겠다는 생각이 들었다.

들어가며

  • Gradle 은 tasks 와 tasks 간 의존성을 정의하는 의존 기반 프로그래밍 (dependency based programming) 의 예시이다.
  • Gradle 은 작성된 빌드 스크립트와 플러그인의 의존성들이 서로 순서에 맞게 실행되도록 보장한다.
Gradle is an example of dependency based programming: you define tasks and dependencies between tasks. Gradle guarantees that these tasks execute in the order of their dependencies. Your build scripts and plugins configure this dependency graph

Task Graphs

  • 어떤 빌드 툴은 실행이 됨에 다라 task graph 를 구성하기도 하지만 Gradle 은 어떠한 task 가 실행되기 전에 task graph 를 그려둔다
  • Configuration avoidance 라는 기술로 Gradle 은 현재 빌드에 해당되지 않은 tasks 에 대한 configuration 를 스킵할 수 있다.
  • Task graphs 는 DAG (Directed Acyclic Graph) 이다.
  • 아래 두 그림은 task graphs 의 예시이다. 

Graph task graphs 의 예시 (https://docs.gradle.org/current/userguide/build_lifecycle.html)

Some build tools assemble a task graph as they execute tasks. Gradle builds the task graph before executing any task. With configuration avoidance, Gradle skips configuration for tasks that aren’t part of the current build. Within each project, tasks form a Directed Acyclic Graph (DAG).

Build Phases

  • Gradle 빌드는 3가지 단계로 구성되어 있으며 Initialization, Configuration, Execution 순으로 실행된다.
A Gradle build has three distinct phases. Gradle runs these phases in order: first initialization, then configuration, and finally execution.

Initialization Phase

  • 프로젝트 구조를 파악하는 단계로 settings.gradle (또는 settings.gradle.kts) 파일이 이 단계에서 실행된다. settings.gradle 파일이 포함된 디렉토리에서 Gradle 을 실행하지 않으면 Gradle 은 부모 디렉토리에서 settings.gradle 파일을 찾고 프로젝트 구성에 따라 멀티 모듈 프로젝트 또는 싱글 모듈 프로젝트로 빌드한다.
In the initialization phase, Gradle detects the set of projects and included builds participating in the build. Gradle first evaluates the settings file. Then, Gradle instantiates Project instances for each project. When you run Gradle in a directory that contains a settings.gradle file, Gradle uses that settings.gradle file to initialize the build. You can run Gradle within any subproject of the build. When you run Gradle in a directory that contains no settings.gradle file:
  1. Gradle looks for a settings.gradle(.kts) file in parent directories.
  2. If Gradle finds a settings.gradle(.kts) file, Gradle checks if the current project is part of the multi-project build. If so, Gradle builds as a multi-project.
  3. If Gradle does not find a settings.gradle(.kts) file, Gradle builds as a single project.

Configuration Phase

  • root 프로젝트 및 각 모듈의 build.gradle (또는 build.gradle.kts) 파일이 실행된다.
  • Task graph 를 생성하여 Task 를 실행할 준비를 하는 단계이다.
  • plugin 의 apply 가 실행되는 단계이다.
In the configuration phase, Gradle adds tasks and other properties to the projects generated by the initialization phase. By the end of the configuration phase, Gradle has a complete task execution graph for the requested tasks.

plugins 부분에서 apply false 를 적용하는 이유?

사실 이게 궁금해서 Gradle 의 빌드 과정에 대해 알아본 것인데, plugins 에 선언이 되면 바로 이 단계에서 바로 적용이 된다. 하위 프로젝트에서 각각 적용하고 싶은 경우에는 최상단 build.gradle (.kts) 에서 apply false 를 해두고 하위 프로젝트에서 선언하여 사용할 수 있다.

Execution Phase

  • Task graph 에 기반하여 의존성의 순서에 맞게 요청된 Tasks 를 실행한다.
  • 주로 라이브러리 다운로드, 코드 컴파일, input 읽기, output 출력하기 등이 포함되어 있다.
In the execution phase, Gradle runs tasks. Gradle uses the task execution graphs generated by the configuration phase to determine which tasks to execute. Task execution includes most of the work you normally associate with a build: downloading libraries, compiling code, reading inputs, and writing outputs.

 

참고

반응형

댓글