Join James Harmon for an in-depth discussion in this video Gradle wrapper, part of Gradle for Java Developers.
- [Instructor] In this video we're going to talk about the Gradle Wrapper. I'll define, what is the Gradle Wrapper? Then we'll see how we can use the Graddle Wrapper. Then we'll explore the Gradle Wrapper directories and files. Then finally, we'll use the Gradle init command to actually create a Gradle Wrapper if you don't have one automatically generated for you. First, let me talk about what is the Gradle Wrapper? It's a thin layer around Gradle. That's where the term wrapper comes from.
It sort of wraps real Gradle. It's a thin layer that checks to see that the requested version of Gradle is installed and if it's not, it will install it automatically. Then finally, it passes whatever command we're sending to Gradle onto the real Gradle. Again, it's just a very thin wrapper. It's only purpose is to make sure Gradle is installed properly. Let's look at some code. I'm in IntellliJ in the Java project that I've been using in the prior videos.
If you're starting at this point you can get the starter project from the exercise files. Let's see how we use the Gradle Wrapper. I'm going to open a terminal session and I'll issue a Gradle command to show me my projects. Something that we've done before. We can see in the output that we've got a root project and we've got a sub project for the Json display. Now I'm going to clear my terminal and I'm going to use the Gradle Wrapper.
./gradlew -q, same as before, and projects, same as before. When I issue that command you'll notice that the output is exactly the same. Now the only difference is that rather than running the Gradle command, I'm running GradleW. Otherwise the output seems the same. Now what's happening under the hood. Well, let's go back to the directory structure. I'll close the terminal by clicking on the x.
You'll notice that we've got two files, GradleW and GradleW Bat. These are scripts that will run the Gradle Wrapper. We have two scripts because Gradle can run in two different environments. GradleW is for Unix type environements. If I open up that file you'll notice here on line one that that's a shell script. The second file, GradleW Bat, you'll notice on line four that's for Windows type environments. So GradleW is what we use on the Mac since it's a Unix style environment.
Now what do these scripts do? What they do, is they run a small Java program that's part of the Gradle Wrapper. The location for that Java program is in the Gradle folder, underneath the wrapper subdirectory and in a jar file called Gradle Wrapper. This is a Java program that just looks to see what version of Gradle we have and what version of Gradle we want. Where does it look to see where that is? Well, in this file just below it, GradleWrapper.properties.
If I open up that file, on line six, you'll notice that there's a long reference to the Gradle distribution. That distribution is hosted on the Gradle.org site. At the end of the url there you can see that there's a reference to the Gradle version 3.5. If we wanted to use a different version of Gradle, we would simply change that, so let me modify the end of line six and I'll use 3.4 there.
Then I'll just save that. Actually we're in IntelliJ. We don't really have to save anything. I'll just close that. Now the next time I run the Gradle Wrapper. Let me open up my terminal and run the Gradle Wrapper for a projects task. You'll notice that it's taking a little longer in this case because it's making sure that we do have 3.4. If we didn't it would go out and get 3.4.
Otherwise, the command looks exactly the same. All it did is make sure that we have that. Now I'll close the terminal session. Let's say we didn't have the Gradle Wrapper installed. Now we have it because we used the IntelliJ project wizard, but you might have older projects that don't have it. What I can do, is I can delete the Gradle Wrapper files, which is the Gradle directory and the two script files, and now the Gradle Wrapper is gone.
In my terminal, if I try to run the Gradle Wrapper, you'll notice it says no such file or directory. How do I put it back? In this case, the Gradle command, not the Gradle Wrapper but the real Gradle, has a task called wrapper which will create that for us. We need to tell it what Gradle version we want the wrapper to be generated for. I'll use 3.5 which is the most recent right now. It may be different for you.
I'll press enter and the terminal shows that it successfully built the wrapper. Now if I close the terminal and go back to my project view you'll notice that those files which I just deleted are all back now because the Gradle Wrapper task has added those back. If I open up the Gradle Wrapper.properties file we can see on line six that it has generated the properties file with the correct version of Gradle. One final comment that I'll make is why is this important? Well, this will ensure that if somebody else is building the project that you're working on that they will build it under the same version of Gradle.
Also, if you're publishing your project to a continuous integration server, such as Jenkins, when the build server runs your scripts again, it will be using the exact same version of Gradle that you're using so the build will be exactly the same. Because of that, it's important that you do check in the Gradle file's diversion control. That is something that should be a part of your project going forward.
- 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