All posts / Spring Boot

Maven Mastery with Spring Boot

NordLearn · · 5 min read

On this page

Maven Mastery with Spring Boot

Maven Mastery with Spring Boot

If you’ve been writing Spring Boot applications for more than a week, you’ve touched Maven. You’ve either accepted its magic with a thankful nod, or fought it in frustration when something breaks. This guide is for both groups.

We’ll go deep — not just ‘add this dependency’ deep, but understanding WHY Maven works the way it does. We’ll cover multi-module project structure, BOM, plugins, dependency scopes, testing with Surefire and Failsafe, Mockito’s verify(), and real-world best practices. We’ll also answer the questions every developer eventually asks.

1. What Is Maven and Why Does It Matter?

Maven is a build automation and project management tool. At its core it does three things: dependency management, build lifecycle management, and project information management. It uses a declarative XML file (pom.xml) to describe your project.

Every artifact in the Maven ecosystem is uniquely identified by three coordinates:

<groupId>com.example</groupId>       <!-- Organization / namespace -->
<artifactId>my-service</artifactId>  <!-- Project / module name    -->
<version>1.0.0</version>             <!-- Version                  -->

2. The Spring Boot Parent POM

Every Spring Boot project starts with inheriting the Spring Boot Starter Parent. Understanding this is foundational to everything else.

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

When you inherit from spring-boot-starter-parent you get all of the following for free:

⦁ Java version default (Java 17 for Spring Boot 3.x)

⦁ UTF-8 source encoding

⦁ Dependency management for hundreds of curated libraries — all versions tested together

⦁ Plugin management with sensible defaults

⦁ Resource filtering for application.properties / application.yml

Do You Always Need to Set the Java Version?

No. The parent already sets a default — Java 17 for Spring Boot 3.x. You only need to declare it in your properties if you want to OVERRIDE the default:

<!-- Only add this if you want a DIFFERENT version than the parent default -->
<properties>
    <java.version>21</java.version>
</properties>

<!-- If you're happy with Java 17 (Spring Boot 3.x default), you don't need this at all -->
<!-- But it's good practice to declare it explicitly so intent is visible to teammates -->

The Parent Inheritance Chain

Your pom.xml
  └─ spring-boot-starter-parent       (compiler settings, plugin config, resource filtering)
       └─ spring-boot-dependencies     (THE BOM — all 300+ dependency versions defined here)
            └─ spring-framework-bom
                 └─ (other BOMs...)

Important: If your organization mandates a corporate parent POM, you cannot inherit from spring-boot-starter-parent. Import spring-boot-dependencies as a BOM instead (covered in Section 5).

3. Dependencies Deep Dive

Dependencies are the heart of your pom.xml. But not all dependencies are equal — their scope defines when and how they’re available.

Dependency Scopes

Dependency Scopes

Dependency Scopes

Lombok: optional=true and Why We Exclude It

Lombok generates boilerplate code (getters, setters, constructors, builders) at COMPILE TIME via annotation processing. By the time your application runs, Lombok has already done its job — the generated methods exist in your .class files. The Lombok JAR itself is completely useless at runtime.

<!-- optional=true means: -->
<!-- 'I need Lombok to BUILD this module, but don't force it on anyone who depends on me' -->
<!-- Without optional=true, every project that uses YOUR library gets Lombok transitively -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>   <!-- Don't leak Lombok to downstream consumers -->
</dependency>

Now the spring-boot-maven-plugin exclusion — why exclude Lombok from the fat JAR?

<!-- The spring-boot-maven-plugin packages ALL dependencies into a fat executable JAR -->
<!-- Without exclusion, Lombok's 1.5MB JAR gets bundled in too — dead weight at runtime -->
<!-- Think of Lombok like scaffolding: essential during construction, removed when done -->
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </exclude>
        </excludes>
    </configuration>
</plugin>

Practical Dependency Examples

<dependencies>
    <!-- Web MVC — compile scope (default) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- No version — managed by parent BOM -->
    </dependency>

    <!-- H2 — runtime only, not needed to compile -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Lombok — compile-time only, don't leak to consumers -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- Testing — test scope only -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Excluding Transitive Dependencies

<!-- Switch from default Logback to Log4j2 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Debug Tip: Run ‘mvn dependency:tree’ to visualise your full dependency graph and spot conflicts. Use ‘mvn dependency:analyze’ to find unused declared dependencies or used undeclared ones.

4. Multi-Module Projects — Full Working Example

Multi-module Maven projects are the industry-standard way to organise large Spring Boot applications. They let you split a codebase into cohesive modules while keeping everything in one repository and one build.

Project Structure

