1 00:00:00,570 --> 00:00:03,100 Cradle is an amazingly flexible build tool, and 2 00:00:03,100 --> 00:00:06,060 it's jam packed with all sorts of features. 3 00:00:06,060 --> 00:00:09,070 Build tools are used to automate the process of packaging up and 4 00:00:09,070 --> 00:00:11,420 shipping your code to your users. 5 00:00:11,420 --> 00:00:14,160 There are a lot of tasks that typically happen when you want to build 6 00:00:14,160 --> 00:00:15,310 your project. 7 00:00:15,310 --> 00:00:18,610 Tasks all the way from running unit tests to making sure that everything's working 8 00:00:18,610 --> 00:00:22,750 as intended, to creating JARs with version specific metadata. 9 00:00:22,750 --> 00:00:26,780 Now over time developers have learned that having a standardized build tool 10 00:00:26,780 --> 00:00:30,600 is an essential part of the development flow, especially when working with teams. 11 00:00:31,790 --> 00:00:34,385 You might have heard of some other build tools such as Maven or Ant, 12 00:00:34,385 --> 00:00:37,810 Gradle does a great job of interacting with those other build tools. 13 00:00:39,160 --> 00:00:43,370 We are going to be narrowing the scope of this workshop to managing dependencies. 14 00:00:43,370 --> 00:00:46,620 There's a lot of ground to cover here, so if you would like us to cover more, 15 00:00:46,620 --> 00:00:48,960 check the teacher's notes to see how to make requests and 16 00:00:48,960 --> 00:00:50,749 what other requests have been made by your fellow students. 17 00:00:52,970 --> 00:00:55,760 Gradle exposes a domain-specific language, or 18 00:00:55,760 --> 00:01:00,400 DSL, that is based heavily on the Groovy programming language. 19 00:01:00,400 --> 00:01:03,140 Groovy is a dynamic scripting language that runs on the JVM. 20 00:01:03,140 --> 00:01:07,560 You'll find it to be very expressive and legible, and 21 00:01:07,560 --> 00:01:10,560 probably quite familiar as it's very similar to Java. 22 00:01:12,340 --> 00:01:15,920 Other build tools have embraced XML as their primary language. 23 00:01:15,920 --> 00:01:17,800 By using the Gradle based DSL, 24 00:01:17,800 --> 00:01:21,060 it encourages everyone to get involved in scripting of the tasks. 25 00:01:22,240 --> 00:01:26,910 Gradle has an opinionated way of how things should be done and laid out. 26 00:01:26,910 --> 00:01:30,660 If you follow its conventions, you'll find things very easy to configure. 27 00:01:30,660 --> 00:01:33,230 And it also provides an easy path to customize it 28 00:01:33,230 --> 00:01:36,900 outside of the suggested patterns, it's the best of both worlds. 29 00:01:36,900 --> 00:01:39,205 Let's take a quick look at how to get started using Gradle. 30 00:01:39,205 --> 00:01:44,110 Okay, so the easiest way to create a new project that is all 31 00:01:44,110 --> 00:01:48,130 Gradle ready is by using the IntelliJ Gradle template. 32 00:01:48,130 --> 00:01:50,970 Let's create a new project that will exercise the comma separated 33 00:01:50,970 --> 00:01:52,959 value library as a way of testing the flow out. 34 00:01:52,959 --> 00:01:56,543 So let's make a little mock project that gathers reviews and 35 00:01:56,543 --> 00:02:00,720 then exports those reviews to spreadsheet format, CSV. 36 00:02:00,720 --> 00:02:03,330 First let's make a new project, so we'll click Create New Project. 37 00:02:03,330 --> 00:02:07,240 And then you'll see over here on the left, normally we leave this on Java, but 38 00:02:07,240 --> 00:02:09,650 let's scroll down here to where it's says Gradle. 39 00:02:09,650 --> 00:02:13,490 So we'll do this, let's choose that and we'll just go ahead leave 40 00:02:13,490 --> 00:02:16,180 the language that we're writing in is Java, so we will go ahead and click next. 41 00:02:17,680 --> 00:02:19,870 Now we'll will get some options presented to us and 42 00:02:19,870 --> 00:02:22,810 unfortunately there's not much direction here, huh? 43 00:02:22,810 --> 00:02:26,040 Now remember that Gradle is first and foremost, it's a build tool. 44 00:02:26,040 --> 00:02:31,740 So these prompts are specifically asking what you want to publish our project as. 45 00:02:31,740 --> 00:02:35,540 So group ID here follows the same packaging names that we've been doing 46 00:02:35,540 --> 00:02:37,140 with normal packages. 47 00:02:37,140 --> 00:02:40,390 So, this is kind of like what we want to say in our base packages. 48 00:02:40,390 --> 00:02:42,900 So let's go ahead and let's, we're making the review app, right? 49 00:02:42,900 --> 00:02:47,440 So we'll do com.teamtreehouse, and let's put it in the package called reviews. 50 00:02:48,680 --> 00:02:51,480 Artifact idea here is what you want to name 51 00:02:51,480 --> 00:02:53,650 the JAR file when it gets created, right? 52 00:02:53,650 --> 00:02:56,570 When it gets published, what do you want to JAR file to be named? 53 00:02:56,570 --> 00:03:00,330 So, it probably makes sense for us to call it reviews.jar. 54 00:03:00,330 --> 00:03:01,140 So let's call it reviews. 55 00:03:02,735 --> 00:03:05,400 Cool, the version here is something that we'll talk about more in the future. 56 00:03:05,400 --> 00:03:07,750 For now, let's just leave it as its default. 57 00:03:07,750 --> 00:03:09,260 So we'll click next. 58 00:03:09,260 --> 00:03:12,450 Okay, so now we have some more Gradle IDE options. 59 00:03:12,450 --> 00:03:16,430 If we want to check the box here for auto import, every time, what that means is 60 00:03:16,430 --> 00:03:19,410 every time that we change the file, the project will automatically refresh. 61 00:03:19,410 --> 00:03:20,890 We'll talk more about this here in a bit, 62 00:03:20,890 --> 00:03:23,610 it will just automatically do it without us specifically forcing it. 63 00:03:23,610 --> 00:03:27,360 I want to show you both ways, but for now, let's just leave that box unchecked. 64 00:03:27,360 --> 00:03:29,370 Now let's check this one, though, 65 00:03:29,370 --> 00:03:32,880 this create directories for empty content roots automatically. 66 00:03:32,880 --> 00:03:37,150 One of the specific ways that Gradle is opinionated is the way that 67 00:03:37,150 --> 00:03:41,420 it by default expects your projects to be laid out structure wise, like folders and 68 00:03:41,420 --> 00:03:42,470 directories. 69 00:03:42,470 --> 00:03:45,610 It's a little bit different actually than what we've been using in the past, so 70 00:03:45,610 --> 00:03:48,517 I want to check this and we'll make sure that we are going to set up the Gradle 71 00:03:48,517 --> 00:03:51,409 defaults in the projects, so we can kinda walk through them a little bit. 72 00:03:51,409 --> 00:03:54,831 You'll find that most Gradle projects and Maven projects for that matter, 73 00:03:54,831 --> 00:03:57,180 actually follow the same directory layout. 74 00:03:57,180 --> 00:04:01,455 Okay, so let's click next, and finally some normal 75 00:04:01,455 --> 00:04:05,680 IntelliJ questions that happen here, and the defaults are pretty sane, right? 76 00:04:05,680 --> 00:04:09,400 The project name is gonna be reviews, and we're gonna drop it in the same location. 77 00:04:09,400 --> 00:04:10,580 So let's click finish. 78 00:04:12,175 --> 00:04:14,510 Okay, and if this is the first time that gradle is being run, 79 00:04:14,510 --> 00:04:16,325 you'll see some downloading happen. 80 00:04:16,325 --> 00:04:19,980 IntelliJ will actually go out and download Gradle and then run some things. 81 00:04:19,980 --> 00:04:22,970 I've already run this Gradle before on this machine so it was pretty fast. 82 00:04:22,970 --> 00:04:25,236 It might take a bit longer on yours. 83 00:04:25,236 --> 00:04:28,410 So, once the download's done, it will go and 84 00:04:28,410 --> 00:04:32,340 create all the default files and folders that are expected by Gradle. 85 00:04:32,340 --> 00:04:34,510 So let's go ahead and open up the project structure over here. 86 00:04:34,510 --> 00:04:37,520 Let's open this up, our reviews project, and 87 00:04:37,520 --> 00:04:40,580 it ended up creating this build script for us which is called build.gradle. 88 00:04:40,580 --> 00:04:45,190 But first let's take a look here under the source directory, this SRC. 89 00:04:45,190 --> 00:04:47,850 So this is split up a little bit differently than we have seen in 90 00:04:47,850 --> 00:04:48,360 the past, right? 91 00:04:48,360 --> 00:04:51,340 Before we just had source and maybe it was marked as a source's root. 92 00:04:51,340 --> 00:04:53,520 So the source's root is a little bit deeper in here. 93 00:04:53,520 --> 00:04:55,899 Under main, you'll see that the source's root is Java and 94 00:04:55,899 --> 00:04:59,740 then there's also a little resources thing that's set up for us which is nice. 95 00:04:59,740 --> 00:05:02,280 And there's also a test directory, and then there's also another thing called 96 00:05:02,280 --> 00:05:04,850 Java here, and it's marked as the test route. 97 00:05:04,850 --> 00:05:07,640 So it's a little bit different of a layout, and this is definitely the Maven 98 00:05:07,640 --> 00:05:11,140 style of doing things, but Gradle follows this by default. 99 00:05:11,140 --> 00:05:13,300 So let's open up that build script that it built for us, and 100 00:05:13,300 --> 00:05:15,144 it's called build.gradle. 101 00:05:16,380 --> 00:05:19,770 There's an option here that will allow us to automatically get 102 00:05:19,770 --> 00:05:21,550 suggestions from the IDE. 103 00:05:21,550 --> 00:05:23,590 So let's go ahead, let's say okay apply this suggestion. 104 00:05:25,200 --> 00:05:27,700 You might see this message, lets feel free to click away from it. 105 00:05:28,840 --> 00:05:31,120 If you can look in here you see that this is the group that we define. 106 00:05:31,120 --> 00:05:33,110 Remember, we said com.treehouse.reviews. 107 00:05:33,110 --> 00:05:36,310 And this is the version that was there at the bottom, so that's there. 108 00:05:36,310 --> 00:05:40,710 There's also this thing here called a plug in, and it's got the Java plug in. 109 00:05:40,710 --> 00:05:43,030 And this is what's defining the layout of the folders and 110 00:05:43,030 --> 00:05:45,210 how to package things up and etc. 111 00:05:45,210 --> 00:05:47,700 Now, because we don't have anything really defined in here, 112 00:05:47,700 --> 00:05:49,180 it's just using the defaults, right? 113 00:05:49,180 --> 00:05:51,400 So we could definitely override the defaults but 114 00:05:51,400 --> 00:05:52,960 there's nothing that we need to here. 115 00:05:52,960 --> 00:05:55,660 So, there's different types of plugins for this. 116 00:05:55,660 --> 00:05:58,510 There's one for Android and it does all the building, packaging and 117 00:05:58,510 --> 00:06:01,500 versioning of making all of that Android magic happen. 118 00:06:01,500 --> 00:06:04,360 Also what I want to point out here is while this looks pretty clear, 119 00:06:04,360 --> 00:06:07,240 it's actually the Groovy programming language. 120 00:06:07,240 --> 00:06:09,430 It's pretty clear why they choose that right? 121 00:06:09,430 --> 00:06:12,040 This is really nice looking, it's pretty easy to read. 122 00:06:12,040 --> 00:06:15,050 But we aren't going to get into too much customization, 123 00:06:15,050 --> 00:06:17,920 I did want to point out that these are just methods sitting on the API. 124 00:06:17,920 --> 00:06:21,550 And this build.gradle file here is in the project context. 125 00:06:22,650 --> 00:06:27,320 So this repositories here, this is a method defined on the project. 126 00:06:27,320 --> 00:06:30,340 And it takes a closure, this is a closure here, 127 00:06:30,340 --> 00:06:32,800 as a parameter in the groovy language. 128 00:06:32,800 --> 00:06:35,880 So you could look at the API and see what the repositories were. 129 00:06:35,880 --> 00:06:38,850 You could find it in the reference documentation if you wanted to do more. 130 00:06:38,850 --> 00:06:41,500 I've added some in the teacher's notes, so go ahead and click through there and 131 00:06:41,500 --> 00:06:42,630 see if you want it. 132 00:06:42,630 --> 00:06:46,110 >> Now, don't worry if the syntax is weird when you actually can't read it. 133 00:06:46,110 --> 00:06:48,310 You will understand it if we spend some time on it. 134 00:06:48,310 --> 00:06:51,710 For now though, I just want you to appreciate the brevity. 135 00:06:51,710 --> 00:06:54,320 One more cool thing that I wanted to point out while we're here 136 00:06:54,320 --> 00:06:56,400 is that in the base directory here there's this file, 137 00:06:56,400 --> 00:06:59,790 there's these two files called gradlew and gradlew.bat. 138 00:06:59,790 --> 00:07:01,770 This is the Gradle wrapper. 139 00:07:01,770 --> 00:07:04,820 It provides some incredible self installing set up of Gradle for 140 00:07:04,820 --> 00:07:06,830 anybody who want's to get their hands on your project. 141 00:07:06,830 --> 00:07:10,414 And might not have IntelliJ idea or Gradle installed. 142 00:07:10,414 --> 00:07:13,440 [LAUGH] It's a pretty genius way of making sure that everyone can build and 143 00:07:13,440 --> 00:07:15,680 test your project the same way. 144 00:07:15,680 --> 00:07:18,440 You just download the code, and you kick off the script, 145 00:07:18,440 --> 00:07:20,750 and Gradle installs itself should it need to. 146 00:07:20,750 --> 00:07:22,350 It's pretty clever, right? 147 00:07:22,350 --> 00:07:24,250 I'll show off some more command line fun here in a bit. 148 00:07:25,400 --> 00:07:28,050 Awesome, now that we got your environment all set up, 149 00:07:28,050 --> 00:07:29,640 let's get on to the task at hand. 150 00:07:30,670 --> 00:07:33,780 Oh, one thing I wanted to add here, both Gradle and 151 00:07:33,780 --> 00:07:37,140 the Groovy Language are open source, pretty cool right? 152 00:07:37,140 --> 00:07:41,150 Again, don't worry if you didn't fully understand the scripting language 153 00:07:41,150 --> 00:07:42,190 in the build script. 154 00:07:42,190 --> 00:07:44,080 We won't be needing it just yet. 155 00:07:44,080 --> 00:07:47,260 And I'll cover what you need to know as we encounter it. 156 00:07:47,260 --> 00:07:51,230 If you'd like to learn more about Groovy, as always, check the teacher's notes. 157 00:07:51,230 --> 00:07:53,880 All right, let's get to managing those pesky dependencies