Maven module using spring-boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A Maven module can absolutely use Spring Boot, but the cleanest design is usually a multi-module build where only one module is the executable application. The other modules hold shared domain code, data access, or integration logic and are consumed as ordinary Maven dependencies.
A Typical Multi-Module Shape
A common layout is:
- parent module with
packagingset topom - library modules for shared code
- one bootable module containing the
mainclass and Boot plugin
The parent POM aggregates the modules and centralizes version management.
Parent POM
The parent usually imports Spring Boot dependency management and declares the child modules.
This gives child modules consistent dependency versions without repeating them everywhere.
Library Modules Versus the Boot Module
Non-bootable modules should usually be ordinary JARs.
For example, a service module might look like this:
The bootable module depends on the others and contains the entry point.
Put the main Class in the Bootable Module
The executable module should contain the @SpringBootApplication class.
Using a root package that covers all modules often removes the need for explicit scanning configuration.
Build and Run
Build from the parent directory so Maven resolves module dependencies in one reactor build.
You can also run just the boot module once the reactor build has installed or built the dependencies.
Why Only One Module Usually Gets the Boot Plugin
The Spring Boot Maven plugin repackages a module into an executable archive. That is perfect for the application module and usually wrong for shared libraries.
If every module is repackaged as a boot jar, you often get awkward artifacts and unnecessary complexity. Keep library modules as normal jars unless you have a specific reason not to.
Keep Dependency Direction Clean
A good multi-module Boot build usually points inward toward stable code. For example, api can depend on service, and service can depend on data, but data should not depend back on api.
That keeps modules reusable and prevents circular dependencies in Maven and in Spring configuration. If two modules need shared DTOs or utilities, move that code into a neutral module such as common instead of making them depend on each other directly.
This structure also makes testing easier. Library modules can be tested without starting the whole Boot application, while the executable module focuses on wiring and integration.
Common Pitfalls
A common mistake is putting the Boot plugin in every module. Most modules in a multi-module project are libraries, not applications.
Another mistake is building a child module in isolation before its sibling modules are available. Reactor builds from the parent avoid this problem.
Developers also often place the @SpringBootApplication class in a narrow package, which prevents component scanning from reaching beans in sibling modules.
Summary
- A Maven module can use Spring Boot as either a library or the executable app module.
- In multi-module builds, the parent POM should aggregate modules and manage versions.
- Usually only one module contains the Boot
mainclass andspring-boot-maven-plugin. - Shared modules should stay as ordinary jars.
- Build from the parent so Maven resolves the entire module graph correctly.

