Gradle tasks are the primary commands that instruct Gradle to perform some function. Learn about the built-in Gradle tasks and how to run them. You will run them from the IDE.
- [Instructor] In this video, we'll talk about Gradle tasks. First, we'll drill a little deeper on a definition of what is a Gradle task. Then, we'll spend a little bit of time seeing how to run Gradle tasks. And finally, we'll talk about where to learn more about the available Gradle tasks and what they do. What are Gradle tasks? Well, they're the functions performed by Gradle, so they're the individual things that Gradle does. They consist of one or more actions. Usually, it's a single action. And they are what we can tell Gradle to do.
So, we can't tell Gradle to perform an action directly, but by telling it to perform a task, the actions within that task will be executed. And that's basically all we do when we run Gradle, is we're telling it what tasks to use. And as we talked about it earlier in the course, a task may depend on another task, so by running one task, we may actually be causing a number of tasks to run. Okay, let's look at some code. Let's see how we run a Gradle task. Well, we'll open the terminal here on the bottom left and we'll use gradle, or gradlew if you're more comfortable with that, and we'll run a task such as build.
And what build does is it compiles our code and produces the build artifact. And so, this is all we needed to do to run a Gradle task. Let me open up the terminal a little bit more, and you'll notice that by running the Gradle build task, each of the lines in the terminal output is a task that has been run because of running the build. So, the build depended on them to run first. And so, we should see the build as the final step, with all the other tasks required before it running before it.
So, if we look down to the bottom of the terminal session, we can see that our last task was json-display:build. And we'll talk more about what that means in a minute. So, let me close the terminal session now. Now, let's talk a little bit about how do we know what tasks are available and what they do? And there's a couple ways to do that. One way is to actually ask Gradle. So, I'll open up the terminal session again, and I'll run a Gradle command called tasks, and here, the actual tasks is a task itself, so when I hit enter, we'll see a rather long display that I can scroll through.
And you might notice that some of the lines begin with a green word, and that's the name of the task itself. So here, in the middle of the terminal output screen, we can see the assemble task, and then, separated with a dash, is the description of the task. So, a couple of things that we're seeing in this output. One is the tasks that are available to us, and then a brief description of what the task is. Let me close the terminal session. Now, there's also another way to see the details of what tasks are available and what they do, and that's in the documentation for Gradle itself.
So, let me go to the DSL webpage within Gradle. And I'm looking in Chapter 47 The Java Plugin, The Java Plugin, which is what we're using to create our Java application, has a section 47.3. If I click on that, I can see a list of tasks where the first column is the name of the task, the second column describes its dependencies, in other words, what tasks that it depends on. The third column is the type of the task, and that'll be important later when we're creating our own tasks.
And then finally, the last column is the description that describes in more detail what the task does. Let me scroll down. So for instance, one of the tasks is uploadArchives. And we can see in the last column that that task uploads the artifacts in the archive, including the jar file, to some destination that we specify when we run that task. And often, tasks need additional information to tell them exactly how to operate. That's just one example. You can see others.
Right below that is the clean task. That's one that we've seen in a prior video that simply deletes the build directory. So this is an excellent source for understanding what tasks are available, how they relate to each other, and what they do.
- The purpose of Gradle
- Building a Java project with Gradle
- Adding a dependency to a project
- Creating a library module
- The Gradle project structure
- Creating new Gradle tasks
- Profiling and using the Gradle cloud