ecommerce-platform/                ← Root (parent) module
├── pom.xml                        ← Parent POM  (packaging: pom)
├── common/                        ← Shared DTOs, utils, exceptions
│   ├── pom.xml
│   └── src/main/java/com/ecommerce/common/
│       ├── dto/
│       │   ├── OrderDTO.java
│       │   └── ProductDTO.java
│       └── exception/
│           └── BusinessException.java
├── user-service/                  ← User domain — runnable Spring Boot app
│   ├── pom.xml
│   └── src/
├── order-service/                 ← Order domain — runnable Spring Boot app
│   ├── pom.xml
│   └── src/
│       ├── main/java/com/ecommerce/order/
│       │   ├── OrderApplication.java
│       │   ├── controller/OrderController.java
│       │   ├── service/OrderService.java
│       │   ├── repository/OrderRepository.java
│       │   └── entity/Order.java
│       └── test/java/com/ecommerce/order/
│           ├── service/OrderServiceTest.java       ← Surefire (unit)
│           └── controller/OrderControllerIT.java   ← Failsafe (integration)
└── product-service/               ← Product domain — runnable Spring Boot app
    ├── pom.xml
    └── src/

Root Parent POM

The root pom.xml is the orchestrator. It declares modules, manages ALL dependency versions centrally, and configures shared plugins. Crucially its packaging type is ‘pom’ — it produces no JAR of its own.

