One of the most powerful features of Gradle is its ability to be extended by creating new tasks. You'll create a new task from scratch and add it to your project.
- [Instructor] In this video we'll talk about creating new Gradle tasks. We'll perform a simple task creation using Groovy. Then we'll see how we can take that task and assign a dependency on other tasks, so a different kind of dependency, not on external libraries, but tasks dependent on other tasks within the Gradle task graft. And then finally, we'll look at how our new custom tasks are integrated with the Terminal tool and the Gradle View. So let's look at some code.
I'm here in IntelliJ, and I'm using my Java project that I've been developing in prior exercises, so I'll just continue on with that. If you're starting from this point, you can load the starter project from the exercise files. So let's create a new task, and this task will show the current date. This is going to be a simple task that we'll write in Groovy. So I'll open up the build.gradle file, and down on line 18 I'll put a blank line so I can start on 19.
I'll create a new task called showDate. I'll run a method called doLast, and I'll pass it a closure, which is a bit of code that's going to run at a certain point. I'll use print line, little simple message, current date, and then I'll add a new date. And remember Groovy is providing those imports for us automatically. How do I run this task? I can open up my terminal and use Gradle, and just name the task.
So the task name is showDate, and let me open up the terminal a little bit more, and we can see the third line of the terminal output, the current date is Wednesday May 3rd. Your date will definitely be different. And just to make it stand out a little bit more, I'll put some blank lines before and after it, and then I'll run it again, and now it stands out a little bit more and you can see current date in the middle of the screen.
Okay, so that was us creating a very simple, new task. What else can we do? Well maybe we want this current date to show after the build has been performed, so we'd like to make this task dependent on running the build first. Let me close my terminal session, and now I'll add a line after line 19. This will now be the new line 20, and this will be dependsOn, now I just have to name my tasks.
And we're pretty familiar now with the build tasks so I'll name that, and now I'll run it again by opening up the terminal, lower left here, running my Gradle showDate task. And we get a bit of output there so let me open up the terminal view a bit. One of the things that we see in the middle of the screen is the logging message that says the showDate task was run. But notice the line just above it, that says build, and that's because showDate depends on build, it runs build first.
And as you can probably guess, build depends on check, which depends on test, which depends on test classes and so forth, and that's why we're getting all those log messages. One log message per task coming out the terminal view here. Let me close my terminal. I'd like to talk a little bit about how Gradle is automatically integrating this new task in the task list. So let's go and open up the Gradle view, which is the menu item on the far right of the screen. I'll open that up, and let me drill down into my Java project, look at the tasks, and I'll look at the other tasks, and you'll notice that showDate has been added there.
Let me see how it's integrated into the task itself. So I'll close the Gradle view, I'll open up the terminal view, and I'll run Gradle tasks all. And what you'll see in this rather long output again as I scroll through this, you can see all the different tasks that we have. What I wanted to point out is under the other tasks group, here in the middle of the screen you can see showDate. Now I'll close the terminal window.
Can we do a little bit better? Well we can, so one of the things that we can do is assign our task to its own group. So we'll create a new group called my tasks, and we'll give it a description, show current date. Now I'll open up the Gradle View again by clicking on the far right, and you'll notice that there's now a new task group called my tasks that contains the showDate task within it.
I'll close the Gradle view. And we'll also see that in the task list and terminal. So let me open up the terminal, and I will run gradle tasks, and if I scroll up through the output, you can see here in the middle of the terminal output, I've got a new group called my task tasks, and my task is listed underneath that. Let me point out the difference between dash dash all, and not using dash dash all.
All will show all of the tasks that are defined, where without using that, if you've got some tasks that don't have a group definition, they won't be defined. Before we leave, let me point out the difference between displaying tasks with the dash dash all argument versus displaying tasks without it. When we define a task, it's up to us whether we assign it to a group. Here on line 21, I'm assigning my new showDate task to a group called my tasks.
If I don't assign it to a group, then displaying the tasks without the all argument will not show this particular task at the terminal, but by using dash dash all, it will show it. So that's a little bit tricky. Once we assign a group to it, it's shown in both cases.
- 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