YouTip LogoYouTip

Maven Multi Module

Maven Multi-module Project Management | Rookie Tutorial Maven Multi-module Project refers to a project structure where a parent project contains multiple submodules. This structure allows us to split a large project into multiple logically independent yet interconnected modules. Each module can be built individually or together as a whole. ### Advantages of Multi-module Projects 1. **Code Reusability**: Common code can be extracted into a separate module for use by other modules 2. **Separation of Responsibilities**: Different teams can focus on developing different modules 3. **Build Efficiency**: Only build the modules that have changed, reducing build time 4. **Dependency Management**: Unify the management of dependencies for all modules 5. **Version Control**: All modules use a unified version number, making management easier ### Applicable Scenarios * Layered large projects (e.g., `web`, `service`, `dao`) * Microservices architecture (each service as a module) * Sharing common code (e.g., `common` module) ### Standard Directory Structure parent-project/ # Parent project root directoryβ”œβ”€β”€ pom.xml # Parent POM (packaging=pom)β”œβ”€β”€ module-a/ # Submodule Aβ”‚ β”œβ”€β”€ src/β”‚ └── pom.xml # Submodule A's POMβ”œβ”€β”€ module-b/ # Submodule Bβ”‚ β”œβ”€β”€ src/β”‚ └── pom.xml # Submodule B's POM└── module-web/ # Web module β”œβ”€β”€ src/ └── pom.xml **Key Features:** 1. **Parent POM**: * Must set `pom` * Manage submodules via `` 2. **Submodules**: * Inherit the parent POM via `` * Can have their own dependencies and build configurations * * * ## Creating a Maven Multi-module Project ### 1. Create the Parent Project The parent project itself usually does not contain any code; it is primarily used to manage submodules and common configurations. Creating the parent project's pom.xml file requires setting the packaging to pom: 4.0.0 com.example parent-project 1.0.0 pom module1 module2 ### 2. Create Submodules Submodules are actual code modules, which can be ordinary Java projects, web applications, etc. Each submodule has its own pom.xml file but needs to declare the parent project: com.example parent-project 1.0.0 4.0.0 module1 * * * ## Multi-module Project Dependency Management In a multi-module project, modules can depend on each other. For example, module2 depends on module1: com.example module1 ${project.version} ### Dependency Inheritance The parent project can define common dependencies, and submodules will automatically inherit these dependencies: junit junit 4.12 test Submodules only need to declare the dependency's groupId and artifactId without specifying the version: junit junit * * * ## Multi-module Project Building ### 1. Build the Entire Project Execute in the parent project directory: mvn clean install This will build all submodules in dependency order. ### 2. Build a Single Module Navigate to the specific module directory and execute: cd module1 mvn clean install Or specify the module from the parent project directory: mvn -pl module1 clean install ### 3. Build a Module and Its Dependencies mvn -pl module1 -am clean install * * * ## Multi-module Project Best Practices ### 1. Reasonable Module Division * Divide modules by function * Divide modules by layer (e.g., dao, service, web) * Extract common utilities into a separate module ### 2. Version Management * Use the parent project to unify version number management * Consider using Maven's version plugin to manage version upgrades ### 3. Build Optimization * Configure appropriate build order * Use profiles to manage builds for different environments * Consider parallel builds to improve efficiency ### 4. Dependency Management * Centralize management of common dependencies in the parent project * Use dependencyManagement to unify versions * Avoid circular dependencies * * * ## Common Issues and Solutions ### 1. Circular Dependency Issue **Problem**: Module A depends on Module B, and Module B depends on Module A **Solution**: * Redesign the module structure, extract common code into a third module * Use interfaces for decoupling ### 2. Build Order Issue **Problem**: Maven cannot correctly identify the dependency order between modules **Solution**: * Explicitly declare module dependencies * Use the reactor plugin to analyze the build order ### 3. Version Inconsistency Issue **Problem**: Different modules use different versions of dependencies **Solution**: * Unify dependency version management in the parent project * Use dependencyManagement By properly using Maven multi-module project management, the maintainability and build efficiency of large projects can be significantly improved. Mastering these skills will help you better organize and manage complex Java projects.
← Jsref Array TolocalestringMaven Commands β†’