Once you've created a new Gradle task you probably want to make that task available to other projects within your enterprise. Or you may even want to make it available to the world as an open source contribution. Learn how to package your new Gradle task as a plugin that can be made available to other Gradle projects.
- [Man] In this video, we're going to create a Gradle Plugin. We'll first talk a little bit about the reasons that you might want to create a plugin, then we'll see how we can create a module for a plugin, we'll add the plugin module to our build script and our root project, and then we'll use the task from the plugin. What are some of the reasons you might have for creating a plugin? Well, first I'd like to say that, as a developer, you're going to use a lot of plugins when you use Gradle, but it's rare that you actually have to create a plugin, so you may never actually have to do this, but it's interesting and useful to know how it's done.
Well, one of the first reasons is that you might find yourself repeating the same task definitions in your build dot Gradle files for different projects over and over again, and we wanna follow the dry principal, don't repeat yourself. So rather than repeating the same task definition over and over, you might want to wrap it up into a plugin that's then reusable. It also allows you to reuse this not only within your project, but within other projects, and then across the enterprise itself, across your company.
And then finally, if the functionality that you're providing in your plugin is generic and useful enough, you may even want to make it available to the world by publishing it to an Open Source Repository. So let's look at some code and see how plugins are created. Now, I'm in the Java project I've been working on in the last videos. If you're starting at this point, you can load the starter project from the exercise files. So let me describe overall what we're going to be doing. We're going to be taking a task that we've created in prior projects, and we're going to be encapsulating that task into a Gradle plugin so it's more reusable.
So let's remind ourselves of the task itself. I'll open the build dot Gradle file, and I can see in lines 19 through 27 that I've defined a task called ShowDate and in line 29 I've created an instance of that task, and in line 31, 32, and 33, I've actually extended and modified that task. Let's verify that this task runs. I'll open the terminal by clicking on the terminal menu option in the lower left-hand corner of the screen and I'll use Gradle to run the ShowDate task.
And you'll notice here at the top of the terminal window, we get a line that says the date is Thursday, May 4th, so that's what we're looking for. I'm now going to delete this task. So I'll delete from my Gradle file, lines 19 through 33, and my task is now gone. I'll go back to my terminal window, and run the task again, and you'll notice in the middle of the terminal window we get a big red message that says the build failed, so the task is gone.
Now let's bring in the plugin module for the task. So I'm going to load the module from our exercise files from the begin directory and the module is called ShowDate. And I'll simply paste the module into the root project and IntelliJay is verifying that I actually want to do this, and I'll click "okay". And now I've got the module in my root project. You can see I've got a new folder, a new sub-directory called ShowDate.
This is where we've encapsulated that code. And I wanted to show you a couple of things very quickly. We've got the ShowDate task itself, which is open in the editor, and then we've also got the code for creating the plug, and as you can see, it's very simple. And finally, we've got a build dot Gradle file within the project. Now, at this point, you may notice that the ShowDate directory is not in bold, and that means the Gradle has not yet recognized it. And for Gradle to recognize it, we need to open up the settings dot Gradle file, and after line 2 we'll include a new line where we will include that module.
At this point, we're now ready to try doing a build, so I'll run a Gradle build, from the terminal. And we can see that that was successful. I'll close my terminal, and now I'm going to drill down into the ShowDate module, and open up the build directory. And you'll notice that underneath build and libs, is a new file, ShowDate 1.0 snapshot dot jar. That's the jar file for the plugin itself now.
And what we need to do is reference that from our root project. So I will open up the build dot Gradle file for the root project, and what I will do here is two things: one is I need to tell Gradle where to go get the jar file for the plugin, and you might think that that's a dependency, that I would enter in line 13, 14, and 15, but it turns out that that wouldn't quite be correct. Because this dependency is not a dependency on the project code I'm writing, I'll tell Gradle that this is a dependency for the build script itself, so I'm gonna add a new section to the build dot Gradle file called "build script", and within that, I'll add a section for dependencies, and within that I'll describe the location of this dependency.
It'll be placed on the class path and come from a file called "show-date", so that's the module. And then the build directory within that, and then the libs directory within that, and then it's referencing the show-date 1.0 dash snapshot jar file. So I've told Gradle where to get the dependency. I've added a few lines after line 23 to give myself some room, and now on line 25, I'll actually apply the plugin, using standard technique we've seen before.
The applied plugin command and then followed by the name of the plugin, show-date-plugin. So at this point we're done and we're ready to try running the task. I'll open up the terminal tool again, I'll run Gradle, showDate, and you'll notice that near the top of the terminal window, we're showing the current date. Let me close the terminal window. So let me summarize what we've done.
We've written the plugin module, in our case I've actually provided that for you. And then we told our project, through the Gradle build script, where to go get that plugin, we told it to turn on the plugin, and then we were able to use the tasks that were defined in the plugin. And finally, just one additional point I'll make, is that often times plugins have many many tasks defined for them, so it would be unusual for a plugin to only have a single task. But for our purposes, this shows the mechanism for creating and using it.
- 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