Gradle is written in the Groovy programming language, a Java JVM language. You can also use Groovy to extend and customize Gradle. You don't need to understand the entire Groovy language for Gradle but you should learn just enough to read and customize the Gradle build files successfully. Groovy is intended to be especially easy for Java developers to learn.
- [Instructor] In this video, we'll learn enough Groovy for Gradle, and just enough. So, as you might recall from earlier videos, we use the Groovy language to actually write the build.gradle file, so it'll be helpful if we learn enough of the Groovy language so that we can understand the Gradle build file and kind of understand how to modify it. We'll see how to run Groovy in the Gradle console in IntelliJ so we can actually run a little bit of Groovy outside of Gradle. We'll see that one of the main advantages of Groovy over Java is its simplified syntax.
We'll also see that there's something called Groovy closures, which gives us a nice way of writing Groovy. That's something that has been introduced in Java 8, it's referred to as lambdas in Java 8, but in Groovy it's referred to as closures. Let's look at some code. I'm in IntelliJ, and if you're starting at this point, you can actually load the starting point from the exercise files or just continue on from the prior video. I'm going to open something called the Groovy console, and I'll do that by selecting Tools at the top-level menu.
Near the bottom is the Groovy console item. That opens a blank area in the middle of the screen along with an output area at the bottom of the screen, and you'll notice that it says in the tab, Bundled Groovy 2.4.6, so we're actually running that version of Groovy. Now, let me do something here that we might do in Java. Remember in Java we often do a Hello World program where we print out Hello World? Well, I'll try that. System.out.println, Hello World, and I'll put a semicolon at the end.
I can run this by selecting the play icon, which is just above my text area and to the left, and it's a green play button. I can click on that or I can use my shortcut, command enter. So I'll run this program, and you'll notice that down in the output section you can see the word result, which is put in by Groovy automatically, and then the string Hello World. So a couple things I want to show you here. Notice that we didn't have to create a whole class and a main method, so that was a big improvement, already.
Also, a couple of things, Groovy automatically imports system.out, so I don't have to put that there. I can remove that. Let me run this again by using command enter, and I get the same output again. Also, Groovy doesn't require a semicolon at the end, so I can take that off and run it again and get Hello World. Also, for method calls where we have a single argument, we don't have to actually even put parentheses. So I'll take the parentheses out and I'll use my shortcut key, command return, and run it again, and the same output is there.
So that's the first point I want to make about Groovy, is that it's a much more simple syntax than regular Java. Now, it is still being compiled down to Java bytecode and running into JVM, which is why it's sometimes referred to as a JVM language, so it's very easy to interoperate it with regular Java, but it's nice to be able to use the very simple syntax. Here's another thing that we can do. I'm going to create a class definition, so I'll use class, MyClass, and you'll notice I don't have to use the same modifiers that I do in Java.
I'm going to create a method here on line three called doSomething, so to return void it'll doSomething, and it'll take an argument of type closure, which, again, if you're a Java developer this may be something that's new to you if you haven't looked at Java 1.8 yet. So what a closure is, think of that as a bit of code, and now I'll run that bit of code that gets passed in using closure.call here on line four.
So I've created a class definition. Now, after line six, I'm going to create a new object from that class definition. My object equals new MyClass. And again, I don't have to type it the way I do in Java, I don't have to put semicolons, so if you're a Java developer this looks a little bit strange, but this simplified syntax really makes for writing less code. Okay, now let's run that doSomething method. How do we normally do that? We use the object reference and doSomething.
What does this thing do? It's asking us to pass something in. We're going to pass in some code. So I will use, in this case, the squiggly braces, and I will pass in a println statement, and we'll print maybe the new date, and you'll notice I don't have to do any imports, that is done automatically. Let me now run this using my shortcut command return, and you'll notice here at the bottom it prints the date.
So let's review this a little bit, looks a little bit strange. We can define a class, as we're doing in line one, and define methods, as we're doing in line three, that take code at run time. So in line eight, we're creating a new instance of the object, and then in line 10 we're doing two things. We're running the doSomething method, but we're passing in a closure, a bit of code, as the argument. So we don't have to decide what code runs at the time we define the class, we can do it at the time we run the method.
So very strange, but closures are an important part of Groovy, and kind of having a little bit of an understanding for that will allow us to look at the build.gradle files and make a little bit more sense of them.
- The purpose of Gradle
- Building a Java project with Gradle
- Adding a dependency to a project
- Creating a library module
- The Gradle project structure
- Creating new Gradle tasks
- Profiling and using the Gradle cloud