천복만복 프로그래밍/Gradle

Understanding Gradle #05 – Lifecycle Tasks

U&MeBlue 2023. 3. 5. 16:55

이번 시간에는 별다른 노력 없이 빌드 작업을 좀 더 쉽게 해주는 방법에 대해서 소개한다. 바로 Lifecycle Task 를 설정하고 추가하는 방법에 대해서 소개한다.

지난시간에 gradle 의 Task 에는 두가지 종류가 있다고 했다. 사용자가 gradle 에게 어떤 작업을 수행해야 하는지를 설명하는 Lifecycle Task 와 실제로 작업을 수행하는 Actionable Task 가 있다.

여기서는 Lifecycle Task 에 집중하여 gradle build 유저에게 보여질 필요가 없는 불필요한 LifeCycle Task 들을 숨기는 방법, 추가적인 Lifecycle Task 를 추가하는 방법에 대해서 소개한다.

좋은 Lifecycle Task 설정은 gradle build 사용자로 하여금 빌드를 좀 더 accessible 하게 하고 ci 와 좀 더 쉽게 통합할 수 있게 해준다.

Run :tasks to see available tasks

지난 시간에 tasks task 를 이용해서 어떤 종류의 task 들을 수행할 수 있는지 확인할 수 있다는 것을 배웠다.

❯ ./gradlew :app:tasks

> Task :app:tasks

------------------------------------------------------------
Tasks runnable from project ':app'
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':app'.
dependencies - Displays all dependencies declared in project ':app'.
dependencyInsight - Displays the insight into a specific dependency in project ':app'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
outgoingVariants - Displays the outgoing variants of project ':app'.
projects - Displays the sub-projects of project ':app'.
properties - Displays the properties of project ':app'.
resolvableConfigurations - Displays the configurations that can be resolved in project ':app'.
tasks - Displays the tasks runnable from project ':app'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the test suite.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 591ms
11 actionable tasks: 1 executed, 10 up-to-date

실행결과를 보면 아주 많은 task 들이 출력된 것을 볼 수 있는데, gradle 을 사용하는 개발자들이 직접적으로 필요로 하지 않는 것들도 나열되어 있어서 보기 복잡하다.

Limit the :tasks task to your own group

지난 시간에 만든 convention plugin 을 통해 몇가지 설정을 해보자. 모든 java 라이브러리(application 이든, 라이브러리이든지에 상관 없이) 에 적용될 수 있는 my-java-base.gradle.kts 파일에 다음과 같은 설정을 할 수 있다. 여기서 해볼것은 tasks 명령어에 보여질 task 들을 그룹핑하는 것이다. 한가지 유의할점은 이렇게 해서 감춘 task 들은 단지 tasks 명령의 결과에 노출되지만 않을뿐, 실제로는 여전히 실행 가능한 상태라는 것이다.

val myBuildGroup = "my project build"
tasks.named<TaskReportTask>("tasks") {
    displayGroup = myBuildGroup // myBuildGroup 에 속한 Task 들만 tasks 명령어를 통해 노출될 수 있도록 한다.
}

tasks.build {
    group = myBuildGroup
}

이상적으로는 개발자는 점진적 빌드를 하는 build task 정도만 활용해도 상관없을 것이다.

그런데 어떤 경우에는 아주 작은 변경을 했을 뿐인데도 불구하고, 아주 많은 부분의 재빌드 과정이 필요할 수 있다. 이런 경우는 integration Test 나 end-to-end Test 를 실행해야 하는 경우가 그렇다. 따라서 상황에 따라서 다른 Lifecycle Task 의 실행이 필요할 수 있다. 그렇게 함으로서 개발자가 로컬 머신에서 작은 수정을 했을때 불필요한 빌드 타임을 소모하지 않도록 할 수 있다.

Add existing lifecycle tasks to your group

또다른 유용한 Lifecycle Task 는 check task 로 모든 하위 프로젝트의 test와 (설정되어 있다면) code quality check 를 수행해준다. 이를 myBuildGroup 에 추가했다.

val myBuildGroup = "my project build"
tasks.named<TaskReportTask>("tasks") {
    displayGroup = myBuildGroup // myBuildGroup 에 속한 Task 들만 tasks 명령어를 통해 노출될 수 있도록 한다.
}

tasks.build {
    group = myBuildGroup
}
tasks.check {
    group = myBuildGroup
}

이제 gradle 의 tasks 명령어를 실행하면 이 2가지 task 가 app subproject 와 business subproject 에서 실행가능한 것을 볼 수 있다.

728x90