<!-- ecommerce-platform/pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.ecommerce</groupId>
    <artifactId>ecommerce-platform</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>          <!-- CRITICAL: not jar! -->

    <!-- Declare all child modules -->
    <modules>
        <module>common</module>
        <module>user-service</module>
        <module>order-service</module>
        <module>product-service</module>
    </modules>

    <!-- Shared properties -->
    <properties>
        <!-- Override Spring Boot parent default only if needed -->
        <!-- For Spring Boot 3.x the default is already Java 17 -->
        <!-- Declare explicitly for clarity: -->
        <java.version>17</java.version>
        <mapstruct.version>1.5.5.Final</mapstruct.version>
        <springdoc.version>2.3.0</springdoc.version>
        <testcontainers.version>1.19.3</testcontainers.version>
    </properties>

    <!-- DECLARE versions here — child modules just opt in without a version -->
    <dependencyManagement>
        <dependencies>
            <!-- Internal module version management -->
            <dependency>
                <groupId>com.ecommerce</groupId>
                <artifactId>common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!-- Third-party pinning -->
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
                <version>${springdoc.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- pluginManagement = recipe book (defines config, does NOT activate) -->
    <!-- Child modules must explicitly declare a plugin to activate it      -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <annotationProcessorPaths>
                            <!-- Lombok MUST be before MapStruct -->
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </path>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${mapstruct.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Like Lombok, MapStruct is annotation-processor-only — it generates code during compilation and ships nothing to production. That is why it gets the annotationProcessorPaths treatment in the compiler plugin, and why Lombok must be declared BEFORE MapStruct (Lombok generates the getters/setters that MapStruct then reads).

common Module POM — Shared Library

The common module produces a plain JAR consumed by service modules. It must NOT have the spring-boot-maven-plugin — that would repackage it into an executable fat JAR that other modules cannot import as a dependency.

<!-- ecommerce-platform/common/pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.ecommerce</groupId>
        <artifactId>ecommerce-platform</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>common</artifactId>
    <!-- Inherits groupId and version from parent -->
    <!-- NO spring-boot-maven-plugin here! -->

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

order-service Module POM — Runnable App

Service modules inherit from the parent, opt into the dependencies they need (no versions — all managed by the parent), and activate the spring-boot-maven-plugin to produce an executable fat JAR.

<!-- ecommerce-platform/order-service/pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.ecommerce</groupId>
        <artifactId>ecommerce-platform</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>order-service</artifactId>

    <dependencies>
        <!-- Depend on our shared common library -->
        <dependency>
            <groupId>com.ecommerce</groupId>
            <artifactId>common</artifactId>
            <!-- No version — managed in parent dependencyManagement -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Activate spring-boot plugin to produce runnable fat JAR -->
            <!-- Config is inherited from parent pluginManagement -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Surefire: unit tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*IT.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- Failsafe: integration tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Critical Rule: Only runnable modules (order-service, user-service etc) should have spring-boot-maven-plugin. Library modules like ‘common’ must NOT — it would repackage the JAR into an executable that other modules cannot import.

5. Bill of Materials (BOM)

A BOM is a special POM that declares a set of dependency versions. It solves the ‘which version do I use?’ problem across large teams and projects.

<!-- Option A: Inherit from parent (preferred when possible) -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

<!-- Option B: Import BOM (when you already have a corporate parent) -->
<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>corporate-parent</artifactId>
    <version>2.0.0</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</roupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.2.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Creating Your Own Company BOM

For large organisations with many internal libraries, a custom BOM ensures all teams use compatible versions — no more ‘works on my machine’ version conflicts.

<!-- my-company-bom/pom.xml -->
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>my-company-bom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>   <!-- Must be pom -->

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>auth-client</artifactId>
                <version>2.1.0</version>
            </dependency>
            <dependency>
                <groupId>com.mycompany</groupId>
                <artifactId>logging-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

<!-- Any team uses it like this — no versions needed: -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-company-bom</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

7. Essential Maven Plugins

maven-surefire-plugin — Unit Tests

Surefire runs during the ‘test’ phase. It picks up classes named *Test.java, *Tests.java, *TestCase.java. Fast tests only — milliseconds each. No Spring context, no database.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- Exclude integration tests from this fast phase -->
        <excludes>
            <exclude>**/*IT.java</exclude>
            <exclude>**/*IntegrationTest.java</exclude>
      </excludes>
        <parallel>methods</parallel>
        <threadCount>4</threadCount>
    </configuration>
</plugin>

maven-failsafe-plugin — Integration Tests

Failsafe runs during the ‘integration-test’ and ‘verify’ phases. It picks up *IT.java and *IntegrationTest.java. Slow tests — real Spring context, real database via Testcontainers.

Critical difference from Surefire: Failsafe does NOT fail the build immediately when a test fails. It waits until the ‘verify’ phase — allowing post-test cleanup to run even on failure.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>   <!-- run the tests -->
                <goal>verify</goal>             <!-- check results, fail if needed -->
            </goals>
        </execution>
    </executions>
    <configuration>
        <skipITs>${skipITs}</skipITs>
    </configuration>
</plugin>

Build Lifecycle: How Surefire and Failsafe Fit

Command                          | What runs
-------------------------------------------------------------------------------
mvn test                         | Surefire only (unit tests). Fast.
mvn verify                       | Surefire first, then Failsafe (integration). Full confidence.
mvn verify -DskipTests           | Skip ALL tests
mvn verify -DskipITs             | Skip only integration tests (run unit tests)
mvn test -Dtest=OrderServiceTest | Run one specific test class

8. Repositories

Maven repositories are where artifacts live. Local (your machine cache) → Central (Maven Central) → Remote (corporate Nexus/Artifactory). Maven checks in that order.

Adding Custom Repositories

<repositories>
    <repository>
        <id>corporate-nexus</id>
        <url>https://nexus.mycompany.com/repository/maven-releases/</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
</repositories>

<distributionManagement>
    <repository>
        <id>corporate-nexus</id>
        <url>https://nexus.mycompany.com/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>corporate-nexus-snapshots</id>
        <url>https://nexus.mycompany.com/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Credentials — Always in settings.xml, Never in pom.xml

<!-- ~/.m2/settings.xml — this file is gitignored by default -->
<settings>
    <servers>
        <server>
            <id>corporate-nexus</id>    <!-- Must match repo id in pom.xml -->
            <username>my-username</username>
            <password>${env.NEXUS_PASSWORD}</password>  <!-- Use env var -->
        </server>
    </servers>
</settings>

Essential Maven Commands

# Full build: compile + unit tests + package
mvn clean package

# Full build including integration tests
mvn clean verify

# Skip all tests (faster, use sparingly)
mvn clean package -DskipTests

# Skip only integration tests
mvn clean verify -DskipITs

# Build only one module (and its dependencies)
mvn clean package -pl order-service -am

# Run a specific test class
mvn test -Dtest=OrderServiceTest

# Run a specific test method
mvn test -Dtest=OrderServiceTest#createOrder_whenPaymentFails

# See full dependency tree
mvn dependency:tree

# Find dependency conflicts
mvn dependency:analyze

# Check for newer dependency versions
mvn versions:display-dependency-updates

# Run Spring Boot app
mvn spring-boot:run -pl order-service

Wrapping Up

1. Coordinates: Every artifact has a unique groupId:artifactId:version address.

2. Inheritance: Child POMs inherit from parents. Use this for consistent defaults.

3. dependencyManagement declares versions. dependencies opts in. Never mix them up.

4. BOM: A POM that only manages versions. Import it when you can’t use a parent.

5. pluginManagement is a recipe book. plugins activates the recipe.

6. spring-boot-maven-plugin only belongs in runnable modules, never in library modules.

7. Lombok and MapStruct are compile-time only. optional=true, exclude from fat JAR.

8. Surefire = unit tests (*Test.java). Failsafe = integration tests (*IT.java).

9. verify() checks BEHAVIOUR. assertThat() checks OUTPUT. Use both.

10. Credentials live in settings.xml. Never in pom.xml.

With these foundations solid you can tackle any Maven challenge — from simple single-module Spring Boot apps to large enterprise multi-module architectures with dozens of services.

If this guide helped you, follow me for more deep-dives into Spring Boot, Java, and backend architecture.

Happy coding!