Java single application - common framework - 07.Spring MVC - Maven modular development (iot-

Original address: http://www.work100.net/training/monolithic-frameworks-spring-mvc-maven-module.html
More tutorials: Beam cloud - free course

Maven modular development

Serial number Chapter in text video
1 Summary -
2 Create root project -
3 Create a unified dependency management module -
4 Create a common tool class module -
5 Create domain model module -
6 Create management background module -
7 Create front end console module -
8 Create interface module -
9 Clean up, compile, package -
10 Functional perfection -
11 Example source -

Please refer to the navigation above for reading

1. overview

In order to facilitate the code maintenance and management in the future, we will subdivide the work of each developer into specific functions and modules, especially for large-scale projects.
With the continuous expansion of the project, there will be more and more modules, which will be more difficult to maintain and expand in the future. In order to cope with this situation, we will use Maven modular development mode, and later we will use micro service architecture for development.

This chapter takes building an IoT cloud platform project as an example. We can divide the modules into the following forms:

  • Unified dependency management
  • Common tools
  • domain model
  • Administrative background (admin)
  • Front end console
  • Interface module (api)

The whole modular development process is mainly to make some changes in the development idea, just follow the following process.

2. Create root project (project)

Create a project named IOT cloud. The 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>

    <groupId>net.work100.training.stage2</groupId>
    <artifactId>iot-cloud</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>

    </modules>
</project>

This project is called Root project. Its main function is to manage all modules of the whole project. When new modules are added, the corresponding module directory needs to be configured under the modules element

3. Create a unified dependency management module

Create a project named IOT cloud dependencies. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-dependencies</artifactId>
    <packaging>pom</packaging>

    <name>iot-cloud-dependencies</name>
    <description></description>

    <properties>
        <!-- Environmental configuration -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <!-- Unified dependency management -->
        <spring.version>5.2.3.RELEASE</spring.version>

        <javax.servlet-api.version>4.0.1</javax.servlet-api.version>
        <javax.jstl.version>1.2</javax.jstl.version>

        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.25</slf4j.version>

        <commons-lang3.version>3.5</commons-lang3.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Begin -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- Spring End -->

            <!-- Servlet Begin -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>${javax.servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${javax.jstl.version}</version>
            </dependency>
            <!-- Servlet End -->

            <!-- Log Begin -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Log End -->

            <!-- Commons Begin -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <!-- Commons End -->
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- Compiler Plug-in unit, Set up JDK Edition -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>

        <!-- Resource file configuration -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

Then modify the pom.xml file of the IOT cloud project:

<?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>

    <groupId>net.work100.training.stage2</groupId>
    <artifactId>iot-cloud</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>iot-cloud-dependencies</module>
    </modules>
</project>

4. Create a general tool module

Create a project named IOT cloud Commons. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../iot-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-commons</artifactId>
    <packaging>jar</packaging>

    <name>iot-cloud-commons</name>
    <description></description>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
</project>

Then add & lt; Module & gt; IOT cloud commons & lt; / module & gt; configuration to pom.xml of IOT cloud project

5. Create domain model module

Create a project named IOT cloud domain. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../iot-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-domain</artifactId>
    <packaging>jar</packaging>

    <name>iot-cloud-domain</name>
    <description></description>

</project>

Then add & lt; Module & gt; IOT cloud domain & lt; / module & gt; configuration to pom.xml of IOT cloud project

6. Create management background module

Create a project named IOT cloud web admin. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../iot-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-web-admin</artifactId>
    <packaging>war</packaging>

    <name>iot-cloud-web-admin</name>
    <description></description>

    <dependencies>
        <dependency>
            <groupId>net.work100.training.stage2</groupId>
            <artifactId>iot-cloud-domain</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>
</project>

Then add & lt; Module & gt; IOT cloud web Admin & lt; / module & gt; configuration to pom.xml of IOT cloud project

7. Create the front console module

Create a project named IOT cloud web console. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../iot-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-web-console</artifactId>
    <packaging>war</packaging>

    <name>iot-cloud-web-console</name>
    <description></description>
</project>

Then add & lt; Module & gt; IOT cloud web console & lt; / module & gt; configuration to pom.xml of IOT cloud project

8. Create interface module

Create a project named IOT cloud web API. The 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>net.work100.training.stage2</groupId>
        <artifactId>iot-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../iot-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>iot-cloud-web-api</artifactId>
    <packaging>war</packaging>

    <name>iot-cloud-web-api</name>
    <description></description>
</project>

Then add & lt; Module & gt; IOT cloud web API & lt; / module & gt; configuration to pom.xml of IOT cloud project

9. Cleaning, compiling and packaging

So far, a complete modular project has been created. The Root 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.work100.training.stage2</groupId>
    <artifactId>iot-cloud</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>iot-cloud-dependencies</module>
        <module>iot-cloud-commons</module>
        <module>iot-cloud-domain</module>
        <module>iot-cloud-web-admin</module>
        <module>iot-cloud-web-console</module>
        <module>iot-cloud-web-api</module>
    </modules>
</project>

We can use the mvn clean command provided by Maven in the Root project to test the following effects. The console output is as follows:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] iot-cloud                                                          [pom]
[INFO] iot-cloud-dependencies                                             [pom]
[INFO] iot-cloud-commons                                                  [jar]
[INFO] iot-cloud-domain                                                   [jar]
[INFO] iot-cloud-web-admin                                                [war]
[INFO] iot-cloud-web-console                                              [war]
[INFO] iot-cloud-web-api                                                  [war]
[INFO] 
[INFO] ---------------< net.work100.training.stage2:iot-cloud >----------------
[INFO] Building iot-cloud 1.0.0-SNAPSHOT                                  [1/7]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud ---
[INFO] 
[INFO] ---------< net.work100.training.stage2:iot-cloud-dependencies >---------
[INFO] Building iot-cloud-dependencies 1.0.0-SNAPSHOT                     [2/7]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-dependencies ---
[INFO] 
[INFO] -----------< net.work100.training.stage2:iot-cloud-commons >------------
[INFO] Building iot-cloud-commons 1.0.0-SNAPSHOT                          [3/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-commons ---
[INFO] 
[INFO] ------------< net.work100.training.stage2:iot-cloud-domain >------------
[INFO] Building iot-cloud-domain 1.0.0-SNAPSHOT                           [4/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-domain ---
[INFO] 
[INFO] ----------< net.work100.training.stage2:iot-cloud-web-admin >-----------
[INFO] Building iot-cloud-web-admin 1.0.0-SNAPSHOT                        [5/7]
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-web-admin ---
[INFO] 
[INFO] ---------< net.work100.training.stage2:iot-cloud-web-console >----------
[INFO] Building iot-cloud-web-console 1.0.0-SNAPSHOT                      [6/7]
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-web-console ---
[INFO] 
[INFO] -----------< net.work100.training.stage2:iot-cloud-web-api >------------
[INFO] Building iot-cloud-web-api 1.0.0-SNAPSHOT                          [7/7]
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iot-cloud-web-api ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for iot-cloud 1.0.0-SNAPSHOT:
[INFO] 
[INFO] iot-cloud .......................................... SUCCESS [  0.160 s]
[INFO] iot-cloud-dependencies ............................. SUCCESS [  0.002 s]
[INFO] iot-cloud-commons .................................. SUCCESS [  0.002 s]
[INFO] iot-cloud-domain ................................... SUCCESS [  0.002 s]
[INFO] iot-cloud-web-admin ................................ SUCCESS [  0.002 s]
[INFO] iot-cloud-web-console .............................. SUCCESS [  0.003 s]
[INFO] iot-cloud-web-api .................................. SUCCESS [  0.002 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.320 s
[INFO] Finished at: 2020-02-22T15:49:59+08:00
[INFO] ------------------------------------------------------------------------

10. Perfect function

Watch the video tutorial

11. Example source code

The instance source code has been hosted to the following address:

Last article: Project refactoring (iot-admin3)

Next article: Form label Library

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.


Tags: Java Maven Apache xml snapshot

Posted on Sat, 21 Mar 2020 10:15:07 -0400 by djcubez