Maven Multi-module Project Management
A Maven multi-module project (Multi-module Project) refers to a project structure in which a parent project (parent project) contains multiple submodules (submodules). This structure allows us to split a large project into modules that are logically independent but interrelated, each module can be built separately or built together as a whole.
Advantages of Multi-module Projects
- Code Reuse: Common code can be extracted into separate modules for use by other modules
- Separation of responsibilities: Different teams can focus on the development of different modules
- Build efficiency: Only build modules that have changed, reducing build time
- Dependency Management: Unified management of dependencies of all modules
- Versioning: All modules use a unified version number, easy to manage
Applicable scenarios
-
Large project layering (such as
web、service、dao) -
Microservice architecture (one module per service)
-
Shared common code (such as
commonmodules)
Standard directory structure
parent-project/ # 父项目根目录
├── pom.xml # 父POM(packaging=pom)
├── module-a/ # 子模块A
│ ├── src/
│ └── pom.xml # 子模块A的POM
├── module-b/ # 子模块B
│ ├── src/
│ └── pom.xml # 子模块B的POM
└── module-web/ # Web模块
├── src/
└── pom.xml
Key features:
-
Parent POM:
Must be set
Pass Manage submodules
Must be set
Pass Manage submodules
-
Submodules:
Pass Inherit parent POM
Can have its own dependencies and build configuration
Pass Inherit parent POM
Can have its own dependencies and build configuration
Creating a Maven Multi-module Project
1. Create the parent project
The parent project itself usually does not contain any code; it is mainly used to manage submodules and common configuration.
To create the parent project's pom.xml file, you need to set packaging to pom:
4.0.0
com.example
parent-project
1.0.0
pom
module1
module2
2. Create submodules
Submodules are the 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 automatically inherit these dependencies:
junit
junit
4.12
test
Submodules only need to declare the dependency's groupId and artifactId, no need to specify the version:
junit
junit
Multi-module Project Build
1. Build the whole project
Execute in the parent project directory:
mvn clean
install
This will build all submodules in dependency order.
2. Build a single module
Enter a 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 (such as dao, service, web)
- Extract common utilities into a separate module
2. Version management
- Use the parent project to manage version numbers uniformly
- Consider using Maven's versions plugin to manage version upgrades
3. Build optimization
- Configure an appropriate build order
- Use profiles to manage builds for different environments
- Consider parallel builds to improve efficiency
4. Dependency management
- Centrally manage common dependencies in the parent project
- Use dependencyManagement to unify versions
- Avoid circular dependencies
Common Problems and Solutions
1. Circular dependency problem
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 to decouple
2. Build order problem
Problem: Maven cannot correctly identify the dependency order between modules
Solution:
- Explicitly declare module dependencies
- Use the reactor plugin to analyze build order
3. Version inconsistency problem
Problem: Different modules use different versions of dependencies
Solution:
- Manage dependency versions uniformly in the parent project
- Use dependencyManagement
Through reasonable use of Maven multi-module project management, you can significantly improve the maintainability and build efficiency of large projects. Mastering these techniques will help you better organize and manage complex Java projects.