How to Create Spring Boot Application Using Maven

Creating a Spring Boot project using Maven is relatively straightforward. Let's set up a Spring Boot project with Maven from scratch. You can do it either through the Spring Initializr web tool or manually. 

Using Spring Initializr Web Tool 

Go to Spring Initializr

Choose the project settings: 

  • Project: Maven Project
  • Language: Java (default) 
  • Spring Boot version: Latest stable (default) 

Project Metadata: Fill in Group, Artifact, etc. 

Dependencies: Choose your desired dependencies (e.g., Spring Web, Spring Data JPA, Thymeleaf, etc.) 

Click on "Generate" to download the zip file. Extract the zip file and open it in your favorite IDE.

Manually Creating Project 

1. Create Project Structure: Create a new directory and inside it, create the following directories and files:

my-spring-boot-app/
├── src/
│   └── main/
│       └── java/
│           └── com/
│               └── example/
│                   └── MyApp.java
├── pom.xml

2. Edit pom.xml: Open pom.xml and add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-spring-boot-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.3</version> <!-- use the latest version -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Make sure you specify the latest Spring Boot version. 

3. Edit MyApp.java: Open src/main/java/com/example/MyApp.java and add the following content:

package com.example;

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

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

4. Build and Run: Navigate to your project directory and run the following commands:

mvn clean install
mvn spring-boot:run

Your Spring Boot application should now be running at http://localhost:8080/. Remember, Spring Boot allows you to add various other dependencies like JPA, Thymeleaf, Security, etc., based on your project needs. You can do this by adding them to your pom.xml.


Comments