IDEA creates SpringBoot parent-child project agreement

1. Preface

Java development specifications follow the unspoken rule that convention is greater than configuration. Please don’t mess around when learning or getting started with unfamiliar things! Search for tutorials first!

2. Create a SpringBoot parent project

2.1 Create a project

2.2 Fill in information


I won’t say much here, but the key point is that it is best to create a parent project with a SpringBoot template. Fill in other information. If you don’t know, it is recommended to review the SpringBoot tutorial or a more basic tutorial.

2.3 Check dependencies

Check dependencies as needed, I only made web dependencies here

2.4 Modify pom dependencies

Check the dependencies, after the creation is complete, wait for the maven build of the project. After the maven build is completed, some places need to be modified, as follows:

Here, the packaging method of the parent project is pom, and modules are added in order to be able to add submodules later. After modification, it looks like this:


Parent project pom.xml file:

<?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>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.zrz</groupId>
    <artifactId>ps-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>ps-learning</name>
    <description>ps-learning</description>

    <properties>
        <java.version>17</java.version>
    </properties>

    <modules>
        
    </modules>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <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>
            </plugin>
        </plugins>
    </build>

</project>

3. Create a subproject

3.1 Create a module


Here is also created with SpringBoot template

Here you can not check the dependencies, only check the plug-ins

3.2 Configuring parent-child projects

The subproject pom.xml file at this time is:

There are three places to modify here, the first one is to modify the parent. Second, modify the packaging method. The third is to remove redundant dependencies. The modified sub-project pom.xml file is as follows:

<?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.zrz</groupId>
        <artifactId>ps-learning</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <groupId>com.zrz</groupId>
    <artifactId>s-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>s-learning</name>
    <description>s-learning</description>
    
    <properties>
        <java.version>17</java.version>
    </properties>
    
    <dependencies>

    </dependencies>

</project>

3.3 Add modules to the parent project pom.xml

 <modules>
        <module>s-learning</module>
    </modules>

4. Test whether the startup is successful

4.1 Write a Controller

@RestController
public class HelloController {<!-- -->

    @GetMapping("hello")
    public String hello(){<!-- -->
        return "hello";
    }
}

4.2 Access

5. Supplement

5.1 pom.xml, dependencyManagement and dependencies

The dependencyManagement tag is a tag specified by the management package and is mostly used for parent-child projects. The tag is defined first, but loaded lazily. The jar package is only loaded when it is declared to be used in a subproject. The sub-project can be used without declaring the version number. The version number is defined in the dependencyManagement of the parent project, and the GA coordinates are referenced in the sub-project. dependencies can directly be inherited by sub-projects from the jar package of the parent project. So pay attention to the difference between the two. A specific example can be withdrawn during the construction of the dubbo project:

parent project

 <!-- Dubbo -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

sub-item

 <dependencies>
        <!-- <dependency>-->
        <!-- <groupId>org.projectlombok</groupId>-->
        <!-- <artifactId>lombok</artifactId>-->
        <!-- <optional>true</optional>-->
        <!-- </dependency>-->

        <!-- <dependency>-->
        <!-- <groupId>org.apache.dubbo</groupId>-->
        <!-- <artifactId>dubbo-spring-boot-interface</artifactId>-->
        <!-- <version>${project.parent.version}</version>-->
        <!-- </dependency>-->

        <!-- dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- spring boot starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zrz</groupId>
            <artifactId>provider-server</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>