Dung (Donny) Nguyen

Senior Software Engineer

Spring Framework with Maven: A Comprehensive Guide

Using Maven with the Spring Framework is the industry standard for managing dependencies, automating builds, and ensuring that your project is reproducible. Whether you are using “Classic” Spring or the modern Spring Boot, the core of the integration lies in the pom.xml (Project Object Model) file.

1. The Quickest Start: Spring Initializr

The easiest way to set up a Maven-based Spring project is through start.spring.io.


2. Manual Setup: The pom.xml Structure

If you’re building it from scratch, your pom.xml needs three main components to work effectively with Spring:

A. The Parent (For Spring Boot)

Using the spring-boot-starter-parent is highly recommended. It manages the versions of all other Spring dependencies so you don’t have to.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.4</version> <relativePath/> 
</parent>

B. The Starters (Dependencies)

Instead of adding 20 different JARs for a web app, you add one “Starter.” Maven will automatically pull in all transitive dependencies (Tomcat, Jackson, Logback, etc.).

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

C. The Build Plugin

This plugin allows you to package the application into a “fat JAR” that includes all dependencies and an embedded server.

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

3. Essential Maven Commands

Once your project is set up, you will use these terminal commands to manage your Spring application:

Command What it does
mvn clean Deletes the target folder (clears old builds).
mvn compile Compiles your Java source code.
mvn test Runs your unit and integration tests.
mvn package Creates a .jar file in the target folder.
mvn spring-boot:run Starts your application immediately without packaging.

4. Project Structure

Maven expects a specific directory layout. If you don’t follow this, Spring won’t find your configuration files:

Pro Tip: If you encounter “Dependency Hell” (version conflicts), run mvn dependency:tree in your terminal. It shows you exactly which library is pulling in which version of a JAR, allowing you to exclude the troublemaker.