The easiest way to use Gradle is from inside an IDE like IntelliJ but you can also run Gradle tasks from the command line. This is very important when automating Gradle or running Gradle from a continuous integration build server such as Jenkins. Be a Gradle master and learn how to use Gradle from the command line.
- [Instructor] In this video we're going to talk about the Gradle View Tool. We'll first talk about what the Gradle View Tool is, then we'll see how we can use the Gradle View Tool from inside Intellij, and we'll also talk about how to run tasks with shortcuts as a little side. So, what is the Gradle View Tool? Well, it provides an alternate way within Intellij of running Gradle tasks. So, often you'll run Gradle tasks from the terminal, but this is an alternate way of doing it.
It also has it's own graphical user interface, which can make it easy to use, and that's why it's useful when you're first learning Gradle, rather than having to remember the tasks off the top of your head, the Gradle View Tool can walk you through what tasks are available, and allow you to execute them from inside the Gradle View Tool. So let's look at Intellij and see how it works. I'm in Intellij, and I'm continuing the projects that we've been using in the last video. If you're starting with this video, you can load the starter project from the exercise files.
So let's say I want to run the Gradle projects task, and I decide that I'll use the Gradle View Tool. First I have to open the Gradle View Tool, and on the far right of the screen, in the menu bar, that's kind of wrapped around around the right side of the screen, so you have to tilt your head to read that. You can select Gradle, and that opens up the Gradle View Tool, and across the top of the Gradle View Tool are a number of icons that allow you to select some of the major things that you might do in the Gradle View Tool.
So, I'm clicking on this first icon, and what that does is refreshes the Gradle project, and basically what that means, is if there's any dependencies that you specified that you haven't gone out to a repository to get, it will go do that. One of the other icons is the plus icon, and by selecting that, we can attach this as a Gradle project, now that happened automatically because we were using the Wizard, but if you were using the Gradle View in a project that you hadn't initially created as a Gradle project, this would be useful.
And a few other convenience tools, you can expand the hierarchy so you can see all of the elements, or you can collapse the hierarchy so you don't have to see all the detail. Another one that is useful is the final icon, you can click on that, and that will take you to the Gradle settings preferences, so that can be useful just to verify how you've set up Gradle, and to change it if you need to. Also, one more that's sometimes useful, is the second from the last one, Toggle Offline Mode.
What that will allow you to do, is turn off the internet check that Gradle performs every time it starts up. Remember, Gradle will let you know if new dependencies are available. Now, if you're in an environment where you don't have an internet connection, or you just want to turn that off because it's slowing down the build, you can do that here. Now, below the icons, we can see the Gradle project structure, so we can see our Root project, and if I open that up, I can see that I've got the Root project, and then the adjacent display module. So what the Gradle View is showing me is the different projects I have.
If I expand one of the projects, I can then see a few things. The first thing is the source set, so it actually can tell me where my source code is, which is especially useful if I've changed it for some reason. The third option, Run Configurations, is actually how I've set up Gradle, how I've configured it, and then the middle one Tasks, that's where we're seeing all the tasks. So, I can drill down into tasks, and I can see my Task groups, and then within a particular group, I can open that up, and then finally see the individual tasks.
So, we've been using the Build Task quite often, so you can see that I've got the Build Task selected here, I can either double click, or I can right click and run it, both of those do the same thing. I'm going to double click it, that's a little bit quicker, and then this task will actually run, and we'll see what happens in that, now the Build Task remember was a complicated task, so lots of things were happening. Let me close that and run a simpler task. So, under Help, I've got the projects task, and I can run that, and you can see this is a little bit simpler.
However, if you've been watching some of the other videos, you might have recognized that it doesn't seem to really being displaying the output of the tasks. What do I mean by that? Let's go into the terminal session for a moment, and I'll run the projects task. Actually let me run it with the dash q, the quiet option, so I don't get quite as much output, and you can see on the display in the terminal output, that it's shown us our two projects, the Root project, java-project, and the module json-display.
But if I go back into the output of the Gradle View, it doesn't actually show me that. What it's showing me instead, is the status of running the task, and it actually did run it behind the scenes, but it's not showing me the output of running it. But this can be interesting. Let me collapse this and walk through this a bit, so we can sort of see what's happening. When Gradle runs, it starts out by saying, okay, we're going to run Gradle, the first thing it does is it looks to see if there's any settings, and it configures those, and then it goes into the build files for each of the projects, the Root project and the module, and reads those build files.
At that point, it calculates the task graph. In other words, it figures out what are all the projects that we need to run, and what order do they have to be run in, and then it runs those tasks, and it shows us which tasks run, and it's showing us the status of the individual actions within the task. We can't run actions individually, but we can see the output of those actions running. And then finally, and this is interesting, in the column over on the right, we can actually see the execution time for each of these steps, and that can be useful later, especially if you want to optimize your build times.
Now, we'll see in later videos, that we can actually more directly get profiling information like this, but that is one use for the Gradle View Tool. Let me say a little bit more about the Gradle View Tool, it may not be one that you use often, many developers tend to work at the terminal tool view, but especially is you're new to Gradle, and you're interested in what tasks are available to you, then maybe some of this other information, it may come in handy, and certainly it's a significant part of the Gradle capability within Intellij.
- 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