Create a multi module project for springboot

Posted by medusa1414 on Sun, 02 Jan 2022 15:34:05 +0100

Use IDEA to create a new project for maven to manage spring boot (because the community version is used, the plug-in of alibaba is downloaded to help create ~)

Pay attention to modifying the packge. If you use the plug-in, the packge under it will not change. It has always been (PIT)

Top level parent project created successfully! The top-level project does not have to worry about specific business logic, but only completes one task: managing sub modules and defining the versions of Maven dependencies. Now delete all files except pom files and configure pom files

 

<?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.4.1</version>
    </parent>

    <groupId>com.geekbang</groupId>
    <artifactId>coupon</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>jakarta.validation</groupId>
                <artifactId>jakarta.validation-api</artifactId>
                <version>2.0.2</version>
            </dependency>

            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>16.0</version>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

 

There are three key tags in the pom file.

< parent > label

In the parent tag, we specify the "parent dependency" of the project as Spring Boot starter parent. In this way, the Spring Boot component version information defined in Spring Boot starter parent will be automatically brought to the sub module. This approach is also common to most Spring Boot projects. It not only reduces the cost of dependency management, but also does not need to worry about the compatibility between components.

< packaging > label

maven has three packaging types: jar, war, and pom. When we specify the packaging type as pom, it means that the current module is a "boss", which only focuses on the top-level strategy, that is, defining dependency versions and integration sub modules, and does not contain specific business implementations.

< dependency Management > tab

The function of this tag is similar to that of the < parent > tag. Both pass down version information. Dependency management is where the boss es define the top-level strategy. We can define the versions of various dependencies here. When the subproject needs to introduce these dependencies, only specify the groupId and artifactId, regardless of which version to write in the version. After completing the writing of geekbang coupling dependency, let's take a look at the writing of coupling template serv dependency.  

Create a child project under the parent project and click file - > New - > module

As for the parent project, remember to modify the package name without the same!

After creation, if the pom file is not blue but orange, you can right-click the pom file and select add as a maven project. The completed project structure is as follows:

Now you need to configure the top-level project pom file and sub project pom file

The top-level project pom file needs to add sub projects

<modules>
        <module>coupon-api</module>
</modules>

The sub project pom file needs to be labeled with < parent >

<parent>
    <artifactId>coupon</artifactId>
    <groupId>com.geekbang</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

Create another sub project, as above. If you want to call the service of the first sub project in the second sub project, you can add dependencies in the < dependencies > tag in the pom file of the second sub project

<dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>coupon-api</artifactId>
            <version>${project.version}</version>
        </dependency>

Simple demo complete!!!!!!!!!

Topics: Java Maven intellij-idea