Gradle in Action4:27 with Chris Ramacciotti
In the last video, you were introduced to Gradle as a dependency management tool. In this video, we'll see Gradle in action by listing the Spring Web Framework as a dependency, and having Gradle download it for us.
Gradle Assets in IntelliJ
If your project structure doesn't look exactly like mine in IntelliJ, just give it a minute or so. When you start a Gradle project, IntelliJ works in the background to download the Gradle wrapper and associated files. So at first, your Gradle project may lack the following:
- .gradle directory
- gradle directory
- gradlew file
- gradlew.bat file
If you were to go back to the previous video, you'd notice at the end of the screen cast portion, my project lacked these assets. But, by the beginning of this video, they've loaded. And yours will, too!
Finding 3rd Party Libraries on Maven Central
You can browse the Maven Central repository at search.maven.org directly, but I find searching on this site to be a bit overwhelming and cumbersome.
Instead, I either go to mvnrepository.com to find my Gradle dependencies, or I Google "library-name Maven", and click the first link in the search results that links to mvnrepository.com. Usually, it's the one I'm looking for.
Importing a Gradle Project into IntelliJ
If you've downloaded a Gradle project from Treehouse, Github, or another source, importing it into your IDE is usually as simple as choosing to import a project, selecting "Gradle Project", then choosing the build.gradle file. Such is the case for IntelliJ. Then, you can use your IDE (or the command line) to refresh dependencies to download all those third-party libraries. This allows developers (like you!) to distribute source code without having to pass around hefty JAR files. Among other advantages, it's a nice way to leave a smaller digital footprint.
All right, we're back in IntelliJ and I have the build.gradle file open. 0:00 Whenever you make a change to the dependencies, whether you've added or 0:04 removed some, 0:07 you can refresh the project's jar files through the Gradle tool window. 0:08 With the Gradle project open like the one we have here, 0:12 you can see the Gradle tool window by choosing View > Tool Windows, 0:14 then Gradle and the window that pops up on the right here is the Gradle tool window. 0:19 In this window you should see this refresh button. 0:24 We'll use that to refresh the dependencies 0:27 when we change them in the dependencies block here. 0:30 Let's expand external libraries on the left panel here and 0:34 see what's on the class path now. 0:38 The first one is the Java core library for Java version 1.8. 0:40 And the next two are the dependencies of our project. 0:45 J Unit is a library that is listed as a dependency here in our profile. 0:48 You see J Unit. 0:52 And hamcrest here is a dependency of J Unit but Gradle figures that out for us so 0:54 we don't have to explicitly list it as a dependency in the dependencies block here. 0:59 That's the beauty of Gradle. 1:04 And just to drive home the fact these external libraries are indeed jar files, 1:06 let's expand each one of these, junit and hamcrest to reveal the fact that, 1:10 yes, we indeed have a jar file here and a jar file here. 1:15 I'm going to collapse these two dependencies and go into 1:20 our dependencies block and delete what we have in here to add one of our own. 1:24 Let's make it the main one we'll use throughout this course. 1:31 The spring Web MD C library. 1:33 This is the part of the Spring frame work that will allow us to write our web 1:36 application. 1:39 The basic way of adding a dependency in Gradle is by typing compile followed by 1:40 single quotes, then the group name. 1:44 Which in our case is org.springframework. 1:46 We follow that with another colon, and then the group ID, which, in this case, 1:51 is spring-mvc. 1:55 Then another colon, followed by the version, 1:56 which as of the recording of this course, is 4.2.1.RELEASE in all caps. 2:00 I'll go ahead and save that file. 2:07 If you're having trouble memorizing that, never fear. 2:10 Switch to a web browser and and do a quick Google search of spring webmvc maven. 2:12 I use maven here because the main repository we're going to 2:20 use is called maven central. 2:23 We see a bunch of search results here, but 2:27 the one that we are interested in is the one that comes from mvnrepository.com. 2:28 Let's click on that. 2:35 There we see Spring MVC. 2:37 We'll choose the version that we like, which is now version 4.2.2.Release. 2:39 And we see a series of tabs here at the bottom. 2:46 I'll choose Gradle, 2:49 since that the dependency management tool that we are using. 2:49 Here you see the exact line you need that follows the compiled key word, 2:53 in your Gradle build file. 2:57 So you can highlight that, copy it, then go back into Intelli J, and paste that. 2:58 Let's review what we just typed. 3:07 We have the compile keyword, 3:09 which tells Gradle that we want this dependency available at compile time. 3:10 That is, it's required in order for our source code to compile. 3:14 Then we have the group ID, org.springframework followed by a colon. 3:18 Then the artifact ID spring-webmvc followed by another colon. 3:23 And finally the version, which is 4.2.2.RELEASE. 3:27 To see Gradle in action, let's save this file, 3:34 then click the Refresh button in the Gradle tool window. 3:37 If you watch the external dependencies on the left you'll see them grow 3:42 substantially. 3:45 Here I'll reiterate an important feature of a dependency management tool 3:47 like Gradle. 3:51 It resolves dependencies for you. 3:52 [SOUND] In the case of our Spring app, 3:55 we need to utilize this spring-mvc library in our code. 3:57 But this spring-mvc library has a bunch of dependencies of its own. 4:01 [SOUND] Six of them to be exact, and two of those have yet more dependencies. 4:06 So here is where I say thank you Gradle for doing so 4:11 much with such little instruction. 4:14 Okay, we've got our local environment set up to start coding and 4:16 even our project created. 4:19 After a quick check for 4:21 understanding, we'll dive into the world of using Java to develop web applications. 4:22
You need to sign up for Treehouse in order to download course files.Sign up