Gradle
By now you have created at least two Spring projects using Gradle. If you recall, Spring is the framework that will enable us to create an MVC application. When we created each project, we selected “Gradle Project”, but what is Gradle?
Gradle is an automated build tool that handles tasks like:
- Managing dependencies
- Testing protocols
- Packaging the executable program for deployment.
So far we have included Gradle in our Spring Boot projects.
You can also import Gradle using the terminal. Instruction on that is beyond the scope of this class. Here is documentation on this process if you are curious.
How Gradle Works
A Gradle build represents one or more projects. Projects are craeted from a series of tasks. Gradle manages tasks based on their function in the build. Some tasks run tests, some compile, etc. Gradle organizes these tasks in the build.gradle file.
build.gradle
The build.gradle file is the project build script. Gradle will refer to this file as it runs through the task list to build the application. You can code tasks into this file or you can import tasks into this file.
If we are using external tasks, we use methods to group tasks by functionality in this file. Tasks call the methods to execute a task or series of tasks to build the project. You are also allowed code directly in the build file, but this is more common if you are creating a Gradle project from scratch.
Spring Boot populated this file as it initialized your hello-spring build.
Open your build.gradle file and look at each section.
tasks and plugins
As stated earlier, Gradle builds a project by running tasks. These tasks are kept in a task method that calls them when needed. The task method is where you create tasks.
hello-spring has a task method that runs a test.
Often you will import tasks from an external resource as plugins. Plugins contain any necessary tasks and scripts to extend a project’s capabilities. Plugins often need dependencies and external repositories.
Your Spring Boot framework projects contain plugins.
dependencies
dependencies are external code libraries used by tasks to build your project. Gradle often groups related dependencies together in configurations. In the dependencies method, the configuration name proceeds the dependency information.
Your hello-spring project has three dependencies.
Note that each dependency has its own configuration name, such as implementation or developmentOnly.
configurations
Some projects contain a stand-alone configurations method. This method bundles dependencies together based on their role in the build. Dependency configurations can extend from one another, reducing redundancies within your code.
In the hello-spring project, the build.gradle file contains a configurations method.
The hello-spring project does not.
repositories
Where do we get the source code if dependencies are external libraries? It comes from an external repository site, such as Maven Central. Maven Central is a repository website that contains files for dependencies. This is not the only external repository. Other repositories work the same as Maven Central. In the build.gradle file we link up with Maven Central by using the repositories method.
You will see repositories in your Spring Boot projects.
Troubleshooting Tips
My dependencies won’t build
We are using IntelliJ as our IDE to build our Gradle projects for this class.
IntelliJ’s built-in IntelliSense should prompt you to refresh your build.gradle whenever
you change a file. You should see a small icon appear in the top right corner of the build.gradle file.
If you click on the icon, it will refresh your build.
If you update build.gradle and the icon does not appear, you can manually refresh the build.
Mac Users try Up + Command + I and Windows/Linux Users try Control + Shift + O.
For more on Gradle and IntelliJ, visit this website .
I’ve refreshed, but my dependencies still won’t build
Read IntelliJ’s documentation on Maven.Importing .
Check Your Understanding
From where do dependencies access their source code?
- An external repository such as Maven Central
- Internal code within a Class you created