Let's check out the fundamentals of Groovy so
we can better understand what's going on in Gradle build scripts.
Before we get started looking at code, a pet peeve of mine in online courses
is when you have to constantly pause the video to copy down code.
You don't have to do that in this course.
All the code we'll demo is included in the course GitHub repository.
Groovy exists to fill a hole for
Java developers who need a scripting language.
It's terse, expressive and it operates extremely well with Java and
has some special features that make it ideally suited to creating domain
specific languages.
Gradle even provides it's own Groovy distribution so
we don't even need to install Groovy.
We can just put our Groovy code in a build doc Gradle file and
ask Gradle to do any task that uses the file.
To start off with, we'll declare a task called Groovy that doesn't do anything.
We'll be talking about this syntax in a minute.
Don't worry about it for now.
All this is doing is just telling Gradle to go ahead and load this file.
If we go ahead and run that task by running gradle groovy, what we see is
that Gradle runs the Groovy task but doesn't really do anything.
That's what we expected.
All right, let's dig into Groovy, and you know where we have to start, right?
Of course we gotta start with hello world.
Let's try running this build script again and see what happens.
All right, we see our message, we've successfully reached hello world.
Let's do something more interesting.
Most Java is also valid Groovy.
Let's test that out by creating a very simple JavaGreeter class,
just as one method to say hello.
And prints hello Java to standard out.
If we create an instance of that class and
then tell it to say hello let's see what happens.
Remember this is Groovy not Java.
Let's run our gradle groovy task again and there we go.
Hello Java.
Gradle is an optionally typed language.
That means that you can give all of your variables types if you want to, but if
you don't feel like it you can declare variables just using the def keyword.
In this case, we're just declaring a variable called foo with value 6.5.
We can also execute simple groovy code inside a string,
by creating a block of curly braces preceded by a dollar sign.
While dynamic typing is really nice,
sometimes it is good to know the class of a variable.
We can do that simply by calling dot class on any variable.
Finally, dynamic typing also allows us to reassign
a variable to a different type.
This isn't very good practice but it's good to show off.
All right, let's give this code a try, awesome it looks like foo has value 6.5,
yep that's what we expect down at this line.
Looks like five plus six does indeed equal 11.
Looks like Groovy was nice enough to do this math for us.
And then finally it looks like foo is of class Java math big decimal when it
has a value of 6.5.
But then when we reassign foo to a string it
looks like foo now has the class java.lang.string.
Let's have a look at how to declare functions in Groovy.
It looks somewhat java-like but note the def out front instead of a return type.
Also we don't need a return statement.
The last expression in the function block is what's returned.
So this function takes its input and doubles it.
We can call it with a number, however, since plus is overloaded in Groovy,
we can also call it with a string.
Let's check out the output of these invocations of doubleIt.
All right, looks like doubleIt, with the argument five gives us ten and
doubleIt with the argument the string foobar, gives us foobarfoobar.
Fantastic.
There's one more super cool feature of Groovy, and
that is whenever a function is called with at least one argument and
it's unambiguous, you can usually leave off the parentheses.
Let's give that a try.
I've set us up with three functions here, one of which no args,
one with one arg, and one with two args.
Let's see what we can do.
If we go ahead and call our one arg function with some argument and
we don't put any parentheses around it, well, let's see what happens.
Hey, we successfully called the function.
All right.
Now let's give the two argument function a try.
Note, we still have to separate the arguments with a comma.
All right, let's see how it goes.
Beautiful, no parentheses, still getting a function call.
Let's see if we can pull the same trick with the no argument function.
Looks like something exploded.
It turns out, you can't use no parentheses when you're
invoking a function that doesn't have any arguments.
Otherwise Groovy is confused about what's going on.
All right.
Let's try something real hard now.
We're doing a nested function call.
So we've got two args.
And then the first arg, 22 args is one arg past 500.
And then 200 is going to be the second arg, for two args.
You think this is going to work?
Let's give it a try.
Hm. Looks like something broke.
Looks like there's something weird going on in here.
Turns out this is actually ambiguous.
It's not clear to Groovy whether you want to pass two arguments to one arg or
what's going on here.
Fortunately, we can make this unambiguous with
just a couple of parentheses around 500.
Now that we've added those parentheses, let's see if it works now.
[SOUND] Beautiful,
we're getting the nested function call exactly how we want it.
First the one arg function is being called then the two arg function is
being called.