Learn how to create a Java application that uses Gradle. You will use the IDE wizard to generate the default project structure for a simple Java application. The application will not yet use Gradle.
- [Narrator] In this video we're going to create a Gradle project in IntelliJ. So we'll start by creating a Gradle project, using the IntelliJ wizard. Remember, before we had just created a regular Java project, and added Gradle manually. Next we'll add back the source code to the project, because we'll be starting from scratch, And then finally, we'll review the project structure to see how a project that has Gradle baked into it is different from a project where we add it manually. So let's look at some code. I'm exiting out of IntelliJ and going back to the welcome screen, and you should do the same thing.
And now I'm going to create a brand new project. So we'll select the first option, Create New Project, and you'll notice that Java is selected, in the column on the left. I want to select Gradle, now. So Gradle's going to be baked into this project. It's still a Java project, we can see that in the center of the screen. If Java's not selected, make sure you select that. I'll click next, and now because it's a Gradle project, it wants to know a little bit about the artifact that we're going to be creating. So we need to give it a group ID, that's our name.
So we're the Garage Sales Team, so, info.garagesalesapp. What's the artifact we're creating? Well this is a Java project. And what version is it? Often times, before you publish your artifacts to your repository, you'll keep them in sort of a development version number, and the standard for doing that is to use just, 1.0 dash, and then SNAPSHOT, as the name of your version, until you're ready to publish it. So we'll use that convention.
I'll click the next button, in the lower right, and now I need to make sure a couple of these items are selected. This is a little bit more complicated wizard for creating Gradle. Use auto-import, I'll go ahead and click yes there. Create directories for empty content roots automatically, I'll select that one as well. And that will create the source directories for our Java code. We'll create separate modules per source set, and we'll use the default Gradle wrapper, and the other items should not be selected. I'll now click the next button, in the lower right of the screen.
And I need to give it a project name. By default it's using the project name I entered as the artifact ID, and it's putting it in, the location that we've been using, our home directory, the ID of the project directory, and then a Java project folder underneath that. So I'll click finish. We're now done with the wizard, and IntelliJ builds our project. So this, it looks very similar to what we've seen before, I'm going to expand the project by clicking on the carat over on the far left of the project view, and you can see that things look a little bit different.
It's a little bit more complicated. A few more folders, a few more files. So let's start by opening one of these. We'll look at build.Gradle. And there's a little message that comes out at the top of the screen, I'm going to hide that, so that doesn't distract us. And now we can see that this is very similar to the build.Gradle file that we created manually, but there's a little bit more information in it. So in lines one and two, we can see that the group and version number coming from the dialog are in this file, in line four we can see that it's automatically generated the Java plug-in declaration, line six we really haven't seen before, but as you might guess this is just telling Gradle that the Java code that we're writing is compatible with JDK 1.8.
Then we see our repository section which tells Gradle to use mavenCentral for the repository, that's in line eight, nine and 10, and then in lines 12, 13, and 14, our dependencies section tells us what our external dependencies are. Now in line 13, this is got an external dependency with the junit framework. Normally when we write Java code, we unit test it with junit, we've got such a small amount of Java, though, that we didn't really do that, and so we don't need this particular dependency.
I could take it out if I wanted to, but we'll just leave it in. Another thing you may notice, and that the source directory where we keep our Java code, is a little bit different. Rather than keeping the package right underneath the source directory, we can see that we've got a main folder, and then a Java folder underneath that. And that's where our code is going to go. May yet wonder why that's the case, well in this case we're using the Maven directory structure, Maven is another build tool that we talked about earlier, that was very popular, and Maven adopted this directory structure.
So the Gradle team, just to make it easier to adopt Gradle, kind of used the Maven defaults for that. So now we need to put our code in here, rather than retype it, even though there's not much, we're gonna pull it in from our exercise files. So go to your exercise files folder, and find the directory for the video 0204. We'll select the begin sub-directory, and that's got an info folder within that. We'll copy it, and we'll paste that into our Java directory.
And you'll notice we're getting some red squiggly lines there, which means we have errors, and if we scroll down on line 55, we can see that it's referencing the Gson library, which we don't have as a dependency. So we'll need to do that, remember we do that in our build.Gradle file. OK, so we can now add a dependency, we'll add a dependency for the compile configuration, we'll add the name of the dependency, and that's in three parts. It's the name of the organization, and that's Google.
So, com.google.code.gson: and then the artifact that we're looking for, and we're looking for the gson library, and then the version number that we're looking for specifically, 2.8.0. I apologize, you might get distracted a little bit by a pop-up that's appearing in the lower right, depending on what your exact configuration is for IntelliJ, you may get that too. You can ignore these. If they bother you, you can close them by mousing over the X and just clicking.
So at this point, we've now got the new dependency, and you'll notice that in the sale event code, the error has now gone away. So that's gone. Now let's see if this builds. So I'm going to open up the terminal, by clicking down in the lower-left, the menu item for terminal. And I'm going to run a Gradle build. And we'll see that it looks like it's successful.
We can see near the bottom of the output where we see a big message that says "Build successful." So that tells us that it worked. What does it do? Well, Gradle creates the build directory, we can see a new build directory has suddenly appeared in our Java project, and if I expand that, I can see that we've got a classes folder, and if I drill down into that, that's where, our compiled Java code. Here we've got the main directory, info, garage sales app domain, which is the package, and then the dot class file which is the compiled Java code.
Now for a Java project, the build artifact is a a .jar file. So where is that? Well if I close my classes directory, I can open up my libs directory, and underneath that, I see a .jar file. And the .jar file name corresponds to the name of the project, which was JavaProject, and then the version which was the 1.0-SNAPSHOT. So we can see we've actually built our Java application, here. Now, calling it an application appears to be a bit of an overstatement.
We've got such a small amount of Java code, but conceptually, this is the process we would go through.
- 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