Senger CodeLab 🚀

Maven parent pom vs modules pom

September 29, 2026

📂 Categories: Java
Maven parent pom vs modules pom

Managing dependencies and configurations across multiple projects can be a real headache. If you’re working with Java, Maven can be your savior. Understanding the interplay between the parent POM and module POMs within Maven is crucial for streamlined project management. This allows for efficient inheritance and overriding of configurations, significantly reducing redundancy and promoting consistency. Let’s delve into the intricacies of Maven’s parent-module structure and discover how it can revolutionize your development workflow.

What is a Maven Parent POM?

The parent POM acts as the blueprint for all its child modules. Think of it as the central repository for common configurations like dependency versions, plugin management, and default build settings. This centralized approach ensures consistency and reduces code duplication across your projects.

For example, if all your modules use JUnit for testing, you can define the JUnit dependency in the parent POM, eliminating the need to declare it repeatedly in each module.

This inheritance model promotes a DRY (Don’t Repeat Yourself) principle, making your project more maintainable and less prone to errors caused by inconsistent configurations.

What is a Maven Module POM?

Module POMs represent individual projects within your larger application. They inherit configurations from the parent POM but can also override or extend them as needed. This provides flexibility for specific module requirements without sacrificing overall project structure.

Imagine you have a web application module that requires a specific servlet dependency. You can declare this dependency within the module’s POM without affecting other modules.

This modularity not only promotes cleaner code organization but also facilitates parallel development as different teams can work on individual modules concurrently.

The Relationship Between Parent and Module POMs

The relationship is hierarchical. The parent POM provides the foundation, and module POMs inherit and customize configurations based on their specific needs. This inheritance mechanism uses the tag in the module’s POM, referencing the parent POM’s coordinates (groupId, artifactId, version).

This hierarchical approach provides a clear and structured way to manage dependencies and configurations across a multi-module project.

Changes made to the parent POM automatically propagate to its children, ensuring consistency. However, modules retain the flexibility to override inherited configurations if necessary.

Benefits of Using a Parent POM

Using a parent POM offers several advantages. It streamlines dependency management, enforces consistent build settings, and reduces code duplication. It simplifies the management of common plugins and their versions across all modules.

Centralizing configurations in the parent POM saves time and effort by eliminating the need to repeat configurations in each module.

  • Centralized Dependency Management
  • Consistent Build Settings

These benefits contribute to a more maintainable and scalable project structure, especially beneficial in large and complex projects.

Overriding Parent POM Settings

While inheritance promotes consistency, sometimes modules require specific settings. Modules can override inherited properties, dependencies, and plugin configurations. This allows flexibility while maintaining the benefits of a centralized structure.

For example, a module requiring a specific version of a library can override the parent’s defined version. This adaptability ensures that individual modules can meet specific requirements without disrupting the overall project harmony.

  1. Locate the property, dependency, or plugin configuration in the module POM.
  2. Re-declare the element with the desired value.

Real-World Example

Consider a large e-commerce application with separate modules for user accounts, product catalog, and order processing. The parent POM defines common dependencies like logging frameworks and testing libraries. Each module can then add specific dependencies, such as database drivers or UI frameworks, relevant to its functionality.

This modular approach improves code organization, maintainability, and scalability of the entire application. It also facilitates parallel development by enabling teams to work independently on different modules.

“Modular design principles are key for building scalable and maintainable software systems.” - Robert C. Martin, Clean Code

Best Practices

For effective parent-module structures, keep the parent POM focused on shared configurations. Avoid placing module-specific configurations in the parent. Clearly document the parent POM’s purpose and structure for easy understanding and maintenance.

  • Focus on shared configurations in the parent POM.
  • Clearly document the structure and purpose of the parent POM.

Following these practices ensures a clean and efficient project structure, maximizing the benefits of Maven’s powerful inheritance mechanism.

FAQ

Q: Can a module have multiple parent POMs?

A: No, a Maven module can only have one parent POM. However, that parent POM can, in turn, have its own parent, creating a hierarchy of inheritance.

Understanding the relationship between parent and module POMs is fundamental for effective Maven project management. It allows for centralized configuration, efficient dependency management, and greater project flexibility. By leveraging the power of inheritance and overriding, you can create a more structured, maintainable, and scalable project architecture. Consider exploring advanced Maven features like dependency mediation and plugin management for even finer control over your project builds. Learn more about dependency management best practices. Further research into multi-module project structures and dependency management will provide a deeper understanding of optimizing your Maven projects. Resources like the official Apache Maven documentation and online tutorials can offer valuable insights. Dive deeper into these concepts and unlock the full potential of Maven for your projects. Check out resources like Apache Maven, Spring Boot, and Baeldung’s Maven Tutorials to further enhance your Maven skills.

