You won't be constrained by Gradle to the standard definition of the built-in build tasks. Using simple Groovy code you can extend or modify the definition of existing tasks. You can also determine when a task should run.
- [Instructor] In this video, we'll talk about extending Gradle tasks. We'll create a new task by extending the existing default task and for Java developers this is going to look very familiar we're just creating a new class by extending an existing one. And then we'll also see how we can extend our own custom classes even further. Again, for Java developers, this will look pretty familiar. So let's look at some code. I'm in in IntelliJ working with my Java project I'm just continuing to work with the same Java project that I've done in the prior videos.
If you're starting at this point, you can load the starter project from the exercise files. Let's open up the build.gradle file and we can see that in lines 19 through 28, we've got a task defined already, show date, and this is using the Gradle DSL, the domain specific language, but what if we wanted to so this in a way that looked a little bit more like regular Java? Well, first, I will delete the existing task definition, so just by highlighting lines 19 through 28, hitting the delete key, I'll remove that.
And now on line 19, I can add the new class, so I'll define a class called show date, which extends the default task and on line 21, I'll add a property of type string called date message and I'll assign that a string of date is and it's hard not to type the semicolons for us Java developers, but remember, we're in Groovy, so we don't have to.
Now I'm going to define the method that contains the action that this task performs and I'll use a special annotation that Gradle has provided for us, called task action, on line 23, and I'll define a method, on line 24, that returns a void, I'll call it show date, although, the name is arbitrary it's really the annotation that marks it as the action. And now I'll put some code in there. So what do I want to have happen in this task? Well, it will print some output, use the date message, and then add the new date, the current date to it and remember because we're in Groovy, Groovy already imports the Java.util.dateclass for us automatically so we don't have to provide an import statement for that.
So let me scroll down a little bit. Now I've defined a task class called show date, but I don't yet have a task for that so just like an object, a class definition still requires us to create the object. So on line 29, I'm going to define the task using the Gradle DSL language, so I'll type task, the name of the task, and here I'm using the camel case convention making it lowercase.
And I'll say the type of the task is show date. And even though that seems a little bit redundant with all this show date, in the first case, on line 29, the name of the task show date lowercase S is the task name where show date, capital S, is the class name for the task. Again, for Java developers, something we're familiar with. So at this point, I can now run the task. How do I do that? I can open up the terminal, clicking on the terminal tab at the bottom left.
Let me move that around a little bit so we can see some things. And I can use Gradle to then run the task and the name of the task is show date. Here in the terminal tool, we can see that on the second line of the terminal output the terminal is displaying date is Wednesday, May third. Your date will definitely be different. That's all we needed to do to extend an existing class. Now let's close the terminal window and let's explore the Java doc a little bit.
It turns out that default task extends abstract task, which implements the task interface. So on the browser, we'll find the definition for the interface task in the Java doc, in the Gradle build tools documentation site and we'll take a quick look at some of the things we can do. So I'm looking at the method summaries and we can see some of the things like a delete all actions if we wanted to remove the actions in the task for some reason, the do first method, the do last method, basically what this does is it gives us a hook for running some code before the task would execute or at the end of the execution, not only of the task but any dependent tasks that it would be based on.
And you can see the link to the site on your screen. Now let's do one more thing. How would we further extend our custom class? So one way to do it, in this case we'll stay within the DSL, the domaine specific language for Gradle, let's create a new task that extends show date, which is our custom task. So at the end of line 29, I'll hit return, and on line 31, I'll create a new task and it will be called custom show date and it's type will be show date and I will override the date message, I'll call this custom time is.
And now let me run this again this automatically gets integrated into the task list. I'll open up the terminal, start Gradle, run my custom task, custom show date, and you can see on the second line of the terminal output we now have a new message, custom time is. So let me summarize. What have we done? In lines 19 through 27, we've created a custom task declaration by extending default task.
On line 29, we've instantiated that task and assigned it to a local task name, show date. And in line 31, 32, 33, we've extended show date further using Groovy and we've overridden the date message. So a couple things here, one is, this is really exciting, this shows the power of Gradle, the ability to really create tasks on the fly and use them immediately in your flow. Also the ability to go back and forth between Groovy and Java and the Gradle DSL gives us a lot of powers.
So which one should you tend to choose, Groovy or the Gradle DSL, some of it is based on your own personal preference. I would say in general, the one with the shortest amount of code should win.
- 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