Spring Boot MVC Multi-Module Executeable jar
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A Spring Boot MVC application can be split into multiple Maven modules without giving up the convenience of a single executable JAR. The usual pattern is to keep shared code in library modules and place the actual Spring Boot application entry point in one top-level web module that depends on those libraries.
The important rule is this: only the module that starts the application should be packaged as the executable Boot JAR. The other modules should behave like normal Java libraries.
A Typical Multi-Module Layout
A clean structure might look like this:
Responsibilities are usually divided like this:
- '
commoncontains shared models or utility code' - '
servicecontains business logic' - '
webcontains controllers, templates, and themainapplication class'
The web module depends on service, and service may depend on common.
Parent pom.xml
The parent project should use pom packaging and list the modules. It can also centralize dependency versions and plugin management.
This parent does not produce a runnable artifact. It only coordinates the build.
Library Modules Stay as Plain JARs
The common and service modules should usually have standard jar packaging and should not apply the Spring Boot repackage goal.
Example service/pom.xml:
These modules are just dependencies. They are not standalone applications.
Put the Boot Application in the Web Module
The executable JAR should be built from the module that contains the Spring Boot entry point:
Its pom.xml should depend on the other modules and apply the Boot plugin:
When you run mvn package from the parent, the web module produces the runnable JAR that contains classes and dependencies from all referenced modules.
Where MVC Resources Should Live
For a traditional MVC app, keep templates and static assets in the executable web module, because that is the module Spring Boot packages as the application artifact.
Typical locations are:
- '
web/src/main/resources/templates' - '
web/src/main/resources/static'
Shared Java code can live in sibling modules, but the web-facing resources should usually stay close to the Boot entry point so classpath resolution remains predictable inside the final executable JAR.
During development, it is also common to run only the application module:
That command still builds dependent modules as needed, while making it explicit which module owns the application lifecycle.
Controller and Service Wiring Across Modules
As long as the packages are under a common root such as com.example, component scanning works normally across modules.
Controller in web:
Service in service:
The module boundary is a build concern. At runtime, Spring still sees classes on one application classpath.
Common Pitfalls
The most common mistake is applying the Spring Boot repackage plugin in every module. That produces multiple bootable artifacts and complicates the build for no benefit.
Another issue is placing the @SpringBootApplication class in a package that does not cover the service or common modules. If component scanning does not reach those packages, beans will not be discovered.
Dependency direction also matters. The web module can depend on service modules, but shared libraries should not depend back on the web layer.
Finally, remember that the parent module should use pom packaging. If the parent is accidentally configured as a normal jar, the build layout becomes confusing very quickly.
Summary
- Use a parent Maven project to coordinate multiple Spring Boot MVC modules.
- Keep shared modules as plain library JARs.
- Put
@SpringBootApplicationand the Boot Maven plugin in one application module, usually the web module. - Let that one module build the executable JAR and depend on the others.
- Make sure component scanning covers classes defined in sibling modules.
Related reading
- Spring Boot not serving static content
- Spring Boot Oauth2 client credentials
- Spring boot on Kubernetes does not get restarted on java.lang.OutOfMemoryError Java heap space
- Spring Boot Overriding favicon
- Spring Boot Page Deserialization - PageImpl No constructor
- Spring Boot Primefaces - Unrecognized Content Type Exception
- Spring Boot Program cannot find main class
- Spring Boot project in IntelliJ community edition

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.