Install Gradle:
First, we will need to install Gradle on our system.
-
Download the latest version of Gradle from the official website (https://gradle.org/releases/). Select the binary-only distribution and unpack the downloaded zip file to the directory where you want to install Gradle.
-
Add the bin directory of the unpacked distribution to your system's PATH environment variable so that you can execute Gradle from any directory. On Windows, you can do this by going to Control Panel > System and Security > System > Advanced system settings > Environment Variables and adding the bin directory to the PATH variable. On Linux or macOS, you can add the following line to your ~/.bashrc or ~/.bash_profile file:
export PATH=path/to/gradle/bin:$PATH
- Verify the installation by opening a new terminal window and typing:
gradle -v
This should print the version of Gradle that you have installed.
Create your Spring Boot project in 10 steps:
-
Create a new directory for your project and navigate to it.
-
Create a new file called
build.gradle
in your project directory and add the following content to it:plugins { id 'org.springframework.boot' version '2.3.5.RELEASE' id 'java' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' }
This file defines the dependencies for your project and tells Gradle to use the Spring Boot and Java plugins.
- Create a new directory called
src/main/java
and create a new file calledHelloWorldApplication.java
inside it with the following content:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }
This is the main class of your Spring Boot application. It tells Spring to start the application from this class.
- In the same directory, create a new file called
HelloWorldController.java
with the following content:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/hello") public String helloWorld() { return "Hello, World!"; } }
This is a simple controller that maps the
/hello
path to a method that returns the string "Hello, World!". - To build and run the application, navigate to the project directory in a terminal and type:
gradle build
This will build the project and create a jar file in the
build/libs
directory. - To run the application, type:
java -jar build/libs/your-project-name.jar
This will start the application on port 8080.
- Open a web browser and navigate to http://localhost:8080/hello to see the "Hello, Spring!" message.
That's it! You have successfully created a "Hello, Spring!" controller using the Gradle build system. You can now customize the controller to add more functionality to your application.