In the modern world of software development it is pretty typical that you would use many libraries to add functionality to your applications. These libraries may be open source, proprietary or even private to your enterprise. Learn how to add libraries to your project using dependency management in Gradle.
- [Instructor] In this video, we'll learn how to create a new library module. We'll start out by defining the new module with IntelliJ, then we'll add some code to the module, and finally, we'll assign the dependency in the original code, to the module. Now what's the use case for this? What we'll be doing is we'll be extracting out the json display code from our current code, and putting that into a separate module, and that would make it reusable, then we could use that json display library in other Java projects.
So, this is one of the big use cases of Gradle, making small library projects that are reusable. So let's look at some code. So I'm in IntelliJ, and I'm continuing on with my project. If you're starting from this video, you can load the starter project from the exercise files. So I'll begin by creating a new module, and I'll use the shortcut key, CMD+N to do that, the first option is Module, I'll press Return, and now what kind of module am I going to build? Well, this is again, a Gradle module, so that's already selected for me, and I'll be writing Java code in this module, so at least for me the defaults here are what I'm looking for.
I'll click Next, and now it wants me to tell it a little bit about the project. Well, the first sign says, "Add as module to the Java Project," and that's perfect, that's what we're gonna be doing, so that's correct. What's the Group ID? We're the same group of people, so info.garagesalesapp is fine. The field that's empty, Artifact ID, that's where we need to put something in, so what artifact are we building? Well this is, we'll call it the json-display module. And for the version number, again, because we're not in production yet, we're in development, we'll use that convention where we just name the version 1.0 snapshot.
The artifact ID, which for us, is perfect. So now I'll click Finish. So what has happened? Well, we've got a new directory. I'm going to look over here in my project view, and about halfway down, we can see that we've got a new directory called json-display. We'll look at that in a minute, but before we do that, we haven't looked in our settings.gradle file before, but we're gonna do that now. I'm gonna open that up, and you'll notice now that we've got two lines in here.
One describes the root project, and that's java-project. The second line, line two, was just added by IntelliJ in the new module wizard, and that says that we've now got a new module called json-display, so really there's two projects in this overall larger project. Now I can open up json-display, and I can see a couple of things there. I can see another build.gradle directory, and this is very confusing for a lot of new Gradle users.
You might have more than one build.gradle file, but not at the same level of the directory structure. So we have one that's at the root level of the directory structure, this one is inside the json-display module. Let me open that up, and it looks very similar to the one at the root level, and that's because it is another Java project. So, for the most part, it's exactly the same, at this point, there really is no difference between it and the root level build.gradle.
Close these files. And let me drill down into the source directory, we have a similar directory structure as at the root level, so source, main, java. So now I'd like to put some code into the Java folder, so I'll use the shortcut key, Cmd+N, and create a new package, and I'll call that package "info.garagesalesapp@domain," and I'll now create a class again using the Cmd+N shortcut key, and I'll create a Java class, and I'll call it "JsonDisplay." After line 6 here, I'll create a method, this'll be a public static method, called showJson, and it will take an object, this will work on any generic object type.
I forgot to put my return value, string, and we'll put the block for entering some code, and for right now, we'll just say return_null, to get everything to compile. Now what I wanna do is move my code to actually turn the object into json here, and where's that code at? It's in the root source directory, in my saleEvent class, and here it is on line 55, 56, and 57, so I'm going to take that code out of here, go back to JsonDisplay, and I will put some lines after line 8, and paste it in there.
Now in this case, we've got a couple of problems, one is that we're getting an error on the gson library, why is that? Well here we're in an independent project, so this project doesn't see the gson library from the other module, so what I have to do is go into the build.gradle file for json-display, and add my dependency. So, compile is the configuration, and then we have to remember the name of the library, so where did it come from, who made it: com.google.code.gson, what's the name of the library? Gson.
What version do we want? 2.8.0. And now, if I look at my module, you can see that I no longer have a problem with the compiler error on Gson. Now I do have a problem here, the saleEvent coming in. Well I've now wrote this method to sort of take any object, so rather than saleEvent, I'll use the reference to the parameter being passed in. So now how do I use this new library I've created? Well, let me go back to the saleEvent, and now, I've created a saleEvent object, but I can use the JsonDisplay, and you'll notice I get an error there.
I really would like my code complete to work, so I need to find a way to fix this error first. What's the problem? Well, I need to tell my module that it needs to use the project. So now, in my build.gradle, I no longer have these dependencies in line 14 and 15, with gson for this project, I have a dependency with the module. And the way I specify that is a little bit different, I still tell it the configuration I want, compile, but now I tell it that I'm going to associate this with a module, so I say project, and then within parentheses, I name the module, so "json-display." And this is a little funny when you put a little colon in front of it, but that's just the syntax for this, it tells it that it's a module.
Now I can go back to saleEvent. So we can see that our reference to JsonDisplay is now compiling correctly, and I can use code complete to select my method, showJson, and now I'll pass in the saleEvent object, and everything compiles. Now let me build the project, I'll go into Terminal, by clicking on the menu item in the lower left, and I'll run a gradle build. And it looks like everything compiled properly, and now I want to show you a couple of things.
Within the json-display directory, I've got a build directory, a libs directory, and I can see that I've got a .jar file from my json-display module. And within my root project, I can drill down into build, again look at libs, and I've got a Java project snapshot .jar there. This .jar file actually contains the .jar file for the library, because in the build.gradle, on line 14, we associated the json-display module with the root project, this tells Gradle to bring in the .jar, and to put it in the project .jar.
So we've really got two projects we've created, a library project, json-display, that we could actually reuse in other places, and then we've got the Json project itself, which has incorporated the json-display library module. And finally, just to sort of verify that all this is working, let's take a look at Terminal and see our dependency report again, and remember we get that by typing the gradle command, and then the dependencies task.
So we can see that we've got a dependency on the project, json-display, and that project has a dependency on the Gson library, and then we've also got a dependency on junit, which has a dependency on a hamcrest library.
- 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