작업 속성과 구성
사용자 지정 작업에 속성을 추가하고 구성하며 증분 빌드가 이루어지도록 하는 방법을 배웁니다.
작업 속성과 구성은(는) CoddyKit의 무료 Groovy & Gradle: JVM Automation and Build Engineering 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Groovy & Gradle: JVM Automation and Build Engineering 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Groovy & Gradle: JVM Automation and Build Engineering 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What are Task Properties?
Custom Gradle tasks can be made highly flexible by adding properties. These properties act like variables that allow you to configure a task's behavior from your build.gradle script.
Using properties makes your custom tasks reusable and adaptable to different project needs without having to change their core logic.
Defining Simple Properties
To add a property to a custom task, you simply declare a public field within your task class. For basic types like String, int, or boolean, this is straightforward.
For more advanced scenarios, Gradle also offers dedicated Property types, but we'll start with simple declarations.
Custom Task with a Property
Here's a basic custom task defined directly in build.gradle. It has a message property that will be printed when the task runs. Notice the default value set for the property.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyMessageTask extends DefaultTask {
String message = "Default task message"
@TaskAction
void printMessage() {
println "Task says: ${message}"
}
}
task helloWorld(type: MyMessageTask)
Configuring Task Properties
Once a task property is defined, you can easily set its value from your build.gradle file. This is done within the task's configuration block, which allows you to customize the task instance.
Run the example to see how the default message is overridden.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyMessageTask extends DefaultTask {
String message = "Default task message"
@TaskAction
void printMessage() {
println "Task says: ${message}"
}
}
task helloWorld(type: MyMessageTask) {
message = "Hello from CoddyKit!"
}
Inputs for Incremental Builds
One of Gradle's most powerful features is incremental builds. This means Gradle can skip tasks if their inputs and outputs haven't changed since the last build, saving a lot of time.
To enable this, you must tell Gradle which properties are considered task inputs. These are properties whose values influence the task's outcome.
Declaring Input Properties
You mark a property as an input using the @Input annotation. Gradle will then track its value. If the value changes between builds, the task will execute. If not, Gradle can mark it as UP-TO-DATE and skip it.
For file or directory inputs, use @InputFile, @InputDirectory, or @InputFiles.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
class MyInputTask extends DefaultTask {
@Input
String greeting = "Hola!"
@TaskAction
void greet() {
println "Greeting: ${greeting}"
}
}
task sayHello(type: MyInputTask) {
greeting = "Bonjour!"
}
Outputs for Incremental Builds
Equally important are outputs. These are the files or directories that your task creates or modifies during its execution. Gradle also tracks these to determine if a task is up-to-date.
If a task's inputs haven't changed AND its declared outputs are present and valid, Gradle knows the task doesn't need to run again.
Declaring Output Properties
You use annotations like @OutputFile for a single result file or @OutputDirectory for a directory containing results. These tell Gradle where the task's generated artifacts will be stored.
The task will ensure the parent directory exists before writing the file.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
class MyOutputFileTask extends DefaultTask {
@Input
String content = "Default report content."
@OutputFile
File outputFile = project.file("build/reports/output.txt")
@TaskAction
void generateFile() {
outputFile.parentFile.mkdirs()
outputFile.write(content)
println "Generated: ${outputFile.name}"
}
}
task generateReport(type: MyOutputFileTask) {
content = "Report data for: " + new Date().format("yyyy-MM-dd HH:mm:ss")
}
The Power of Incremental Builds
By correctly declaring both inputs and outputs, you unlock Gradle's powerful incremental build feature. This means your tasks only run when necessary, drastically speeding up build times.
This optimization is crucial for efficient development workflows and robust CI/CD pipelines, ensuring quick feedback and resource savings.
Quick Check on Properties
Imagine you have a custom Gradle task that takes a specific configuration file as an input. Which annotation would you use for the property that defines the path to this single configuration file?
Recap: Task Properties & Incremental Builds
In this lesson, you learned how to add properties to custom tasks, making them configurable and reusable. You also discovered how to mark properties as inputs (e.g., @Input, @InputFile) and outputs (e.g., @OutputFile, @OutputDirectory).
These annotations are key to enabling Gradle's powerful incremental build feature, which significantly speeds up your builds by skipping tasks that are already up-to-date. Keep practicing to master efficient task configuration!
자주 묻는 질문
“작업 속성과 구성” 강의는 무료인가요?
네 — “작업 속성과 구성” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 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개 중 3번째 강의입니다.
“작업 속성과 구성” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Groovy & Gradle: JVM Automation and Build Engineering 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Groovy & Gradle: JVM Automation and Build Engineering 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 사용자 지정 작업 정의
- 작업 타입과 동작
- 작업 속성과 구성
- 증분 작업과 최신 상태 확인