Integrate the pom.xml files of multiple springboot projects

Integrate the pom.xml files of multiple springboot projects

0.0, antecedent

​ When I first entered the company and typed the code, I found that a project would contain multiple sub-projects, and each sub-project would represent a functional module. This really surprised me, a rookie. And this divide-and-conquer method also leads to a question: how to manage the dependencies of each sub-project in a unified way?

We know that each project will import specific dependencies, and each dependency has several versions. If this version is used in this project and that version is used in that project, it is easy to cause confusion and version conflicts. Therefore, dependencies can be divided into parent-child relationships. That is, a parent dependency manages several child dependencies.

Go directly to the code:

parent dependency:

<?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 https://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.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.hcgx-root</groupId>
    <artifactId>hcgx-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <packaging>pom</packaging>

    <modules>
        <module>hcgx-service</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <mysql-connector.version>8.0.20</mysql-connector.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--Mysql数据库驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1. What is this thing?

First of all: parentthe meaning can be understood as inheritance. The overall meaning is that this xmlwill inherit spring-boot-starter-parentall dependencies in the file, which is equivalent to inheriting parent dependencies. And this thing exists when every springboot is initialized, and it is automatically configured by springboot for us. The purpose is to omit some common dependencies when we create the sping project

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

2. Indicates maven version, must fill in 4.0.0 to use multi-maven project integration

 <modelVersion>4.0.0</modelVersion>

3. The name of this xml file is used later when it is associated with a sub-project

<groupId>com.hcgx-root</groupId>
<artifactId>hcgx-root</artifactId>
<version>0.0.1-SNAPSHOT</version>

4. The packaging method will be packaged into jar by default. Because the parent project is only used to coordinate the dependencies of the subprojects, it is set to pom, which means it will not be moved.

Each pom.xml file represents a maven project, which needs to be combined with this file to package the project into a jar package, and the purpose of packaging the program into a jar package is because there are java codes in the project and these codes need to use these dependencies, and the parent The src directory has been deleted in the project, regardless of the sub-projects in it, so there is no reason to be packaged into a jar package (the sub-project itself has a pom.xml file to package itself into a jar package)

 <packaging>pom</packaging>

5. It is used to associate the name of the sub-project, hcgx-servicewhich is the name of the sub-project (it is true that it is going in both directions)

<modules>
    <module>hcgx-service</module>
</modules>

6. The dependent version management in this tag mysql-connector.versionis a custom tag. When using it, ${mysql-connector.version}use the version number under the corresponding tag (Note: the version number must be real, otherwise it will become popular when using a custom tag)

<properties>
    <java.version>1.8</java.version>
    <mysql-connector.version>8.0.20</mysql-connector.version>
    <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
</properties>

7. dependencyManagementThe label represents the template, which is used directly under normal circumstances dependencies(that is, the outermost layer does not have it dependencyManagement), indicating that this dependency is imported, and dependencyManagementthe label is only used as a dependent template <version>${mysql-connector.version}</version>to store the version number of the dependent template, which is directly used by subsequent sub-projects:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

You can import dependencies without specifying a dependency version.

<dependencyManagement>
        <dependencies>
            <!--Mysql数据库驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

8. dependenciesThe dependencies imported in the parent project will be imported by default by all sub-projects

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

sub-items:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.hcgx-root</groupId>
        <artifactId>hcgx-root</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.hcgx-service</groupId>
    <artifactId>hcgx-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hcgx-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1. parentThe tag is used to describe the object that needs to be inherited, that is, after using this tag, we will inherit groupIdthe parent project com.hcgx-rootof is and artifactIdis hcgx-root, this content is actually in step 3 above.

<parent>
    <groupId>com.hcgx-root</groupId>
    <artifactId>hcgx-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

2. Your own name, etc.

<groupId>com.hcgx-service</groupId>
<artifactId>hcgx-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hcgx-service</name>
<description>Demo project for Spring Boot</description>

3. No need to specify the version number, the parent project specifies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

0.1, file directory

insert image description here

Guess you like

Origin blog.csdn.net/qq_43483251/article/details/129155913