Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven Multi-module Project Management

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

  1. Code Reuse: Common code can be extracted into separate modules for use by other modules
  2. Separation of responsibilities: Different teams can focus on the development of different modules
  3. Build efficiency: Only build modules that have changed, reducing build time
  4. Dependency Management: Unified management of dependencies of all modules
  5. Versioning: All modules use a unified version number, easy to manage

Applicable scenarios

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:

  1. Parent POM:

  2. Must be set pom

  3. Pass Manage submodules

  4. -

    Must be set pom

    -

    Pass Manage submodules

    1. Submodules:

    2. Pass Inherit parent POM

    3. Can have its own dependencies and build configuration

    4. -

      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

      2. Version management

      3. Build optimization

      4. Dependency management


      Common Problems and Solutions

      1. Circular dependency problem

      Problem: Module A depends on module B, and module B depends on module A
      Solution:

      2. Build order problem

      Problem: Maven cannot correctly identify the dependency order between modules
      Solution:

      3. Version inconsistency problem

      Problem: Different modules use different versions of dependencies
      Solution:

      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.