在搞懂Maven多模块应用开发前,建议阅读 该博客,夯实一下基础,其实也就是规范的问题,每个Maven项目标签的使用方式及含义。搞清楚后,对后面的项目创建就轻松很多。笔者已经将整个项目托管到Git仓库,点击此处访问 项目,下载导入IDE即可享用,逻辑是简单易理解的。
一、在初始化器上创建SpringBoot全局工程
1.1 阿里初始化器
1.2 填写配置清单
1.3 选择版本
1.4 填写工程名
此时看一下整个项目的目录:
1.5 裁剪项目文件
删除选中的文件
二、创建两个Maven子模块
2.1 创建demo-web模块
2.2 导入Spring Web依赖
在子模块的pom文件里添加如下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.3 整个pom文件内容
<?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"> <parent> <artifactId>demo</artifactId> <groupId>com.example</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-web</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Maven会自动将子模块添加到主pom文件中。
2.4 创建demo-common模块
同demo-web子模块的创建一样,该模块用于提供通用工具类。
2.5 创建demo-common模块
三、子模块间的联系
3.1 web模块依赖common模块
在web模块的pom文件中引入common模块依赖:
<dependency> <groupId>com.example</groupId> <artifactId>demo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
3.2 Maven打包结果
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] demo [pom] [INFO] demo-common [jar] [INFO] demo-web [jar] [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 0.0.1-SNAPSHOT [1/3] [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] --- spring-boot-maven-plugin:2.3.7.RELEASE:repackage (repackage) @ demo --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo --- [INFO] Installing F:\respository\MultiModulesApp\pom.xml to E:\respository\com\example\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.pom [INFO] [INFO] ----------------------< com.example:demo-common >----------------------- [INFO] Building demo-common 0.0.1-SNAPSHOT [2/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo-common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-common --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to F:\respository\MultiModulesApp\demo-common\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo-common --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\respository\MultiModulesApp\demo-common\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-common --- [INFO] Changes detected - recompiling the module! [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo-common --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo-common --- [INFO] Building jar: F:\respository\MultiModulesApp\demo-common\target\demo-common-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.3.7.RELEASE:repackage (repackage) @ demo-common --- [INFO] Attaching repackaged archive F:\respository\MultiModulesApp\demo-common\target\demo-common-0.0.1-SNAPSHOT-EXEC.jar with classifier EXEC [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo-common --- [INFO] Installing F:\respository\MultiModulesApp\demo-common\target\demo-common-0.0.1-SNAPSHOT.jar to E:\respository\com\example\demo-common\0.0.1-SNAPSHOT\demo-common-0.0.1-SNAPSHOT.jar [INFO] Installing F:\respository\MultiModulesApp\demo-common\pom.xml to E:\respository\com\example\demo-common\0.0.1-SNAPSHOT\demo-common-0.0.1-SNAPSHOT.pom [INFO] Installing F:\respository\MultiModulesApp\demo-common\target\demo-common-0.0.1-SNAPSHOT-EXEC.jar to E:\respository\com\example\demo-common\0.0.1-SNAPSHOT\demo-common-0.0.1-SNAPSHOT-EXEC.jar [INFO] [INFO] ------------------------< com.example:demo-web >------------------------ [INFO] Building demo-web 0.0.1-SNAPSHOT [3/3] [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo-web --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to F:\respository\MultiModulesApp\demo-web\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo-web --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory F:\respository\MultiModulesApp\demo-web\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo-web --- [INFO] Changes detected - recompiling the module! [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo-web --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo-web --- [INFO] Building jar: F:\respository\MultiModulesApp\demo-web\target\demo-web-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.3.7.RELEASE:repackage (repackage) @ demo-web --- [INFO] Replacing main artifact with repackaged archive [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo-web --- [INFO] Installing F:\respository\MultiModulesApp\demo-web\target\demo-web-0.0.1-SNAPSHOT.jar to E:\respository\com\example\demo-web\0.0.1-SNAPSHOT\demo-web-0.0.1-SNAPSHOT.jar [INFO] Installing F:\respository\MultiModulesApp\demo-web\pom.xml to E:\respository\com\example\demo-web\0.0.1-SNAPSHOT\demo-web-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for demo 0.0.1-SNAPSHOT: [INFO] [INFO] demo ............................................... SUCCESS [ 1.084 s] [INFO] demo-common ........................................ SUCCESS [ 1.587 s] [INFO] demo-web ........................................... SUCCESS [ 1.289 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.299 s [INFO] Finished at: 2021-10-20T14:33:44+08:00 [INFO] ------------------------------------------------------------------------
3.2 还可以叠罗汉一样构建多模块项目
一般情况下,深度为3的应用已经相当复杂了。如Thingsboard源码就属于三层Maven多模块结构。