Using Dependencies7:48 with Craig Dennis
So now that you have the dependencies, how do you use them?
Okay, so now that we have our dependencies defined and 0:00 we've run the Gradle command to download them, it's actually really simple to use. 0:04 You just import it to a file and use it. 0:08 What might not have been clear is that as long as you've checked in your 0:11 Build Script using Git or another version control tool, Anyone who has a copy 0:14 of your source code can now run the Build command on their computer and 0:19 get the same version dependency that you specified. 0:22 Pretty cool, right? 0:25 No need to check in the JAR files, 0:26 because they can get installed the same way that we did. 0:28 So this opens up a whole new world of APIs to us, right? 0:31 Let's first go play with our new library, and then poke around and 0:35 see what else is out there. 0:38 Okay, so let's make a new class in our source main Java directory here, 0:41 let's make a new class, and let's call it, main right? 0:46 We'll make the first file just to play around, so 0:49 we'll call it com.team treehouse.reviews.Main. 0:54 And then we'll click okay. 0:59 I have set up some version control here, so I'm just gonna go ahead and 1:03 automatically check that in. 1:06 Okay so, here's our Main class here and 1:07 let's just write up a quick prove of concept. 1:10 Let's imagine that our app allowed reviews of different code snippets. 1:13 So what we want here is we wanna provide a way to export the data, those reviews, and 1:18 then later import it into a spreadsheet to make further graphs and things. 1:23 So this is actually a pretty common request, and 1:28 the good news is we really don't have to do that much, right? 1:30 Cuz we already have this library. 1:33 So let's do this, let's just go ahead and 1:34 let's start out with our public static void main method. 1:36 And let's just start talking about the CSVPrinter object. 1:42 So the way that you do that is you just start typing, right? 1:46 So CSVPrinter, oh there it is, that's what we want, CSV Printer, okay? 1:48 And we'll call that a printer, and we'll make a new one of them, 1:54 and I'm not exactly sure what that is, but let's just see what it comes back with, so 1:58 it says it needs an appendable called out. 2:03 So this is just a proof of concept but 2:05 I happen to know that the system.app where we normally print the console. 2:07 That implements the appendable interface, so let's do that, so 2:10 system.out is the first parameter here and the next thing I was looking for 2:14 was a format, right? 2:19 So it's a CSV format and we'll make it excel format, 2:21 cuz that's what they're gonna be looking at here. 2:24 Right, so we got a little red mark here, let's see why. 2:27 Let's surround that with a try and a catch. 2:31 We'll just leave that as the default here, this is again just a quick little mock up. 2:33 Okay so let's add, let's write this stuff out. 2:37 So I'm gonna type printer.printRecord, I saw that that was available, right? 2:40 I saw that was available when I was looking at the documentation. 2:46 But I don't remember exactly how to use it, so now I could press F1, and 2:49 look at that, all the help is here because it's in the Java doc. 2:52 Okay, so we just, we put in the different values that we want, and 2:56 it takes in an endless amount of objects. 2:59 So it'll print, oh that's cool, so I can print out, 3:01 not only can I print out the string. 3:04 So let's say that we had, we'll just make a couple of fake reviews here. 3:06 So I totally thought that that code was five and I said that, I love it, 3:11 five out of five, right? 3:14 And then let's do another one and 3:16 we'll do printRecord, and we'll say 3:20 that Chris, our other Java teacher here. 3:25 If you haven't seen his stuff yet, it's great. 3:30 He gave it a 4 though, and he says pretty good. 3:32 Would be better with an annotation or two. 3:38 I noticed he used a comma in his review, and that's kind of the edge case that we 3:45 were talking about, that was a problem before right? 3:48 So, the other thing that's kind of cool that I also wanted to show you real quick, 3:52 is that you can also navigate to the source, right? 3:56 So if I Cmd+B that or Ctrl+B. 3:59 I can actually look and 4:01 see how the codes happening because it's an open source, pretty crazy right? 4:02 All right, so 4:06 we're back here, we're in our file, it automatically gets those imports for me. 4:07 I don't know if you saw that pop up. 4:11 So I think if we scroll down here and 4:13 we run it we should be all good, let's see what happens. 4:17 We'll say run, we'll run main. 4:23 I forgot a semicolon, there we go. 4:30 Now let's go ahead and run that. 4:35 Here we go, see, it's comma separated and notice that when the comma happens, 4:37 it put it in quotes. 4:41 This is how Excel format allows you to have the separator inside the text, but 4:42 this did it for us. 4:46 Note how it knew about strings and numbers, 4:48 that we didn't have to think about any of that. 4:51 It just did it all for us. 4:52 Just a few lines of code, right? 4:54 This is just the beginning of your open source adventures. 4:56 So this example that I kinda walked you through works pretty well if you know 4:59 exactly what you're looking for. 5:03 And I kinda did, right, I showed you this library. 5:04 But what if we're looking for something less specific. 5:07 For example, let's think about adding some more cool features to our project. 5:09 I think it would be great if we could go to any website and 5:13 pull information from it. 5:16 Like with these code snippets that are there. 5:17 Let's go to a website, pull that code snippet down if it didn't have API. 5:18 So now I could probably write this myself right? 5:22 I understand how HTML tags work and everything like that. 5:24 But first, I think we should check if something else is out there 5:28 that does this. 5:30 So, what I would do is I would search and 5:32 say how do you do this and then I come across some technology. 5:33 Now it might not be specifically in my language, and, so it says, oh, 5:36 it sounds like you are talking about an HTML parser or something along those line. 5:40 So that is what I want, so 5:45 now logically you'd think that I can just go search Maven for that right? 5:47 So let's try that, let's go over here, this is search.maven.org. 5:50 So I'm going to search for html parser. 5:54 That comes back with something. 5:59 Oh, it says it hasn't been updated since 2010, 6:01 the last html parser written was in 2010. 6:04 Not true, this search is a little bit off. 6:08 Now, your other favorite java teacher, Chris, wanted to make sure 6:11 that I shared his technique with how he does things like this. 6:16 So what Chris does is he goes to Google and he searches just for Maven. 6:19 And then what are you searching for? 6:25 So, html parsers, so he does that. 6:26 And you'll see that there's this great thing here called www.mvmrepository.com. 6:29 Now mvm is the word for Maven shortcut, that's kind of how it goes. 6:32 This is a little open source website that indexes the Maven site. 6:36 So here is one, htmlparser.jar. 6:41 We could take a look at it if we wanted to, and 6:44 see that it was created on 2011, that's okay. 6:48 Let's go ahead and let's search this site, this site looks pretty good. 6:53 It may have it in repositories, so HTML parser. 6:55 Oh here's one called Jsoup and it shows you how many people are using it, 335. 6:57 So that looks pretty nice. 7:01 So open this up, and here, oh, August 2015, that's great. 7:03 It's been updated recently, I think we should use this one. 7:06 Let's use J suit. 7:09 So I'd use Jsoup, and that's how right? 7:10 I'd just come over back to our dependencies and 7:12 I'd pop it in our deps right here. 7:14 Deps is the cool way to say dependencies. 7:16 Also remember that the majority of active open source project code is actually now 7:18 on GitHub and GitHub uses a thing for stars right? 7:23 So you can come in and you can search for language colon Java or 7:26 whatever they're over here on the side too. 7:29 And if you sort by most stars, you'll see the most popular. 7:32 And you can cruise around here and see what's out, and see what you find and 7:35 share it in the community with us. 7:38 And also, don't forget that when you see these, you can contribute. 7:40 If you see a spelling error, fix it. 7:44 You can do that. 7:46 That's a different lesson, though. 7:47
You need to sign up for Treehouse in order to download course files.Sign up