Create a Maven aggregation project to realize component reuse

A Maven aggregation project allows you to create multiple components (projects) in a Maven parent project. These multiple components can depend on each other to achieve component reuse.

1. Create a normal Maven project

Insert image description here
Creation completed
Insert image description here

2. Modify the pom.xm of the parent project and set the packaging method to pom.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.study</groupId>
    <artifactId>Maven-study</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>

Insert image description here
The parent project is used to manage sub-projects and does not implement business, so the src directory can be selectively deleted.

3. Create Module

Select the parent project-right-click-New-Module, then select the project type you want to create
Insert image description here
and enter the sub-project name (inherited from the parent project).
Insert image description here
The module's pom will inherit the parent project's pom.
Insert image description here
The parent project's pom will declare the child of the current parent project. Modules
Insert image description here
generally only store pojo, dao and other reusable classes in common, and then call them in other modules.

4. Create two SpringBoot submodules

Insert image description here

5. Modify the parent project pom file to inherit spring-boot-starter-parent, declare the sub-module, and add the dependencies that both projects need to use.

Insert image description here

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.study</groupId>
    <artifactId>Maven-study</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>common</module>
        <module>manager-system</module>
        <module>user-system</module>
    </modules>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

6. Inherit the parent project in the two child modules and delete the declared dependencies in the parent project

Insert image description here

By introducing the common module into a sub-project, you can call the classes in the common. Before doing this, you need to compile and package the common module and
Insert image description here
Insert image description here
add dependencies in dependency management: dependencyManagement in the parent project, which means defining the default version of this dependency in the sub-project.

Guess you like

Origin blog.csdn.net/qq_42042158/article/details/121656785