It is more effective to learn Gradle by doing. Start by creating a simple Java program and running it using the IDE.
- In this video, we're going to create a Simple Java Program. We'll start by creating the project without using Gradle, just as a reminder to ourselves what a regular Java Program would require. And then we'll build some simple starter code that we can use as the basis for subsequent labs. So if you're like me, none of this makes sense until we actually look at some code. So, let's pull up IntelliJ and get into the code. I've started IntelliJ and I'm on the welcome screen. I'm going to create a new project, so I'll select the first option.
And we get to the first screen of the new project wizard. You'll notice on the far left of the screen, there's a column of different project types. Java's automatically selected for me and I'll select that. And then I will click next in the lower right corner of the screen. Now the second screen in the new project wizard asks me if I have an existing template that I might wanna build this project from. I don't. So I'll just select next again. The third and final screen in the new project wizard wants to know the name of my project and where I'm going to put it.
So I'll call my project, Gradle Lab. And you'll notice in the field for project location, it's automatically appended the name of the project to the end of the location. Now I'm putting my project in my home directory under the idea projects folder, which is what IntelliJ uses by default. I can see that's built the project out and built the directory structure for the project. I need to create a Java class and it's not a lot of coding, but still more than I wanna type.
So I'm gonna bring it in from the exercise files. So I've copied a folder called info from the Begin directory in the exercise files for this video and I'm going to paste it into the source directory. So I'll select source, I'll right click and then paste the folder into the source directory. IntelliJ is asking me to confirm that I want to copy that directory into source.
I'll click okay. And now you'll notice that underneath the source directory, I've got a Java package called info.garagesalesapp.domain. And underneath the package, I've got a Java class called SaleEvent. And IntelliJ has automatically loaded the SaleEvent class and is displaying it for me. Now let's take a quick review of the SaleEvent code. In line six, I can see I've defined a class called SaleEvent.
In line eight, nine and 10 we're defining a few simple properties. The idea of the event, the street address and the city. And then in lines 12 through 32, I've defined the getters and setters of those properties. Let me scroll up. Now in line 36 through 42, we can see that we've overridden the two string method, so we're printing out a simple string to show the values of the properties in this SaleEvent. And then finally in 44, we've defined a main method that we'll use to test this class.
And in line 45, it will create a new SaleEvent. And then in lines 46, 47 and 48, we'll assign some initial properties. And then in line 50, we'll actually print out the value of this SaleEvent string. And we'll use that to verify that we've built our SaleEvent correctly. So let me run this to see if everything's working right. So I right click on the SaleEvent class, within my project view. And scroll down a bit and run this SaleEvent main method.
And you can see, we're getting some console, I'll put at the bottom of the screen. And on the second line of the console, I'll put that we can see the string that represents our Java class. A SaleEvent with an idea of 100, Street address of 123 Main street and so on. So things seem to be working okay. I'll close the run view. However, I'd like to change the format of my output a little bit. Rather than a unformatted string, I'd like to produce my output as Json code. So I'm going to add a little bit of code to actually display my SaleEvent as a Json object.
So I'll go to line 50 in SaleEvent and enter a new line of code. And I'm going to use a Jason library from Google called Gson. And I need to begin by defining a Gson object. I now notice on line 52 that the Gson class is in red and that's telling me that I don't have that class definition available. And of course I immediately remember, oh yes that's in a third party library called the Gson library and I need to bring that in.
Now given that the Gson library is an external library, that means that somewhere out on the internet, there's a jar file that I can use. Normally what I would have to do is go out to the internet and find the jar file somewhere and then bring it in to my project. I've done that for you. I've copied a folder called libs from the Begin directory inside the exercise files for this video. And I am going to paste it into my project at the root level.
And IntelliJ is asking me to confirm that I wanna copy it in, I'll click okay. And you'll notice that I now have a libs directory with the Gson jar file in the libs directory. I need to make the jar file part of my class path. So I'll right click on the jar file and add it as a library to my project. IntelliJ's asking me to confirm, I'll click okay. And now, you'll notice the error for the Gson class has gone away, so I can continue on.
And I'll add a new Gson builder. And I'll run the create method on the Gson builder. Now on line 56, I need to create a new string and I'll call it Json. And that new string will come from the Gson builder. And I'll run the to Json method and pass it the SaleEvent object. And then finally, I'll print that Gson string using a shortcut in IntelliJ.
And it generated the a System.outline.println statement for me, passing in as the argument, the Json string that we just created. Now I can rerun this class by clicking on the green play button in the top right of the screen. And IntelliJ has opened up the run console and you'll now notice that on the fourth line of the terminal output, we're seeing a nicely formatted Json string, representing the properties in the object.
Let's summarize a few of the pain points for this particular project. One is that we had to find the jar file for the Gson library. And if we didn't know where that was, that could actually take awhile. And then we had to add it to the class path for the project so that we could compile against it. And finally, we have to be concerned about changes in the version number to Gson. So, when a new version comes out, we'll have to go through that entire process manually again. How can Gradle help us overcome these pain points? Well we'll see in the next video.
- 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