Learn how to add Gradle functionality to an existing Java program. You'll do this manually by creating and editing files rather than using a wizard within IntelliJ. This will give you the ability to add Gradle to any existing Java project that you may have.
- [Instructor] So now let's add Gradle to a simple Java project. We'll do this in four steps. First, we'll create the build.gradle file. Then we'll add source code declarations to build.gradle so that it knows where our source code is. And then we're using an external library. Remember, for JSON we're using the GSON library, so we'll tell Gradle that we have an external dependency. And then finally, we'll tell Gradle where to go get that external dependency. So let's look at some code.
Now I've got IntelliJ up already. But if you're starting at this point, what you can do is load the Gradle lab from the O two, O three begin exercise files directory. So step one is to create the build.gradle file. And that's pretty easy to do, I can highlight my root project, this is highlighted in the project view in the upper left here. And I can use my shortcut key, command N, to create a new file, and I'll call that file build.gradle. You can see that it has a green circle decorator, and this is the icon for Gradle.
So anytime you see that you know that that file or directory has something to do with Gradle. So I've created the build.gradle file. The second step is to add or turn on the Java plugin. And that's pretty easy to do. I just add a apply plugin statement, and I name the plugin. Now at this point, I've Gradle enabled the project. How can I tell that for sure? Well let me open the terminal, and I'll use the link at the bottom of the screen, and I get a nice terminal.
And now I can run the Gradle command to build this project. So to do that, I run the Gradle executable. That's just simply the word gradle. And then I tell it to build this project. And at that point it starts to process, and it runs through all the steps that a Java application needs to be built. And so you can see on line two of terminal, we're doing a compile Java, then processing resources, and then on line four, classes, and on line five, jar, and so on. And in this particular case, even though we don't have anything to really build yet, we haven't told it where our source code is, it still runs through the steps.
I'll close the terminal by simply clicking on the link at the bottom. So we've applied the Gradle plugin. We saw that worked on line one. Now we need to tell Gradle where our source code is. And it's at the root level, Gradle lab, in the source directory. To do that, we need to add a source sets directive, and within the braces, we'll tell it that our main Java code, as compared to our test Java code, which we don't have any of, is in the source directory.
So we'll use the java.srcDirs directive. And the syntax for that is to use square braces. And within quotations, we'll put the name of the directory. And if we had more than one directory where we had Java source code, we could just use commas to add additional directories, we only have one. Now remember, we're using that JSON library. And that's external dependencies, so we need to tell Gradle that we have an external dependency. To do that, we need to add the dependency section on line nine, and within the braces, we'll tell it that we have a dependency for the compile configuration.
And within quotes we'll name the dependency. Why don't we drill down into how we name dependencies? They have three parts, the first part is the name of the organization that created the dependency. So in our case, for GSON, that's com.google.code.gson. Then we use a semicolon to separate the next part, which is the name of the dependency itself. So this is the GSON library here on line 10. And another colon, and now we'll tell it what version to use.
Now right now, the most current version is 2.8.0, maybe more recent for you. Okay, now on line 13, I need to do one final thing. I've told Gradle that we have a dependency, but it doesn't know where to go to get it. So let's tell it that, where is the repository? And to do that, we add a repository section, braces. And then we'll name the repository. And Gradle knows about a repository called mavenCentral, where a number of artifacts are saved in Maven format.
Now one of the things that the Gradle developers did which was a great idea is to make Gradle compatible with the Maven structure for describing dependencies. So we can use any of the existing dependencies in the Maven repository as is. So that makes it easy to adopt Gradle if you're already using Maven. So at this point, let's test this. We'll open up the terminal again, and we'll run the Gradle build. So in the terminal we ran the Gradle build, and we can see that it's gone through the various steps and compiled our code for us.
Let me close the terminal session. So let me summarize, what have we done? We've created the build.gradle file. Then in line one, we've applied the Java plugin. On line three, we've told Gradle where our source code is. On line nine, we've told Gradle we have a dependency. On line 13, we've told Gradle where the repositories are for those dependencies. And the final thing, we've a new directory here called build that we haven't really talked about before.
If I drill down into that, I can see that I've got build classes, then the package name info, garage sales, domain. And I've got my class file, which is the compiled Java code. In the libs directory, it's actually compiled our code and turned it into a jar file. So Gradle lab is actually the result of this Java build. So for this project, the build artifact is GradleLab.jar.
So even though this is a simple project, we've now gone through the entire process and built this project.
- 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