Gradle is an extremely powerful build tool that leaves the full power of groovy at your disposal. This power allows experienced developers to make their lives a lot easier, however, it can sometimes make it hard get down the basics. If you are anything like me, when you start using Gradle you will be a bit overwhelmed, and upon research, you will find getting started tutorials that spend a lot of time talking about cool features that add complexity to the so-called basics.

Who should read this?

Having said that, this tutorial is going to be targeted at the absolute beginner who wants to understand single project dependencies. It can also be helpful for people in the position I was in about a year ago, able to get applications building using tutorials but not understanding everything that I was doing. When “it” clicked for me, I was mad it took me so long since I now understood how simple dependency management in Gradle is. Hopefully, this tutorial will help you skip that step.

Basic Concepts of Gradle Dependencies

When dealing with Gradle dependencies, you should ask yourself two questions:

What artifacts do I need?

Where can I find them?

What artifacts do I need?

If you are trying to work with Gradle dependencies, it means that you need access to classes or artifacts that are not included in your project’s source code. In order to retrieve artifacts, Gradle needs a way to uniquely identify them. In order to do this, Gradle uses groups, names, and versions. Groups look a lot like Java packages and are used to relate artifacts used for similar purposes. Names and versions are kind of self-explanatory, they identify the name and version of the artifact you want to use. In order to import an artifact and use it, you will need to know all three. I will go over how to find these later.

Where can I find them?

Artifacts can be stored locally or remotely in maven repositories, or in other projects of a multi-project Gradle setup (not in the scope of this tutorial). Maven repositories are basically directories that contain artifacts and their metadata. One of the simplest ways to get started is to use the Maven Central repository which holds most of the third party artifacts you would need. It will even allow you to search on their website to find the group, name, and version of the artifact you want to import.

Ok, but how do I convey this in a build script?

Once you have the answers to both these questions, adding a dependency to your build.gradle is easy. The dependencies block tells Gradle what artifacts you need:

dependencies { compile group: 'com.google.guava', name: 'guava', version: '20.0' testCompile group: 'junit', name: 'junit', version: '4.+' }

Note: compile tells Gradle that you need this dependency when compiling your main application code. testCompile tells Gradle that you need this dependency only when compiling test code. (These are called source sets and are outside the scope of this tutorial.)

As you can see, it’s as simple as specifying the group, name, and version. In this example, the group is com.google.guava, the name of the module is guava, and the version is 20.0.

Now that Gradle knows what we want, we need to tell is where to find it. If you are using the maven central repo, its’ as simple as:

repositories { mavenCentral() }

You may recognize this block from the documentation, or templates generated by IDEs. The repositories block tells Gradle where to find the articles located in the dependencies block. If you were to specify a URL for another Maven repository your repositories block would look something like this:

repositories { maven { url "http://repo.mycompany.com/maven2" } }

This block of code is directly from the spring documentation. The nested maven block tells Gradle that the repository you are using is a Maven repository and the url property specifies where the repository is located.

Conclusion

I hope that breaking down Gradle dependencies into this thought process has helped you. If you have any questions, feel free to contact me at slimwolfempire@gmail.com or via my contact page.

Recommended Book On Gradle: Gradle Dependency Management

This post is brought to you by the Eat Sleep Code Repeat T-Shirt