Fool learning ⭐ Create a multi module parent-child project in IDEA ⭐

Create a multi module parent-child project in IDEA

1, Why create a multi modular project

As we all know, Web engineering is a three-tier architecture of MVC, and there are generally two login portals: foreground and background in the system.

In this case, the user usually logs in at the foreground and the administrator logs in at the background to view the system information and system maintenance.

Therefore, there will be two View layers, corresponding to two controller layers.

Let's leave View aside. This is what the front-end does. At this time, the back-end corresponds to these architecture layers: the front-end control layer (Manager), the back-end control layer (Controller), the Service layer (Service) and the data layer (Dao);

The control layer uses different interfaces, but the service layer and data layer use the same interface.

There is a design principle of "high cohesion and low coupling" in the structure of Java. The above is obviously inconsistent. Although the hierarchy is divided by package name, for the architecture level, everything is coupled together. Therefore, we need to carry out multi module division project.

A multi-level structure should be set as follows:

---- app-xxx
	|-- app-controller
	|        |-- pom.xml (jar)
	|
	|-- app-Manager
	|        |-- pom.xml (jar)
	|
	|-- app-Service
	|        |-- pom.xml (jar)
	|
	|-- app-Dao
	|        |-- pom.xml (jar)
	|
	|-- pom.xml (war)   

In the above, APP controller, APP manager, APP service and app Dao are different architecture layers. App controller, APP service and app Dao constitute a set of services, and app manager, APP service and app Dao also constitute a set of services. Obviously, APP controller and app manager share the same set of APP service and app Dao.


2, Maven creates a multi module parent-child project

In the previous blog, when a file carries multiple projects, it creates an empty project to carry. In this blog, it is a multi module parent-child project. Multiple modules are related to each other. Maven is used to carry our multiple modules.

1. Create parent project (Maven project)



Then we click Next to create the maven project.

Here we have created the maven project. The structure is as follows:

We need to deal with it!

Delete the src file. The src file saves the source code, but our multi module code is stored in multiple architecture layers, so we don't need this src to store the code. The change diagram is as follows:

2. Create subproject (Maven project)

Just started a New Moudle.


Click Next.

The Spring Web project is created. Select Finish to complete the creation.

Start SpringBoot and test!

Successful. The architecture layer is as follows.

In addition, the pom.xml file in the outermost maven project

  1. You need to add a new module tag
  2. Add package as pom
  3. And add a parent tag
  4. Add the springboot dependency package.

The complete 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 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.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <packaging>pom</packaging>

    <modules>
        <module>springbootManager</module>
    </modules>

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

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

    <groupId>org.wang</groupId>
    <artifactId>maven-moremodule</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

For. xml files in subprojects

  1. You need to change its parent label to its parent project
  2. Add package as jar
  3. Delete the dependent package (inheriting the dependent package of the parent project)

The complete 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>org.wang</groupId>
        <artifactId>maven-moremodule</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <packaging>jar</packaging>
    
    <groupId>com.wang</groupId>
    <artifactId>springbootManager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootManager</name>
    <description>springbootManager</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Start the test Manager again;

It can be started normally, which proves that it inherits the dependency of the parent project.

Then create three architecture layers in the same way: Controller, Service and Dao. The architecture layer is as follows:

We can delete the files in springboot that do not affect the project first.

These can be deleted. Some projects have an. iml file, which cannot be deleted. After my project is created, there is no such file.

The document structure is as follows:

Clean. Next, modify the pom.xml file and the parent 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 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.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <packaging>pom</packaging>

    <modules>
        <module>springbootManager</module>
        <module>springbootController</module>
        <module>springbootService</module>
        <module>springbootDao</module>
    </modules>

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

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

    <groupId>org.wang</groupId>
    <artifactId>maven-moremodule</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

The files of the subproject are the same as those of the Manager layer. Only the files of the Dao layer are given below.

<?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.wang</groupId>
        <artifactId>maven-moremodule</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <packaging>jar</packaging>

    <groupId>com.wang</groupId>
    <artifactId>springbootDao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootDao</name>
    <description>springbootDao</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

We can test whether each layer inherits the parent project. Before the test, close the last test item first, and the port will be occupied.

For interface calls, the Controller layer needs to call the methods of the Service layer.

Import the Service layer in the Controller layer.

    <dependencies>
        <dependency>
            <groupId>com.wang</groupId>
            <artifactId>springbootService</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

Write Service method:

Write the method of Controller layer and call the method of Service layer:

Some students can't introduce classes in the Service layer. Please configure Maven.

package com.wang.controller;

import com.wang.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("hello")
    @ResponseBody
    public String hello() {
        return helloService.hello();
    }

}

Call to test:

Start the startup class in the Controller layer.


If successful, we can decouple the modules of the project.

In addition, the dependency imported by the parent project can be inherited by the child project.

The parent project imports the lombok dependent package.

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

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


There will also be in the subproject.

Welcome to leave a message!!!

Tags: Java Maven IDEA

Posted on Sat, 04 Sep 2021 18:04:31 -0400 by Toy