Gradle is a dependency manager tool similar to the maven. It is used to handle and manage all your project’s dependencies.
It uses the build.gradle file to list all the configurations and dependencies.
Let’s start to create a Gradle-based spring project. You can follow these steps:
Click on the File menu. Then, click new and select the Gradle project. If Gradle option is not available, simply click on the others and you can easily find the Gradle there.
After selecting the Gradle project, click on the next button and you will see a new window that asks for the project info.
Provide the project name and click the next button. After that, it takes some couple of seconds and creates a new project for you that looks like the following:
Its main configuration files is build.gradle that looks like the following:
It contains some default project configurations.
This project here is just a blank and simple Gradle project that does not have any Spring features yet. So, we add some Spring dependencies to this project first.
Adding Spring Dependencies
To make this project a spring application, we need to add the spring dependencies to the build.gradle file.
Add these spring core and context dependencies to the Gradle file by using the following statement:
implementation group: ‘org.springframework’, name: ‘spring-core’, version:’5.3.21′
implementation group: ‘org.springframework’, name: ‘spring-context’, version: ‘5.3.21’
You can find these dependencies on this site https://mvnrepository.com/artifact/org.springframework/ where you can select the latest dependencies.
After adding these lines, refresh the project by right clicking on the project -> Gradle->Refersh Gradle Project
This downloads all the dependencies automatically to the project.
You can verify these newly downloaded Spring JARs into the project explorer.
Now, let’s create a class to check whether the dependencies are working.
We create a class by clicking on the File menu and selecting a new class like the following:
Then, we add a @Component annotation to this class. This annotation is the core annotation of the spring framework that specifies that the marked class is a component class and should be treated as a component by the spring runtime.
Now, you can scale up your spring application according to your requirement. For example, you can add the dependencies for JDBC, JSON, JSP and Servlet, etc. and make it a live working application. Although, you will learn all these in our other articles.
If you are working on other IDE such as IntelliJ IDEA or STS (Spring Tool Suit), even then, you can create the Gradle project easily. The process of adding the dependencies is quite similar.
Always use the build.gradle file to add the dependencies and don’t forget to refresh the project that automatically loads all the required JARs to your project.
Here, we learned the step-by-step process to create a Gradle-based spring project by using the Eclipse IDE. We learned how to add the spring dependencies and use them into the source code.
In the next article, we will learn how to work on STS (Spring Tool Suit) which is an official IDE for creating the Spring application.