Question & Answer :
There seem to be several ways to structure parent poms in a multiproject build and I wondering if anyone had any thoughts on what the advantages / drawbacks are in each way.

The simplest method of having a parent pom would be putting it in the root of a project i.e.

myproject/ myproject-core/ myproject-api/ myproject-app/ pom.xml 

where the pom.xml is both the parent project as well as describes the -core -api and -app modules

The next method is to separate out the parent into its own subdirectory as in

myproject/ mypoject-parent/ pom.xml myproject-core/ myproject-api/ myproject-app/ 

Where the parent pom still contains the modules but they’re relative, e.g. ../myproject-core

Finally, there’s the option where the module definition and the parent are separated as in

myproject/ mypoject-parent/ pom.xml myproject-core/ myproject-api/ myproject-app/ pom.xml 

Where the parent pom contains any “shared” configuration (dependencyManagement, properties etc.) and the myproject/pom.xml contains the list of modules.

The intention is to be scalable to a large scale build so should be scalable to a large number of projects and artifacts.

A few bonus questions:

  • Where is the best place to define the various shared configuration as in source control, deployment directories, common plugins etc. (I’m assuming the parent but I’ve often been bitten by this and they’ve ended up in each project rather than a common one).
  • How do the maven-release plugin, hudson and nexus deal with how you set up your multi-projects (possibly a giant question, it’s more if anyone has been caught out when by how a multi-project build has been set up)?

Edit: Each of the sub projects have their own pom.xml, I’ve left it out to keep it terse.

In my opinion, to answer this question, you need to think in terms of project life cycle and version control. In other words, does the parent pom have its own life cycle i.e. can it be released separately of the other modules or not?

If the answer is yes (and this is the case of most projects that have been mentioned in the question or in comments), then the parent pom needs his own module from a VCS and from a Maven point of view and you’ll end up with something like this at the VCS level:

root |-- parent-pom | |-- branches | |-- tags | `-- trunk | `-- pom.xml `-- projectA |-- branches |-- tags `-- trunk |-- module1 | `-- pom.xml |-- moduleN | `-- pom.xml `-- pom.xml 

This makes the checkout a bit painful and a common way to deal with that is to use svn:externals. For example, add a trunks directory:

root |-- parent-pom | |-- branches | |-- tags | `-- trunk | `-- pom.xml |-- projectA | |-- branches | |-- tags | `-- trunk | |-- module1 | | `-- pom.xml | |-- moduleN | | `-- pom.xml | `-- pom.xml `-- trunks 

With the following externals definition:

parent-pom http://host/svn/parent-pom/trunk projectA http://host/svn/projectA/trunk 

A checkout of trunks would then result in the following local structure (pattern #2):

root/ parent-pom/ pom.xml projectA/ 

Optionally, you can even add a pom.xml in the trunks directory:

root |-- parent-pom | |-- branches | |-- tags | `-- trunk | `-- pom.xml |-- projectA | |-- branches | |-- tags | `-- trunk | |-- module1 | | `-- pom.xml | |-- moduleN | | `-- pom.xml | `-- pom.xml `-- trunks `-- pom.xml 

This pom.xml is a kind of “fake” pom: it is never released, it doesn’t contain a real version since this file is never released, it only contains a list of modules. With this file, a checkout would result in this structure (pattern #3):

root/ parent-pom/ pom.xml projectA/ pom.xml 

This “hack” allows to launch of a reactor build from the root after a checkout and make things even more handy. Actually, this is how I like to setup maven projects and a VCS repository for large builds: it just works, it scales well, it gives all the flexibility you may need.

If the answer is no (back to the initial question), then I think you can live with pattern #1 (do the simplest thing that could possibly work).

Now, about the bonus questions:

  • Where is the best place to define the various shared configuration as in source control, deployment directories, common plugins etc. (I’m assuming the parent but I’ve often been bitten by this and they’ve ended up in each project rather than a common one).

Honestly, I don’t know how to not give a general answer here (like “use the level at which you think it makes sense to mutualize things”). And anyway, child poms can always override inherited settings.

  • How do the maven-release plugin, hudson and nexus deal with how you set up your multi-projects (possibly a giant question, it’s more if anyone has been caught out when by how a multi-project build has been set up)?

The setup I use works well, nothing particular to mention.

Actually, I wonder how the maven-release-plugin deals with pattern #1 (especially with the <parent> section since you can’t have SNAPSHOT dependencies at release time). This sounds like a chicken or egg problem but I just can’t remember if it works and was too lazy to test it.