SoFunction
Updated on 2025-04-28

Summary of the association problem between parent class and child class in SpringBoot project multi-module project

1: Configuration of parent class POM

  • Parent POM via<modules>Declare the submodule and pass<dependencyManagement>Manage dependent versions.
  • Sub-POM by<parent>Specify the parent POM and inherit the configuration of the parent POM.
  • The connection method of father-son POM is the basis of multi-module projects, which can effectively manage dependencies and configurations and improve the maintainability of projects.
  • The parent class POM is an aggregation project (packagingforpom), it passes<modules>Tag declares submodules and pass<dependencyManagement>Manage dependent versions.
&lt;project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;!-- Basic information of parent project --&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;parent-project&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt; &lt;!-- The parent project must be pom type --&gt;
    &lt;!-- Submodule declaration --&gt;
    &lt;modules&gt;
        &lt;module&gt;module1&lt;/module&gt;
        &lt;module&gt;module2&lt;/module&gt;
    &lt;/modules&gt;
    &lt;!-- Dependency management --&gt;
    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
                &lt;version&gt;2.6.13&lt;/version&gt;
            &lt;/dependency&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;mysql&lt;/groupId&gt;
                &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                &lt;version&gt;8.0.24&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;
    &lt;!-- General configuration --&gt;
    &lt;properties&gt;
        &lt;&gt;1.8&lt;/&gt;
    &lt;/properties&gt;
&lt;/project&gt;

2: Configuration of subclass POM

Subclass POM via<parent>Tags specify the coordinates of the parent POM (groupIdartifactIdversion) and inherit the configuration of the parent POM. Submodules can add their own dependencies and configurations.

&lt;project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;!-- Specify parent POM --&gt;
    &lt;parent&gt;
        &lt;groupId&gt;&lt;/groupId&gt;
        &lt;artifactId&gt;parent-project&lt;/artifactId&gt;
        &lt;version&gt;1.0.0&lt;/version&gt;
        &lt;relativePath&gt;../&lt;/relativePath&gt; &lt;!-- father POM Relative paths --&gt;
    &lt;/parent&gt;
    &lt;!-- Submodule basic information --&gt;
    &lt;artifactId&gt;module1&lt;/artifactId&gt;
    &lt;!-- Dependencies of submodules --&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

3: The connection method of father and son POM

3.1 Parent POM Declaration Submodule

In the parent POM, use<modules>Tag declaration submodule:

<modules>
    <module>module1</module>
    <module>module2</module>
</modules>

3.2 Child POM Specify the parent POM

In sub-POM, use<parent>The tag specifies the coordinates of the parent POM:

&lt;parent&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;parent-project&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
    &lt;relativePath&gt;../&lt;/relativePath&gt; &lt;!-- father POM Relative paths --&gt;
&lt;/parent&gt;

3.3 Dependence on inheritance

Submodules can inherit from parent POM<dependencyManagement>Managed dependencies, no need to specify a version number:

<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

4: Working mechanism of father and son POM

  • Dependency management: Parent POM Passed<dependencyManagement>Manage dependency versions, submodules inherit these dependencies.
  • Configuration Inheritance: in parent POM<properties><build>The configuration will be inherited by the submodule.
  • Module aggregation: Parent POM Passed<modules>Aggregate submodules, all submodules will be built in order when constructed.

5: Things to note

1: Parent POM'spackagingMust bepom

<packaging>pom</packaging>

2: Submodule<parent>The coordinates of the parent POM must be specified correctly

<parent>
    <groupId></groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <relativePath>../</relativePath>
</parent>

3: Submodule<artifactId>Must be unique:

Each submodule'sartifactIdCan't be repeated.

4: Dependency version management:

The submodule inherits the dependent version of the parent POM. If the version needs to be overridden, it can be explicitly specified in the submodule.

Summarize

Parent POM via<modules>Declare the submodule and pass<dependencyManagement>Manage dependent versions.

Sub-POM by<parent>Specify the parent POM and inherit the configuration of the parent POM.

The connection method of father-son POM is the basis of multi-module projects, which can effectively manage dependencies and configurations and improve the maintainability of projects.

This is the article about the detailed explanation of the relationship between parent classes and subclasses in the SpringBoot project multi-module project. For more related contents of SpringBoot parent classes and subclasses, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!