Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, I'll show you how to use a powerful build tool called Gradle to manage the dependencies (libraries) that your project relies on.
Dependency Management with Gradle
If you haven't already, carve out 30 minutes for Craig to introduce you to managing dependencies with Gradle. Here is a link to the workshop:
After getting a decent grasp of how the
libraries will be using our bundled as jar
0:00
files, let's now turn to how and where
we're going to obtain those libraries.
0:04
And how we'll get them
into the class path.
0:09
Now, we could just do a simple web search
for a specific jar file we want to use,
0:12
download it, then use an IDE option
to add it to the class path.
0:18
If you search for
instructions on how to do this for
0:23
a specific library, the Spring Web
library, for example, you might be
0:25
surprised to find little guidance on where
to manually download this .jar file.
0:29
Even trickier is that if you were
to download a .jar file, and
0:34
successfully add it to the class path,
you might find that the .jar
0:38
file you downloaded depends on yet
another .jar file.
0:41
Now you have to do it all over again.
0:45
And probably not just once,
but likely many more times.
0:47
And if you ever wanted to share
your project with other developers,
0:50
it'd have to go through the same process.
0:54
If that isn't enough, what happens
when you update your application and
0:56
some libraries are left behind
while new libraries are added?
1:00
Because of this,
most developers use special tools for
1:05
managing what are called dependencies.
1:08
Dependencies of a project are libraries
that a project depends on
1:11
in order to function.
1:14
There are a couple big names out
there in dependency management tools,
1:16
namely Maven and Gradle.
1:19
We're going to use Gradle to manage
our dependencies in this course.
1:22
Fortunately this tool is
integrated with IntelliJ, so
1:26
you should be able to use it without
installing Gradle separately.
1:29
Using what's called a Gradle build file,
we'll be able to list
1:33
the names of the libraries that we
want to incorporate into our project.
1:37
Then, with a click of a button, Gradle
will download all of the .jar files for
1:40
the libraries we listed and even
the libraries our listed ones depend on.
1:45
It's much easier, quicker, and more
reliable than the manual download method.
1:50
I don't know about you, but
I'm itching to code a little bit.
1:56
Remember how we plan on building
an animated gif library called gif lib?
1:59
Let's get that project created in
IntelliJ and see Gradle in action.
2:03
To get started let's open IntelliJ and
create a new project.
2:08
I'll choose Create New Project
from the screen that pops up here.
2:12
From the left hand side,
we'll choose Gradle.
2:17
And with Java selected, we'll click Next.
2:19
The GroupId field should be
formatted like a root package name.
2:23
I'll use com.teamtreehouse.
2:27
The ArtifactId should be an identifier or
project name.
2:31
I'll use giflib.
2:34
And then we shall click Next.
2:37
In the next window,
we'll accept all the defaults.
2:40
Notice that the recommended option here
is to use the default Gradle wrapper,
2:43
which means Gradle as a program will
be included as part of our project.
2:47
Click Next and verify the name and
location of your project.
2:51
When you're ready to create it,
click Finish.
2:55
Let's expand our project, and
take a look at what we have.
2:58
Know that within a few moments, you'll
see your project directory expand as
3:02
the Gradle tool is downloaded and
added to our project directory.
3:06
The first file we'll look at,
is the .iml file.
3:11
As you may know, IntelliJ uses .iml
files to manage module settings.
3:14
Though we're seeing,
we won't actually edit this file, so
3:19
I will go ahead and close it.
3:22
The settings.gradle file simply
contains our root project
3:24
name which we had entered as giflib.
3:28
We also will not edit
this file in this course.
3:30
So, I'll go ahead and close that one too.
3:33
The one file we will concern ourselves
with is the build.gradle file,
3:35
this is the file I referenced in
a previous video where we can list our
3:40
projects dependencies and
Gradle will download them for us.
3:43
Let's check out a couple of
things in the build file.
3:46
I'll go ahead and
hide the tip that's come up in IntelliJ.
3:50
At the top, you see the group inversion
we specified when creating the project.
3:53
For now, suffice it to say, that the group
inversion allow repositories such as
3:57
the one we'll be using
to get our dependencies
4:01
to group related libraries together and
track versions.
4:04
Though I'll leave it alone,
you can change this at any time you like.
4:08
The apply plug-in line tells Gradle to
4:11
add pre-defined functionality when
using Gradle to build our project.
4:14
This particular functionality has
been given the short name Java.
4:18
The repository's blog
allows us to define one or
4:22
more locations where Gradle should
look for the libraries we list.
4:26
We'll use one of the largest public
dependency repositories called
4:29
mavenCentral.
4:33
Finally, we see now where we'll actually
list the libraries that we'll need.
4:35
It's called, quite intuitively,
the dependencies block.
4:39
In the next video we'll replace what's
listed and see Gradle in action.
4:43
You need to sign up for Treehouse in order to download course files.
Sign up