작업 타입과 동작
다양한 작업 타입을 살펴보고 작업 동작을 정의하며 작업 입력과 출력을 관리합니다.
작업 타입과 동작은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Groovy & Gradle: JVM Automation and Build Engineering 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Tasks: Types & Actions Intro
In Gradle, tasks are the core units of work. While Lesson 1 showed how to define basic tasks, this lesson dives deeper into task types and actions.
We'll learn how different task types offer specific functionalities, define what a task actually does using actions, and understand how to manage task inputs and outputs for efficient builds.
What are Task Actions?
A task action is a piece of code that a task executes. Think of it as the 'what to do' part of your task.
You can define actions directly within a task block or using special methods like doLast and doFirst.
- Directly: Code inside
doLast { ... }or the task block itself. doLast: Executes the action at the very end of the task's execution.doFirst: Executes the action at the very beginning of the task's execution.
Simple Task Action Example
Let's create a simple Gradle task with an action using doLast. This action will print a message to the console.
Save this as build.gradle and run gradle helloAction.
task helloAction {
doLast {
println 'Hello from a Gradle action!'
}
}doFirst vs doLast Explained
When a task has multiple actions, their order matters. doFirst actions run before any other actions, while doLast actions run after.
If you define actions directly in the task configuration block (without doFirst/doLast), they are implicitly added as doLast actions.
doFirst & doLast in Action
Observe the execution order in this example. The println outside of doFirst/doLast runs during the configuration phase.
Run gradle orderedActions to see the output.
task orderedActions {
doLast { println 'Action C: This is the main action (doLast).' }
doFirst { println 'Action B: This runs before everything.' }
doLast { println 'Action D: Another action at the end.' }
println 'Action A: This runs during configuration phase.'
}Built-in Task Types
Gradle isn't just for custom scripts! It provides many powerful built-in task types for common operations like compiling Java code, running tests, or copying files.
These types are classes that extend Gradle's DefaultTask and come with pre-defined properties and behaviors. You configure them instead of writing all the logic from scratch.
Example: The `Copy` Task Type
The Copy task type is used to copy files and directories. It's much more robust than a simple script for file operations.
To run this, first create an empty file named my_document.txt in your project root. Then execute gradle copyMyFile.
task copyMyFile(type: Copy) {
from '.' // Source directory (project root)
include 'my_document.txt' // File to copy
into 'build/backup' // Destination directory
}Example: The `JavaExec` Task Type
The JavaExec task type is designed to execute a Java application in a separate JVM process.
For this to run, you'd typically have a Java file (e.g., src/main/java/MyApp.java) with a main method. Here's how you'd define the task in build.gradle:
// In build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
task runMyJavaApp(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
mainClass = 'MyApp' // Your main class name
args 'Hello', 'World' // Optional arguments
}
// A sample src/main/java/MyApp.java file:
// public class MyApp {
// public static void main(String[] args) {
// System.out.println("Args: " + String.join(", ", args));
// }
// }Task Inputs: The 'What It Needs'
Task inputs are any values, files, or directories that a task uses to perform its work. Gradle tracks these inputs.
If the inputs haven't changed since the last build, Gradle knows it can skip executing the task (making your builds faster!). This is crucial for incremental builds.
For custom task classes (covered in the next lesson), you mark properties with annotations like @Input, @InputFile, or @InputDirectory.
Task Outputs: The 'What It Produces'
Task outputs are any files or directories a task creates or modifies. Gradle also tracks these.
By declaring outputs (e.g., with @OutputDirectory or @OutputFile for custom task classes), Gradle can cache task results, share them across builds, and properly manage task dependencies.
Understanding inputs and outputs is key to leveraging Gradle's build caching and parallel execution features.
Quick Check: Action Order
Consider a Gradle task definition:
task myTask {
doLast { println 'Action C' }
doFirst { println 'Action B' }
doLast { println 'Action D' }
println 'Action A'
}What is the correct order of output when gradle myTask is run?
Recap: Master Your Tasks!
Great job! You've explored deeper into Gradle tasks.
- We defined task actions using
doFirstanddoLastto control execution order. - You saw how to leverage powerful built-in task types like
CopyandJavaExec. - We also touched upon the importance of declaring task inputs and outputs for incremental builds and caching.
Next, we'll learn how to add properties to custom tasks and configure them for even more flexibility!
자주 묻는 질문
“작업 타입과 동작” 강의는 무료인가요?
네 — “작업 타입과 동작” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Groovy & Gradle: JVM Automation and Build Engineering 강의 전체를 잠금 해제할 수 있습니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
“작업 타입과 동작”에서 뭘 배우나요?
다양한 작업 타입을 살펴보고 작업 동작을 정의하며 작업 입력과 출력을 관리합니다. 브라우저에서 직접 실행하는 실습 코드로 Groovy & Gradle: JVM Automation and Build Engineering을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Groovy & Gradle: JVM Automation and Build Engineering을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Groovy & Gradle: JVM Automation and Build Engineering은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“작업 타입과 동작” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 사용자 지정 작업 정의
- 작업 타입과 동작
- 작업 속성과 구성
- 증분 작업과 최신 상태 확인