Buried deep inside Gradle is the graph of Java objects which represent your project. In some build systems this is known as the project object model. What are these objects and how do you access and modify them to configure your build? You will find out how to use the Gradle Project Object Model by exploring the API for the org.gradle.api.Project class.
- [Instructor] In this video, we're going to talk about the Gradle project object module. We're talk about, what is an object model? We'll talk about one of the most important objects within that module, and that's the project object. And then we'll talk about a couple of other objects that are also very important within the project object module, and those are tasks and actions. So first, what is the project object model? Well, essentially, it's just Java objects that are built by Gradle when we run the build.gradle file.
The objects just represent things in our application. So not the domain objects like sale of antenna case but the actual objects that we're building as part of our build. It's used by Gradle to build the projects. So one of the first things that Gradle does when it starts up is to figure out what the project object module is. And the other thing about it that's very cool is that it's modifiable by developers. So in other words, it's not a static module that we can't affect, but we can actually modify it.
The probably most important object in the project object module is the project object itself. And it really has a one-to-one relationship with the build.gradle. So every build.gradle represents a project and a Java object gets built called project. And it's actually assigned to a reference variable called project that we can use to run methods ourselves or change properties. The second most important object in the project object module is the task object.
We can think of a project as a collection of tasks. So compile, create jar file. And a task itself is a collection of smaller actions, although we don't ever see the individual actions. So really we only work at the task level. Actions are the actual functions performed by Gradle, and usually there is only a single action per task. And one of the things we'll be able to do when we look at the code in a bit is actually list what the tasks are for a project. We'll look at some code now, and we will open up our build.gradle file, and we'll see that the build.gradle file modifies the project object.
So in other words, when we do something like look at the source compatibility, what we're really doing is changing an attribute on the project object. And so for instance, if I specify on line six project.sourceCompatibility, that works fine because by default whenever we don't specify project that's the object that's being used. So I can say project.repositories, or project.dependencies.
So I'm actually able to use the project object. Now what other properties do we have available to us? Well, there's a couple of ways to see that. One way is to use Gradle to run the properties task. So gradle properties. And this is a little bit long, we don't need to see this in detail but we can see that we've got a list of these projects. For instance, just a few lines down we've got the buildDir. So the buildDir is the directory where our build artifact is created.
So for me, you can see that it's in my user directory in the IdeaProjects folder and then the java-project folder underneath that. And then finally, in the build directory. And so if I highlight that in my project view, I can see that that's the property. And so it's actually built in that location. And if I wanted to, I could change that particular property by typing project on line 17, dot, and then buildDir, and I could put that in a different place if I wanted to.
So I could call it mybuilddir and it would build it in that directory. I'm not going to do that. I want to keep the default, but the point is that I could. What's another way to see what's some of those properties on the project object dir. I'm gonna call up my browser and take a quick look at some of the Gradle documentation. And I'm going to docs.gradle.org, and you can see that this defines what's in the project. And I'm gonna scroll down a little bit, and basically I can see a bunch of properties here.
For instance, at the top of the screen, I can see dependencies. And I can see positories now near the top of the screen. So this is a nice piece of documentation for allowing you to see what all the different properties are that you can use and that you can manipulate. And then I'm scrolling down a little bit further, and I can see the methods. So remember, the project object is just a regular old Java project. So we can set any of its properties, read any of its properties and run any of its methods.
So at some point you wanna take a read through this to sort of get a handle on what all the things are that you can do in a project. Let's come back to IntelliJ, I'll close my terminal. And the next thing I wanted to talk about was tasks. So what tasks are available to us? Well, let me open a new terminal session by clicking on the terminal link down at the bottom left to the screen. And I'll use by Gradle command, tasks. What the task tasks does is shows me all the tasks that are available for me to run.
So for instance, we've been running build and you can see that on the fourth line of the terminal output, a build actually assembles and tests the project. I could run other tasks like clean and jar and task classes that are available here. So if you ever wanna know just what are the tasks available to you, you can run the Gradle tasks command and see that. The other thing about this output is that the tasks are grouped together. So we can see at the top we have this label Build tasks, and those are the tasks that are assigned as build tasks.
We also have Build Setup, Documentation tasks, Help tasks, and Verification tasks. So they are just grouped logically into different categories so you don't see them all at one time. In a later video, we'll drill down into the tasks and see how we can actually create our own tasks and have them automatically added to the tasks list.
- 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