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. See how to add libraries to your project using dependency management in Gradle.
- [Instructor] In this video we're going to talk a little bit more about dependency management. So we'll review how to declare dependencies and repositories. We'll also look at some dependency reports that Gradle's able to provide for us that will actually show us the dependencies in a little bit more detail. And then we'll actually see how we can save those reports in HTML format so they could be part of your permanent archive. I'm back in IntelliJ and I'm continuing on with my project. As always, if you're starting from this video you can load the starter file from the exercise files.
So let's take a look at a Gradle command we haven't seen before. I'll open up the terminal and run gradle projects. So we have single project here called Java project, and what if I wanted to look at the dependencies? Well, I can always open up the build.gradle file and I can see those. But do any of these dependencies have other dependencies? How would I know that? Well, let me go back to the terminal. I'll create a new terminal session.
And I'll run Gradle along with a task called dependencies. And there's a quite a bit of output there. It's showing me all the dependencies for this project. I can limit that a little bit. Let me clear this console. And run it again for a particular configuration, the compile configuration. And this is a little but simpler. And in this case you can see in the middle of the terminal window here, we can see that we've only got really one dependency in this particular project.
And that's to the gson library. I'm going to run this again. And I'll run it for the test compile configuration. And this one's a little bit more complicated. We can see that we've got the dependency on com.google.code.gson and we know we needed that because in line 13 in the Java project, here in the middle of the screen, we can see that we've got the test compile dependency for that. But for the test compile that also includes all the regular compiles, so we also have the gson dependency, the junit dependency.
And notice junit has a transitive dependency on some library called org.hamcrest. And what does that do, you might ask yourself. Well, I'd answer that question in a certain way. You don't know and you don't care. And what I mean by that is that it's used internally by junit and although it might be interesting and certainly is an interesting library, it's not something we really need to know. And Gradle's brought that in as a transitive dependency and handled it all for us. We don't really even have to know that it exists.
I'll close the terminal session so that I can see my build.gradle file again. Now let's say that we wanna replace the json parsing library with a different one. Maybe one called json simple that I'm familiar with. How would we do that? Well, on line 14 we'd put in an extra line here and we'd add the dependency. We'd make that dependency apply to the compile configuration and we'd give it the name and the organization that it's coming from, which is googlecode.json-simple.
And then we'd name the artifact, which is json-simple. And then we would name the version, which at least right now is 1.1.1. Now I'm going to open the terminal and I'm going to run the dependency report again. So gradle dependencies, and I'll run it for the test compile configuration. And we'll see what that looks like. And you'll notice here at the top of the terminal output we can see that it's gotten a little bit more complicated.
We've still got the gson library, because we haven't removed that yet. But on the second line after the gson library we can see the json-simple library. Now we'll notice that the json-simple library actually has a transitive dependency. It depends on junit. And which version of junit? Junit 4.10. Now you'll notice, if you read that line carefully, that there's an arrow pointing to 4.12.
And why is that? Well, a few lines below this we see that we're bringing in the junit 4.12. And that's the one that we specified in our build.gradle. Let me show you that. There that is on line 13. So we've got a conflict. We're directly bringing in 4.12 and indirectly bringing in 4.10. Well you really can't have in a single project two versions of the same library. So Gradle has to resolve that conflict.
And we can see that what it does is it replaces the older version 4.10 with the newer version 4.12. And this happens automatically. If for some reason you wanted to go and use 4.10 instead of 4.12, you could have them both downgraded to that. But that's more unusual. Usually you want the most recent version. Let me show you one more thing related to dependency reporting. I'll close the terminal. And I can produce an html version of this dependency report.
But I can't do that with the Java plugin alone. But there is a special built in plugin that I can turn on in Gradle that will allow me to do that. So I need to apply another plugin. How do I do that? I use my apply plugin directive. And then I name the plugin. And this one happens to be called project-report. Now I'll go back to my terminal. And I will run gradle htmlDependencyReport.
I'll run that, and you'll notice I really don't see anything here. But if you notice on line 3 of the terminal output it says see the report at file://users/jamesharmon/ IdeaProjects/java-project/build/reports. Let me close the terminal. And now I can drill down into my build directory, there's reports, and if I drill deeply enough into projects, dependencies and then index html, I can right click and open this in the browser, in whatever your default browser happens to be, and notice now I get this nice dependency report that's in html.
I can click through on java-project and then I can look at the various configurations. Let me scroll down a little bit and look for testCompile and there it is in the middle of the screen, I can click on this and now I can see that same dependency report that I was seeing before. What are the advantages to doing it this way? Well one, it looks a little bit nicer, which is always pleasant. But the main advantage is that it's saved out to that directory, so if I wanted to archive this somewhere and have a permanent record of it, or email this to someone, it would be easy to do.
- 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