Introducing Gradle7:54 with Craig Dennis
Gradle is an amazingly flexible build tool and it is jam packed with all sorts of features.
Cradle is an amazingly flexible build tool, and 0:00 it's jam packed with all sorts of features. 0:03 Build tools are used to automate the process of packaging up and 0:06 shipping your code to your users. 0:09 There are a lot of tasks that typically happen when you want to build 0:11 your project. 0:14 Tasks all the way from running unit tests to making sure that everything's working 0:15 as intended, to creating JARs with version specific metadata. 0:18 Now over time developers have learned that having a standardized build tool 0:22 is an essential part of the development flow, especially when working with teams. 0:26 You might have heard of some other build tools such as Maven or Ant, 0:31 Gradle does a great job of interacting with those other build tools. 0:34 We are going to be narrowing the scope of this workshop to managing dependencies. 0:39 There's a lot of ground to cover here, so if you would like us to cover more, 0:43 check the teacher's notes to see how to make requests and 0:46 what other requests have been made by your fellow students. 0:48 Gradle exposes a domain-specific language, or 0:52 DSL, that is based heavily on the Groovy programming language. 0:55 Groovy is a dynamic scripting language that runs on the JVM. 1:00 You'll find it to be very expressive and legible, and 1:03 probably quite familiar as it's very similar to Java. 1:07 Other build tools have embraced XML as their primary language. 1:12 By using the Gradle based DSL, 1:15 it encourages everyone to get involved in scripting of the tasks. 1:17 Gradle has an opinionated way of how things should be done and laid out. 1:22 If you follow its conventions, you'll find things very easy to configure. 1:26 And it also provides an easy path to customize it 1:30 outside of the suggested patterns, it's the best of both worlds. 1:33 Let's take a quick look at how to get started using Gradle. 1:36 Okay, so the easiest way to create a new project that is all 1:39 Gradle ready is by using the IntelliJ Gradle template. 1:44 Let's create a new project that will exercise the comma separated 1:48 value library as a way of testing the flow out. 1:50 So let's make a little mock project that gathers reviews and 1:52 then exports those reviews to spreadsheet format, CSV. 1:56 First let's make a new project, so we'll click Create New Project. 2:00 And then you'll see over here on the left, normally we leave this on Java, but 2:03 let's scroll down here to where it's says Gradle. 2:07 So we'll do this, let's choose that and we'll just go ahead leave 2:09 the language that we're writing in is Java, so we will go ahead and click next. 2:13 Now we'll will get some options presented to us and 2:17 unfortunately there's not much direction here, huh? 2:19 Now remember that Gradle is first and foremost, it's a build tool. 2:22 So these prompts are specifically asking what you want to publish our project as. 2:26 So group ID here follows the same packaging names that we've been doing 2:31 with normal packages. 2:35 So, this is kind of like what we want to say in our base packages. 2:37 So let's go ahead and let's, we're making the review app, right? 2:40 So we'll do com.teamtreehouse, and let's put it in the package called reviews. 2:42 Artifact idea here is what you want to name 2:48 the JAR file when it gets created, right? 2:51 When it gets published, what do you want to JAR file to be named? 2:53 So, it probably makes sense for us to call it reviews.jar. 2:56 So let's call it reviews. 3:00 Cool, the version here is something that we'll talk about more in the future. 3:02 For now, let's just leave it as its default. 3:05 So we'll click next. 3:07 Okay, so now we have some more Gradle IDE options. 3:09 If we want to check the box here for auto import, every time, what that means is 3:12 every time that we change the file, the project will automatically refresh. 3:16 We'll talk more about this here in a bit, 3:19 it will just automatically do it without us specifically forcing it. 3:20 I want to show you both ways, but for now, let's just leave that box unchecked. 3:23 Now let's check this one, though, 3:27 this create directories for empty content roots automatically. 3:29 One of the specific ways that Gradle is opinionated is the way that 3:32 it by default expects your projects to be laid out structure wise, like folders and 3:37 directories. 3:41 It's a little bit different actually than what we've been using in the past, so 3:42 I want to check this and we'll make sure that we are going to set up the Gradle 3:45 defaults in the projects, so we can kinda walk through them a little bit. 3:48 You'll find that most Gradle projects and Maven projects for that matter, 3:51 actually follow the same directory layout. 3:54 Okay, so let's click next, and finally some normal 3:57 IntelliJ questions that happen here, and the defaults are pretty sane, right? 4:01 The project name is gonna be reviews, and we're gonna drop it in the same location. 4:05 So let's click finish. 4:09 Okay, and if this is the first time that gradle is being run, 4:12 you'll see some downloading happen. 4:14 IntelliJ will actually go out and download Gradle and then run some things. 4:16 I've already run this Gradle before on this machine so it was pretty fast. 4:19 It might take a bit longer on yours. 4:22 So, once the download's done, it will go and 4:25 create all the default files and folders that are expected by Gradle. 4:28 So let's go ahead and open up the project structure over here. 4:32 Let's open this up, our reviews project, and 4:34 it ended up creating this build script for us which is called build.gradle. 4:37 But first let's take a look here under the source directory, this SRC. 4:40 So this is split up a little bit differently than we have seen in 4:45 the past, right? 4:47 Before we just had source and maybe it was marked as a source's root. 4:48 So the source's root is a little bit deeper in here. 4:51 Under main, you'll see that the source's root is Java and 4:53 then there's also a little resources thing that's set up for us which is nice. 4:55 And there's also a test directory, and then there's also another thing called 4:59 Java here, and it's marked as the test route. 5:02 So it's a little bit different of a layout, and this is definitely the Maven 5:04 style of doing things, but Gradle follows this by default. 5:07 So let's open up that build script that it built for us, and 5:11 it's called build.gradle. 5:13 There's an option here that will allow us to automatically get 5:16 suggestions from the IDE. 5:19 So let's go ahead, let's say okay apply this suggestion. 5:21 You might see this message, lets feel free to click away from it. 5:25 If you can look in here you see that this is the group that we define. 5:28 Remember, we said com.treehouse.reviews. 5:31 And this is the version that was there at the bottom, so that's there. 5:33 There's also this thing here called a plug in, and it's got the Java plug in. 5:36 And this is what's defining the layout of the folders and 5:40 how to package things up and etc. 5:43 Now, because we don't have anything really defined in here, 5:45 it's just using the defaults, right? 5:47 So we could definitely override the defaults but 5:49 there's nothing that we need to here. 5:51 So, there's different types of plugins for this. 5:52 There's one for Android and it does all the building, packaging and 5:55 versioning of making all of that Android magic happen. 5:58 Also what I want to point out here is while this looks pretty clear, 6:01 it's actually the Groovy programming language. 6:04 It's pretty clear why they choose that right? 6:07 This is really nice looking, it's pretty easy to read. 6:09 But we aren't going to get into too much customization, 6:12 I did want to point out that these are just methods sitting on the API. 6:15 And this build.gradle file here is in the project context. 6:17 So this repositories here, this is a method defined on the project. 6:22 And it takes a closure, this is a closure here, 6:27 as a parameter in the groovy language. 6:30 So you could look at the API and see what the repositories were. 6:32 You could find it in the reference documentation if you wanted to do more. 6:35 I've added some in the teacher's notes, so go ahead and click through there and 6:38 see if you want it. 6:41 >> Now, don't worry if the syntax is weird when you actually can't read it. 6:42 You will understand it if we spend some time on it. 6:46 For now though, I just want you to appreciate the brevity. 6:48 One more cool thing that I wanted to point out while we're here 6:51 is that in the base directory here there's this file, 6:54 there's these two files called gradlew and gradlew.bat. 6:56 This is the Gradle wrapper. 6:59 It provides some incredible self installing set up of Gradle for 7:01 anybody who want's to get their hands on your project. 7:04 And might not have IntelliJ idea or Gradle installed. 7:06 [LAUGH] It's a pretty genius way of making sure that everyone can build and 7:10 test your project the same way. 7:13 You just download the code, and you kick off the script, 7:15 and Gradle installs itself should it need to. 7:18 It's pretty clever, right? 7:20 I'll show off some more command line fun here in a bit. 7:22 Awesome, now that we got your environment all set up, 7:25 let's get on to the task at hand. 7:28 Oh, one thing I wanted to add here, both Gradle and 7:30 the Groovy Language are open source, pretty cool right? 7:33 Again, don't worry if you didn't fully understand the scripting language 7:37 in the build script. 7:41 We won't be needing it just yet. 7:42 And I'll cover what you need to know as we encounter it. 7:44 If you'd like to learn more about Groovy, as always, check the teacher's notes. 7:47 All right, let's get to managing those pesky dependencies 7:51
You need to sign up for Treehouse in order to download course files.Sign up