Gradle tasks are the workhorses of your build process. They represent actions that can be executed to build, test, and package your project. This guide provides an overview of the tasks available in Gradle and how to use them effectively.
Overview of Tasks
Gradle comes with a rich set of predefined tasks that cover most of the common build operations. Here are some of the most commonly used tasks:
compileJava
: Compiles the Java source files in your project.compileKotlin
: Compiles the Kotlin source files in your project.test
: Runs the tests in your project.jar
: Packages the main code of your project into a JAR file.assemble
: Assembles the project outputs into a directory.install
: Installs the project outputs into the local Gradle cache.
Using Tasks
To execute a task, you can simply call its name in the command line:
./gradlew build
This command will execute all tasks that are marked as UP-TO-DATE
and need to be executed.
Task Dependencies
Tasks can depend on other tasks. This allows you to define complex build processes where tasks are executed in a specific order.
task clean {
doLast {
println 'Cleaning project...'
}
}
task build(dependsOn: ['compileJava', 'compileKotlin', 'test', 'jar', 'assemble', 'install', 'clean']) {
doLast {
println 'Building project...'
}
}
In the example above, the build
task depends on several other tasks.
Extending Tasks
You can extend tasks to customize their behavior. This can be done by adding custom actions or modifying existing actions.
task myTask {
doLast {
println 'Custom task action...'
}
}
Resources
For more information about tasks in Gradle, please refer to the Gradle User Guide.
[center]