原文地址:http://www.work100.net/training/monolithic-frameworks-spring-mvc-maven-module.html
更多教程:光束云 - 免费课程
Maven 模块化开发
序号 | 文内章节 | 视频 |
---|---|---|
1 | 概述 | - |
2 | 创建根项目(工程) | - |
3 | 创建统一的依赖管理模块 | - |
4 | 创建通用的工具类模块 | - |
5 | 创建领域模型模块 | - |
6 | 创建管理后台模块 | - |
7 | 创建前端控制台模块 | - |
8 | 创建接口模块 | - |
9 | 清理、编译、打包 | - |
10 | 功能完善 | - |
11 | 实例源码 | - |
请参照如上章节导航
进行阅读
1.概述
在多人协同开发时,特别是规模较大的项目,为了方便日后的代码维护和管理,我们会将每个开发人员的工作细分到具体的功能和模块上。
随着项目的不断扩大,模块也会越来越多,后续会更加难以维护和扩展,为了应对这种情况后期我们还会采用微服务架构的方式进行开发。
本章节以构建一个 IoT云平台
项目为例,我们可以将模块划分为如下形式:
- 统一的依赖管理(
dependencies
) - 通用的工具类(
commons
) - 领域模型(
domain
) - 管理后台(
admin
) - 前端控制台(
console
) - 接口模块(
api
)
整个模块化开发过程主要是在开发思想上稍作了一些转变,只需要按照下面的流程操作即可。
2.创建根项目(工程)
创建一个名为 iot-cloud
的工程,pom.xml
文件如下:
<?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>
该项目称之为 Root
项目,主要作用是管理整个工程的全部模块,当有新模块加入时需要在 modules
元素下配置对应的模块目录
3.创建统一的依赖管理模块
创建一个名为 iot-cloud-dependencies
的项目,pom.xml
文件如下:
<?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>
<!-- 环境配置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 统一的依赖管理 -->
<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 插件, 设定 JDK 版本 -->
<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>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
然后修改 iot-cloud
工程的 pom.xml
文件:
<?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.创建通用的工具类模块
创建一个名为 iot-cloud-commons
的项目,pom.xml
文件如下:
<?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>
然后在 iot-cloud
工程的 pom.xml
中增加 <module>iot-cloud-commons</module>
配置
5.创建领域模型模块
创建一个名为 iot-cloud-domain
的项目,pom.xml
文件如下:
<?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>
然后在 iot-cloud
工程的 pom.xml
中增加 <module>iot-cloud-domain</module>
配置
6.创建管理后台模块
创建一个名为 iot-cloud-web-admin
的项目,pom.xml
文件如下:
<?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>
然后在 iot-cloud
工程的 pom.xml
中增加 <module>iot-cloud-web-admin</module>
配置
7.创建前端控制台模块
创建一个名为 iot-cloud-web-console
的项目,pom.xml
文件如下:
<?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>
然后在 iot-cloud
工程的 pom.xml
中增加 <module>iot-cloud-web-console</module>
配置
8.创建接口模块模块
创建一个名为 iot-cloud-web-api
的项目,pom.xml
文件如下:
<?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>
然后在 iot-cloud
工程的 pom.xml
中增加 <module>iot-cloud-web-api</module>
配置
9.清理、编译、打包
至此一个完整的模块化工程创建完毕,此时的 Root
工程 pom.xml
文件如下:
<?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>
我们可以在 Root
工程中使用 Maven
提供的 mvn clean
命令测试一下效果,控制台输出如下:
[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.功能完善
请观看视频教程
11.实例源码
实例源码已经托管到如下地址:
- https://github.com/work100-net/training-stage2/tree/master/iot-cloud
- https://gitee.com/work100-net/training-stage2/tree/master/iot-cloud
上一篇:项目重构(iot-admin3) - Spring MVC - 常用框架 - Java单体应用 - 免费课程 - 光束云 - work100.net")
下一篇:表单标签库
如果对课程内容感兴趣,可以扫码关注我们的
公众号
或QQ群
,及时关注我们的课程更新