Dependency Management8:44 with Chris Ramacciotti
One of the main purposes of a build tool is dependency management. This video discusses how to add external libraries to your projects with Maven.
A dependency is a Java library that this project depends on. 0:00 While developing Java applications, you'll almost always be using other developers 0:04 libraries, in addition to your own. 0:08 So if you want to use code that isn't a part of the Java core library, 0:10 you'll need to add that library as a dependency. 0:14 Dependencies and Maven are declared inside this dependencies element. 0:17 With each dependency represented by its own dependency element. 0:20 Naming follows the GAV convention which stands for Group Artifact and Version. 0:25 Remember, this application is going to spy on a directory in our file system. 0:31 And alert us when files of a certain type are added to the directory. 0:35 Detecting the file's type can be a cumbersome process. 0:39 And at the very least, it's a problem that other developers have already tackled, so 0:41 let's use their code. 0:45 Hey, remember that Apache Tika library we found on Maven central in this last video? 0:47 Guess what? 0:51 We're gonna use it. 0:52 So let me click on this version, I'll copy this dependency element here. 0:53 And I'm going to paste it into my palm, under the dependencies element, great. 0:59 Now, when we write our code and compile with Maven, 1:05 we'll be able to reference the classes provided by Apache Tika. 1:07 Let's write our code now in app.java. 1:11 I'm gonna wipe out most of this class and start from scratch. 1:15 Okay, let's start with a couple import statements. 1:19 First, we're gonna import java.nio.file and 1:21 I'm gonna import all classes in that file package. 1:25 And this java.nio package stands for 1:30 Java's non-blocking input/output API, more on that in just a bit. 1:32 The next thing I wanna import is org.apache.tika.Tika; class. 1:38 Now, you'll notice that Intellige can't find this Apache package. 1:42 Though this won't be a problem for Maven when we compile, 1:50 my IDE will keep complaining. 1:53 To fix this and more fully utilize my IDE for coding, 1:55 I'll pop open the Maven Projects window here. 1:58 And then, I'll click that refresh icon. 2:02 After that, my IDE is happy as you can see. 2:06 Be sure to check out the options in your own IDE's Maven 2:10 tool window to see what's available. 2:12 But for this workshop, we'll stick with the command line for all Maven commands. 2:14 Okay, let's continue coding our class. 2:19 This class is called, public class App. 2:21 And I'm gonna drop two constants at the top of the class here for 2:27 the file type and the directory we want to watch. 2:30 So private static final String and I'll call it FILE_TYPE, 2:33 and I'll say text/csv files, cool. 2:38 And then, I'll do the same for the directory to watch 2:42 private static file String and all say DIR_TO_WATCH. 2:47 And also a /Users/Chris/Downloads and I made a tmp directory there. 2:52 You can change this to any empty folder on your own system, cool. 2:59 And then, I wanna public static void main method here, awesome. 3:04 Now ,the code we're gonna write here using the Java NIO, might look a little cryptic. 3:08 I'm certainly not asking you to have a complete understanding of how to use this 3:13 non-blocking input/output API provided by the Java core library. 3:16 Our work here is more about understanding how to package the project 3:22 into a distributable jar using Maven. 3:25 In any case, it's a nice opportunity for 3:27 us to look at Java features you may not have seen. 3:29 So we'll start by defining a path object that contains the directory to watch, 3:32 as well as a Tika object for detecting files. 3:37 So I say, path dir = Paths.get and then, 3:39 I'll say, (DIR_TO_WATCH), cool. 3:43 And I'll define a Tika object and call its default constructor. 3:47 Next, lets add our watch service which will allow us to spy on the directory, so 3:53 WatchService. 3:58 I'll just say watchService = FileSystems.getDefault. 3:59 It gets the current file system, newWatchService, cool. 4:07 And then, I'm going to register this watch service with a directory. 4:11 So dir.register(watchService,) and 4:15 I want to register it for events, for creating new files. 4:19 So I only wanna detect events for when new files are added to this directory. 4:25 So I'll say, ENTRY_CREATE. 4:30 Now, this is a constant that comes from a certain class. 4:34 So let's import that, 4:39 .StandardWatchEventKinds.ENTRY_CREATE;, cool. 4:41 Now, what you're probably going to see at this point are a bunch of warnings saying, 4:46 that you have on caught exceptions. 4:51 So I'm gonna do something here which I would normally do on a distributed Java 4:54 application. 4:58 But I'll do it here for purposes of brevity. 4:58 I'm just going to say throws Exception and that will silence the compiler, cool. 5:00 Now, we can move on. 5:06 Okay, let's start our event loop which will continually run until we receive 5:08 an invalid watch key. 5:12 So I'll define a WatchKey called key, and then, I'll define as do while loop. 5:13 while(key.reset());, this will loop as long as the key is valid. 5:20 Now, you'll see this little red squiggly here, until we assign key a value. 5:28 But we're gonna do that inside the loop. 5:32 Let's go ahead and do that. 5:35 So now, this watch key is an object that 5:37 represents the registration of our path object with the watch service. 5:40 When an event occurs, a key will be available through the watch service, 5:43 through a call to its take method. 5:47 So that's what I will assign this key variable, watchService.take(). 5:49 Now, at this point in our code, we need to loop through any events that come through. 5:55 And instead of using a for loop here which we could do. 6:00 We'll use streams to access the events, we'll call the poll of events method and 6:02 examine the stream there. 6:07 So I'll say, key.pollEvents().stream(). 6:08 And since, we only care about the create events for a certain file type. 6:12 Let's filter our stream().filter and that will accept an event object. 6:15 And it will return true, if we want the item to be included in our stream or 6:21 false, if we don't. 6:25 So in general, what we need to do here is return true when 6:28 the file type equals our constant FILE_TYPE, up here. 6:30 Looks like a misspelled it, FILE, there we go. 6:35 So we wanna return true, when the file associated with 6:40 this event is of this file type and false, otherwise. 6:44 So let's start with that code and work backwards. 6:49 So I will say, return FILE_TYPE.equals, and I'll say, (type). 6:53 Well, this must mean, we need a variable declared as type before this line of code, 6:59 let's do that. 7:04 How are we going to get that, well, String type = tika., 7:05 this is where that library comes in handy. 7:09 (filename.toString()) but I don't have a filename yet, 7:12 so how am I gonna get the file name of the file associated with this event e? 7:17 Well, here's how you can do that. 7:22 We'll say, Path filename =, I'm gonna cast to 7:23 a path object to e.context();, just like that. 7:28 All right, now, you might get a warning like I am, 7:35 that Lambda Expressions are not supported it's language level. 7:38 Well, let's change the language level to eight in our IDE. 7:41 Okay, now that error is gone. 7:47 That's not needed for Maven but just needed for our IDE. 7:49 So now, we have a filtered stream, cool. 7:53 Now, what do we wanna do with each one of these events? 7:55 Let's use the forEach method to perform an action on each one of these events. 7:57 So we'll say, forEach and then an event will be the parameter. 8:02 And what do I wanna do, I'll do a single line here. 8:07 That way, I don't have to enclose it in curly braces. 8:11 I'll do System.out.println, now, I'll say printf. 8:13 And I'll save file found, and I'll drop the name of the file. 8:18 And then, I'll drop a new line there, cool. 8:22 And e.context()), will give me that filename. 8:25 Oops, don't need a semicolon because I haven't used the curly braces here, 8:30 Eexcellent. 8:32 Now, with our code in place, 8:34 we're ready to start running Maven commands from the terminal. 8:35 So next, we'll learn about Maven build life cycles. 8:38 And we'll begin to build our app through Maven commands. 8:41
You need to sign up for Treehouse in order to download course files.Sign up