Gradle can be used as the build system for many programming environments—such as C, Android, and Python—but as a Java developer you are probably most interested in its use as a Java build system. You will learn how to use the Java Plugin for Gradle to build various types of Java applications.
- [Narrator] So let's talk about the Java Plugin for Gradle. We've already seen a simple Java program but how do we add Gradle to it? Well, we use something call the Java Plugin for Gradle. So let's see what does the Java plugin do? How do we declare the plugin? What are the tasks that the plugin makes available to us? And how do we declare dependencies and repositories? So what does the Java plugin do? Well it really makes Gradle "Java aware." In other words, without the Java plugin, Gradle doesn't actually know how to build a Java application.
We can think of Gradle as sort of the core of a build system but without the knowledge to build any specific kind of application. So the Gradle plugin will allow it to build Java applications. What does that mean specifically? Well the Gradle plugin defines a set of tasks that are specific to Java builds. Also, there are many kinds of plugins available for Gradle. And that's really part of it's power. The fact that it's extensible to many other kinds of application types, that it can be used as a build system through the use of plugins.
How do we actually apply the Java plugin? While I kind of use those terms, that gives it away. In the build doc Gradle file, we simply add a line that says apply plugin and we give it the name of the plugin. So in our case, we're using the Java plugin, we'll say apply plugin Java at the top of the build doc Gradle file. Specifically, the Java plugin adds some tasks that are specific to Java. So, we'll have a clean task that'll clean up the Java byte-code that the Gradle writes out, a compile task, which will actually compile the Java code, an assemble task, which will take the byte-code and turn it into a jar file, and a test task that'll actually run our unit tests.
Now these aren't the only tasks we have, these are the main ones, but this is what a plugin does. It adds additional, application specific tasks. Now in our project we have a dependency on the gson library. So we'll use Gradle to actually go get that dependency for us and the technique for that is to simply add a dependency section to the build doc Gradle file. We can see what it looks like here. We've got the dependencies key word and then within braces, a list of our dependencies that are associated with compiling or testing.
In this example here, this will be associated with the compile, and then the dependency consists of three parts separated by colons. The first part is the name of the organization, com.google.code.gson. The second part is the name of the artifact itself, the name of the library, gson. And then the third part is the version number that we're looking for. So in this case, 2.8.0. Now typically in an actual application, we have a lot more dependencies than this but this gives you the idea.
Once we've declared our dependencies, we need to tell Gradle where to get those dependencies, so, which repository to look into. The way we do that is by adding a repository section to the build doc Gradle file, and within the braces we add the list of repositories we want Gradle to check. So in this example we're asking Gradle to look in mavenCentral. And that may surprise you a bit if you've had some experience with Maven. How is Gradle actually able to use the Maven repository? Well one of the things that the Gradle developers did to make it easy to adopt Gradle was to use the same format for defining dependencies.
So in other words, we can use the same format as Maven so we can take advantage of all the jar files that are already in Maven repositories around the internet.
- 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