[springboot advanced] Build based on the starter project (1) parent project

Table of contents

1. Build your own parent project

1. Create a parent project

2. Build your own project dependencies

2. Build the parent project of Spring Cloud

1. Create a parent project

2. Build project dependencies

3. Parent project packaging


This series explains how the project is built, mainly using the combination of the parent project parent and the custom starter. The project uses the latest springboot3 and jdk19. For the code repository of this series, see  the gitee repository

In this article, we will learn about the construction of the parent project, and build the required project dependency packages for the Spring Boot projects created in the future.

1. Build your own parent project

If you have used Spring Boot, then you must know that the project needs to introduce a spring-boot-starter-parent, which is the parent dependency of Spring Boot, which is used to provide related Maven default dependencies.

Then we can similarly build our own parent-level dependencies, and other projects can inherit parent-level dependencies after they are introduced.

1. Create a parent project

We create a parent project, created in the way of Spring Initializr, and the project is named backend-parent.

 Spring Boot uses the latest version 3.0.0. The following dependencies do not need to be checked, just click "Create" to create the project.

 After the project is created, we delete unnecessary code packages and only need to leave a pom.xml file. 

2. Build your own project dependencies

Paste the Maven dependency package introduced by the pom file. Here you can import the required dependency package according to your actual project situation.

<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath />
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <modules>
        <module>backend-cloud-parent</module>
    </modules>

    <groupId>org.liurb.springboot.scaffold</groupId>
    <artifactId>backend-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>
    <name>backend-parent</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>19</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

        <fastjson.version>2.0.19</fastjson.version>
        <hutool.version>5.8.10</hutool.version>
        <commons-pool2.version>2.11.1</commons-pool2.version>
        <mybatis-plus-boot-starter.version>3.5.2.7-SNAPSHOT</mybatis-plus-boot-starter.version>
        <mybatis-plus-generator.version>3.5.3</mybatis-plus-generator.version>
        <mybatis-plus-join.version>1.3.7</mybatis-plus-join.version>
        <javax.mail.version>1.4.7</javax.mail.version>
        <bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
        <java-jwt.version>4.2.1</java-jwt.version>
        <mysql-connector-java.version>8.0.31</mysql-connector-java.version>
        <commons-lang3.version>3.12.0</commons-lang3.version>
        <okhttp.version>4.10.0</okhttp.version>
        <mapstruct.version>1.5.3.Final</mapstruct.version>

        <gson.version>2.10</gson.version>

        <junit.version>4.11</junit.version>
    </properties>

    <dependencyManagement>

        <dependencies>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba.fastjson2</groupId>
                <artifactId>fastjson2-extension</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>${commons-pool2.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus-boot-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>${mybatis-plus-generator.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.yulichang</groupId>
                <artifactId>mybatis-plus-join</artifactId>
                <version>${mybatis-plus-join.version}</version>
            </dependency>

            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>${javax.mail.version}</version>
            </dependency>

            <dependency>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bcprov-jdk15on</artifactId>
                <version>${bcprov-jdk15on.version}</version>
            </dependency>

            <dependency>
                <groupId>com.auth0</groupId>
                <artifactId>java-jwt</artifactId>
                <version>${java-jwt.version}</version>
            </dependency>

            <dependency>
                <groupId>com.mysql</groupId>
                <artifactId>mysql-connector-j</artifactId>
                <version>${mysql-connector-java.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>

            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>${okhttp.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>

            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>${gson.version}</version>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>

        </dependencies>

    </dependencyManagement>

</project>

The parent project can first declare some public dependency packages through the dependencyManagement tag. If a subproject needs to use which dependency package, it only needs to be declared in the pom file of the project without filling in the version number, so that it can inherit the parent The version number of the project, and the version number of dependencies can be managed uniformly through the parent project in the future.

Maven can manage dependencies through the dependencyManagement element, which has the following two characteristics:

  • Dependencies declared under this element will not be actually introduced into the module, and will only be introduced into the module if the dependency is also declared under the dependencies element.
  • This element can restrict the use of dependencies under dependencies, that is, if the dependencies declared by dependencies do not specify a version, the version specified in dependencyManagement will be used, otherwise the version in dependencyManagement will be overwritten.

2. Build the parent project of Spring Cloud

Because the general Spring Cloud project is a Spring Boot project, we only need to inherit the parent project created above to build the Spring Cloud parent project.

1. Create a parent project

Here we create it as a module, and the command is backend-cloud-parent.

 Similarly, after the project is created, we delete unnecessary code packages and only need to leave a pom.xml file. 

2. Build project dependencies

Paste the Maven dependency package imported by the pom file, because it inherits the parent project defined above, so here you only need to declare the dependency package that the Spring Cloud project needs to use.

<?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">
    <parent>
        <artifactId>backend-parent</artifactId>
        <groupId>org.liurb.springboot.scaffold</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>backend-cloud-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>

    <name>backend-cloud-parent</name>

    <properties>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
    </properties>

    <dependencyManagement>

        <dependencies>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>

    </dependencyManagement>

</project>

Special attention should be paid to the fact that the type tag is imported in the form of pom, while the scope tag is import, which means that the submodule is forced to use the same version number as the parent module.

3. Parent project packaging

The parent project has been built, so how to use these two parent projects? The answer is to package them and import them through Maven dependencies through other projects.

The structures of the two parent projects currently created are as follows.

  In general production, we will put this kind of project in the server private server warehouse (such as Alibaba Cloud's cloud effect), because our side is just a demonstration, so we only need to hit the local warehouse.

Open the Maven tab on the right side of Idea, click backend-parent, double-click package, after execution, double-click install to install to the local warehouse.

Here is to package from the root (root) project, then the sub-project backend-cloud-parent will also be packaged together, if only sub-projects need to be packaged, then open the command under the corresponding project to package.

At this time, open the local warehouse, and you can see the two directories we just packaged.

Open the backend-parent directory in turn, and you will find that there is only one pom file in it, because the type we defined is pom 

At this point, the parent project that belongs to itself is built, and we can introduce this parent project when creating a subproject in the future.

Guess you like

Origin blog.csdn.net/lrb0677/article/details/127156397