Java – Spring Boot REST Java Microservice: Why Use Maven Submodules

javamavenmicroservicesspring-boot

I have seen a project which uses Maven submodules. The project itself is a spring boot application that exposes Restful API endpoints for a microservice ex: Customer Service (getCustomers, createCustomers etc); consisting of standard model classes, repository, service interface&implementations, controllers etc.

Is there a good reason why we want to make model classes, repository, service interface&implementations, controllers, submodules ?

To be clear, i'm comparing these 2:

1) Normal case:

Project
            src
               main
                   java
                      dao
                      model
                      service
                      controller
                   resources
               test
                   ...
             pom.xml

2) Submodules case:

Project
        dao
            src
               main
                   java
                      dao
                   resources
               test
                   ...
            pom.xml
         service
            src
               main
                   java
                      service
                   resources
               test
                   ...
            pom.xml

Why go for no 2?

To me this structure looks really complex with each pom.xml in each submodule folder..

Best Answer

To my experience, for small apps or very small services you are right, maven modules make thing unnecessary complex in two ways: technical and cognitive.

However, modules can be of much help when:

  • Services are complex and we need a way to visualize their most relevant boundaries so that developers can find or add features in the right place quickly.

  • Decomposing services. Even microservices can be composed of several but smaller services. Using maven modules can provide us with valuable feedback about the interaction of the components and the best way to decouple them. Or a reason to leave where they are. Moving code to a standalone process will be easier this way.

A well-modular application can enforce a healthy separation of concerns too and reduce the coupling between elements of the application.

Modules can be the tactical answer to a strategic question or need too. For example:

  • We need/want to develop, evolve and maintain features or capabilities separately. In other words, providing them with different development life cycles.
  • Versioning of code or logic
  • Code reuse (as shared libs, aka microkernels)
  • Services' archetyping (look for maven archetypes for more information)

If the service implements a Hexagonal architecture, another advantage is in the "adapters". These can be moved to a different module, constraining any possible coupling between abstractions and implementation details. At the same time, the new module can be implemented in parallel. While one member of the team is focused on the business, others focus on the adapters.

Testing also matters. It's not the same testing a small module (in terms of time and complexity) than testing a whole service which usually involves many more "moving-things" to take into account.

Related Topic