Part 2: Running Gradle Tasks
Learn the basics of Gradle tasks by running one and looking at its output.
Step 0. Before you Begin
-
You initialized your Java app in part 1.
Step 1. Viewing available Tasks
A task is a basic unit of work that can be done by Gradle as part of the build.
In the tutorial directory, enter the command below to list all the available tasks in the project:
$ ./gradlew tasks
The list includes tasks contributed by the application plugin and the plugin it applies:
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.
...
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
...
Other tasks
-----------
compileJava - Compiles main Java source.
...
A task might be responsible for compilation, copying, and moving files around, creating JAR files, generating Javadoc, publishing artifacts to repositories, or many other discrete units of work.
You can also list the tasks only available in the app subproject by running ./gradlew :app:tasks.
You can obtain more information in the task listing using the --all option: ./gradlew tasks --all.
|
In part 1 of the tutorial, we ran the build task using the ./gradlew build command.
Step 2. Understanding Tasks
We’ve listed the tasks available when the project is initialized. However, users can create custom tasks that perform some specific work.
Custom task code is typically available in the build.gradle.kts file, or it can come as part of a custom plugin. We will learn more about plugins in part 4 of the tutorial.
Here is an example Hello World task:
tasks.register("welcome"){
doLast {
println("Welcome to Gradle")
}
}
This custom task, if added to build.gradle.kts file in the app subproject directory, would be executed using the command ./gradlew :app:welcome.
Many times, a task requires another task to run first. If task B uses the output of task A, then task A must complete before task B begins.
-
A task may declare its dependencies explicitly.
-
A task may depend on other tasks implicitly.
Here is an example of explicit task dependency:
tasks.register("hello") {
println('Hello!')
}
tasks.register("greet") {
println('How are you?')
dependsOn("hello")
}
In this case, hello prints before greet. The output is Hello! How are you?.
Task execution order is automatically determined by Gradle, taking into account explicit and implicit task dependencies. If there is no dependency between tasks, Gradle enables users to request a specific execution order.
Step 3. Viewing Tasks in the IDE
Project tasks are also available in IntelliJ. The project should be open following part 1 of the tutorial.
On the right-hand side of your window, open the Gradle pane:
Step 4. Running Tasks in the IDE
You can run a Gradle task via IntelliJ by double-clicking that task in the pane.
Double-click tutorial > app > build > build.
Once the build finishes, make sure it is successful in the IntelliJ console:
BUILD SUCCESSFUL in 966ms
7 actionable tasks: 7 executed
3:18:24 AM: Execution finished 'build'.
Step 5. Running Tasks in the Terminal
The jar task can be used to create an executable JAR file of the App.
Run the following command in your terminal:
$ ./gradlew jar
Once the build finishes, an app.jar file is created in your tutorial/app/build/libs/ folder.
Invoke the run task and check the output:
$ ./gradlew run
> Task :app:run
Hello World!
BUILD SUCCESSFUL in 436ms
Take a moment and read the Java source code in tutorial/app/src/main/java/com.gradle.tutorial/App.java:
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
Step 6. Understanding Typed Tasks
Gradle tasks are typed.
This example shows a task of type Copy:
tasks.register("copyTask",Copy) {
from("source")
into("target")
include("*.war")
}
This task copies *.war files from the source directory to the target directory.
Popular tasks types include:
-
Copy -
Copyis useful to copy files around. -
Delete -
Deleteis useful to delete files and directories. -
Exec -
Execis useful to execute arbitrary O/S commands. -
Zip -
Zipis useful to bundle files.
Many more types are included in the DSL documentation.
To view a task type, use the command gradlew help --task <TASK_NAME>.
Next Step: Understanding Dependencies >>