Run Spring Boot Maven Command

Maven is a popular build automation tool used primarily for Java projects. Running a Spring Boot application with Maven is straightforward using the spring-boot:run goal provided by the Spring Boot Maven plugin. This guide will show you how to set up and run a Spring Boot application using Maven commands.


  • JDK 17 or later
  • Maven installed on your machine
  • Spring Boot (version 3.2+ recommended)
  • An IDE (IntelliJ IDEA, Eclipse, VS Code, etc.)

Step 1: Set Up a Spring Boot Project Using Spring Initializr

Use Spring Initializr to generate a new Spring Boot project with the following configuration:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.2.x
  • Dependencies: Spring Web

Download the generated project, unzip it, and open it in your IDE.

Example Project Structure

The basic structure of a Spring Boot project with Maven looks like this:

├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/demo/
│   │   │       └──
│   │   └── resources/
│   │       ├──
│   └── test/
│       └── java/
│           └── com/example/demo/
│               └──
├── mvnw
├── mvnw.cmd
├── pom.xml
└── .mvn/
    └── wrapper/

Step 2: Configure pom.xml

The pom.xml file is the heart of a Maven project. It defines the project's dependencies, build configuration, and more. Open the pom.xml file and ensure it has the following configuration:

<project xmlns=""

    <description>Demo project for Spring Boot</description>

        <relativePath/> <!-- lookup parent from repository -->





Step 3: Create the Application Class

Create a Java class named DemoApplication in the src/main/java/com/example/demo directory.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class DemoApplication {

    public static void main(String[] args) {, args);


  • @SpringBootApplication: Marks this class as the main entry point for the Spring Boot application.
  • main method: Starts the Spring Boot application.

Step 4: Create a Simple REST Controller

To verify the application works as expected, let's create a simple REST controller.

Create a Java class named HelloController in the src/main/java/com/example/demo directory:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {

    public String sayHello() {
        return "Hello, World!";


  • @RestController: Marks this class as a REST controller.
  • @GetMapping("/hello"): Maps HTTP GET requests to the /hello endpoint to the sayHello method.
  • sayHello method: Returns a "Hello, World!" message.

Step 5: Run the Application

Using the Maven Wrapper

The Maven Wrapper is a script that allows you to run Maven commands without having Maven installed on your system. It's included in your project when you generate a Spring Boot project from Spring Initializr.

Steps to Run the Application

  1. Open a terminal: Navigate to the root directory of your Spring Boot project.

  2. Run the Application: Use the spring-boot:run goal to start the application.

For Unix/Linux/macOS:

./mvnw spring-boot:run

For Windows:

mvnw.cmd spring-boot:run

Using Maven Installed on Your Machine

If you have Maven installed on your machine, you can use the mvn command directly:

  1. Open a terminal: Navigate to the root directory of your Spring Boot project.

  2. Run the Application: Use the spring-boot:run goal to start the application.

For Unix/Linux/macOS/Windows:

mvn spring-boot:run

Step 6: Verify the Application

Open a web browser or a tool like Postman and navigate to the following URL to verify the application:

  • Hello Endpoint:
    • URL: http://localhost:8080/hello
    • Method: GET
    • Response: Hello, World!

You should see the "Hello, World!" message returned by the HelloController.

Step 7: Creating a Test Class

Create a Java class named DemoApplicationTests in the src/test/java/com/example/demo directory.

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

class DemoApplicationTests {

    void contextLoads() {


  • @SpringBootTest: Indicates that this is a Spring Boot test.
  • contextLoads method: Tests if the Spring application context loads successfully.

Running Tests

  1. Run Tests: Use the mvn test command to run the tests.

For Unix/Linux/macOS/Windows:

mvn test
  1. View Test Results: The test results will be displayed in the terminal, indicating whether the tests passed successfully.


In this tutorial, you have learned how to set up and run a Spring Boot application using Maven commands. We covered:

  • Setting up a Spring Boot project using Spring Initializr with Maven.
  • Configuring the pom.xml file.
  • Creating the main application class.
  • Creating a simple REST controller to verify the application works as expected.
  • Running the application using Maven commands.
  • Creating and running tests.

By following these steps, you can easily set up and manage Spring Boot projects using Maven, leveraging its powerful build and dependency management capabilities.