How to Build a Multi-Module Maven Project

TL;DR
A multi-module Maven project uses a root POM with pom packaging and a modules block to coordinate several subprojects. Shared dependencies can be inherited directly, selectively managed through dependencyManagement, or versioned in an imported BOM, while module dependencies allow code reuse without permitting cyclic relationships.
Transcript
hello everyone welcome back to my channel this is the next video about maven in this video i'll explain what is the multi-module maven project how to create it how to manage dependencies in this kind of project how to import one module to another and we'll take a look at what is the bom file and how to use it okay let's start what is a multi-module... Read More
Key Insights
- A multi-module Maven project is a collection of smaller Maven projects coordinated by a root POM. The root declares module folder paths, allowing Maven to recognize and work with their individual POM files as parts of one larger project.
- The root project uses pom packaging when it has no source code. Its POM serves as a central place for configuration shared by the modules, including settings and dependencies that child modules inherit from the parent or reactor POM.
- Dependencies declared directly in the reactor POM are inherited by every module. The demonstration shows a module using StringUtils from a shared Commons Lang dependency without declaring that library again inside the module's own POM.
- The dependencyManagement block centralizes dependency versions, scopes, and related configuration without placing those dependencies on module classpaths automatically. Each module selects the dependencies it needs by declaring their coordinates, while managed details such as the version can be omitted.
- A BOM is a POM file that controls dependency configuration through dependencyManagement. It has its own coordinates and pom packaging, and it can be installed in the local repository so that different modules or projects can locate and import it.
- A BOM import requires a dependency declaration with type pom and scope import inside dependencyManagement. Importing the BOM makes its managed dependency information available, but individual libraries must still be declared by modules that actually need them.
- A module can reuse another module by declaring it as a dependency with the target module's group ID, artifact ID, and version. The example allows the second subproject to create and use an entity class defined in the first subproject.
- Cyclic module dependencies are forbidden in the demonstrated Maven project. Compile and runtime dependencies from an imported module are accessible to the consuming module, while dependencies using test or provided scope are not accessible through that module relationship.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is a multi-module Maven project?
A multi-module Maven project is a project composed of several smaller Maven projects. A root POM coordinates these modules and lists the paths to the folders containing their POM files. This arrangement provides a shared place for common configuration while still allowing each module to maintain its own source code, POM, and module-specific dependencies.
Q: How do you configure the root POM for multiple Maven modules?
Configure the root POM with packaging set to pom because the root project has no source code in the demonstrated structure. Add a modules block containing the folder paths of the individual modules whose POM files Maven should use. The root POM can then hold common configuration and dependencies inherited by the modules.
Q: How are dependencies inherited from a Maven parent POM?
Dependencies declared directly in the parent or reactor POM are inherited by every module, placing those libraries on each module's classpath. In the demonstration, the first subproject uses StringUtils from a Commons Lang dependency inherited from the root. Modules may also add unique dependencies in their own POM files when only that module needs them.
Q: What does dependencyManagement do in a multi-module Maven project?
The dependencyManagement block stores dependency versions, scopes, and other configuration in one central location, but it does not automatically add those dependencies to module classpaths. A module must declare the dependency it needs using its coordinates. It may omit the version and other managed details because Maven obtains them from the root dependencyManagement configuration.
Q: Why use dependencyManagement instead of repeating dependencies?
DependencyManagement avoids repeating versions, scopes, and configuration across several module POM files. When a dependency version needs to change, the maintained value can be updated in the central reactor POM instead of every module declaration. Modules still choose only the libraries they require, which helps keep their executable files as light as possible.
Q: What is a Maven BOM and how is it imported?
A Maven BOM, or bill of materials, is a separate POM file used to control dependency configuration through dependencyManagement. Give it coordinates, set its packaging to pom, and install it in the local repository. Import it inside another project's dependencyManagement block by declaring those coordinates with type pom and the special scope import.
Q: How can one Maven module depend on another module?
A module can use another module by adding a normal dependency declaration containing the target module's group ID, artifact ID, and version. In the demonstration, the second subproject depends on the first and can instantiate its entity class, set fields, and call its methods. Removing that declaration makes the class unavailable to the consuming module.
Q: Which dependency scopes are available through an imported module?
Dependencies with compile or runtime scope in an imported module are accessible from the consuming module, according to the demonstration. Dependencies using other scopes, including test and provided, are not accessible that way. The Lombok example becomes available to the second module only after its scope in the first module changes from provided to compile.
Summary & Key Takeaways
-
A multi-module Maven project consists of several smaller projects coordinated by a root POM. The root uses pom packaging because it contains no source code, and its modules block identifies the folders containing module POM files. Modules can inherit common configuration and dependencies from this parent, which is also called the reactor POM.
-
The dependencyManagement block centralizes dependency versions, scopes, and other configuration without automatically adding those libraries to every module. A module must still declare each dependency it needs, but it may omit configuration already managed by the root. This keeps module outputs lighter and avoids repeating version changes across several POM files.
-
A BOM is a separate POM containing dependencyManagement configuration. After installation in the local repository, projects import it with type pom and scope import. Modules can also depend directly on sibling modules by their Maven coordinates, but cyclic module relationships are forbidden, and dependency availability is affected by scopes such as compile, runtime, test, and provided.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Nikolay Panyukov 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
