Spring Boot 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一)
最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块。代码中的多模块是用maven管理的,每个模块都使用spring boot框架。之前有零零散散学过一些maven多模块配置的知识,但没自己从头到尾创建和配置过,也快忘得差不多了。这次正好对照着这个项目,动手实践一下,下面我们就开始吧。
maven多模块项目通常由一个父模块和若干个子模块构成,每个模块都对应着一个pom.xml。它们之间通过继承和聚合(也称作多模块)相互关联。多模块适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理。
1 多模块项目创建
因为本系列的下一篇是《Spring Boot集成Dubbo》,所以本章就以创建多模块的dubbo项目作为示例。示例中的开发环境是Win 7,编辑器是Intellij IDEA,Java版本是1.8。
1.1 父模块创建
首先我们在IDEA中创建一个spring boot工程作为父项目。
一、在界面左上角选择File->New->Project后,选择Spring Initializr,默认使用的Java版本是1.8。

二、点击Next,进入下一步,可以设置项目的一些基本信息。
这里我们先来温习下groupId、artifactId、package这三个参数的一般填写规范。
groupId和artifactId统称为“坐标”,是为了保证项目唯一性而提出的。groupId是项目组织唯一的标识符,实际对应JAVA的包的结构,ArtifactID是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。groupId一般分为多个段,一般第一段为域,第二段为公司名称。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org,公司名称是apache,artifactId是tomcat。包结构package最好是以groupId.artifactId打头的。
因为后续打算将“代码学习和实践”写成一个系列的文章,文中演示的工程都作为该工程的子模块,所以这里项目名Name就填写CodeLearnAndPractice。
这里是个人练习的项目,不涉及公司名,但groupId、artifactId、package参数的填写,还是尽量按照上面的规范来填写,这里package就直接用groupId.artifactId。如下所示:

三、点击Next,进入下一个选择dependency的界面,作用是在pom中自动添加一些依赖,在项目开始时就下载。这里我们暂时不勾选任何依赖。
四、点击Next,进入下一个界面,填写工程名,并选择工程所在目录。填写完成后,点击Finish,即可创建一个spring boot项目。

1.2 创建子模块
在上面创建好的CodeLearnAndPractice工程名上,点击右键,选择New–>Module,进入New Module页面。
该模块为dubbo服务的提供方,Name为springboot-dubbo-server,后面其他的参数都可参照父模块的参数设置。

下面创建另一个Module,dubbo服务的调用方,Name为springboot-dubbo-client,其他参数设置参照上步。

以上3个模块创建完成之后,整个项目的目录结构如下图所示。
我们把下图选中的无用的文件及文件夹删掉,包括三个模块的mvnw、mvnw.cmd文件及.mvn文件夹,还有父模块的src目录,因为此处的父模块只做依赖管理,不需要编写代码。

到这里,一个父模块和两个子模块都创建完成啦~~
2 多模块项目配置
2.1 父模块pom配置
父pom是为了抽取统一的配置信息和依赖版本控制,方便子pom直接引用,简化子pom的配置。
下面介绍下父pom的配置中需要注意的一些地方。我贴出的pom看起来会有点冗余,因为其中一些不需要的地方,我没有直接删掉而是注释掉,并加了说明,是为了后续查看的时候还能清楚删掉的原因。
1、父模块的打包类型
多模块项目中,父模块打包类型必须是pom,同时以给出所有的子模块,其中每个module,都是另外一个maven项目。
我们的项目中目前一共有两个子模块,springboot-dubbo-server和springboot-dubbo-client。后续新增的子模块也必须加到父pom的modules中。
2、继承设置
继承是maven中很强大的一种功能,继承可以使子pom获得parent中的各项配置,对子pom进行统一的配置和依赖管理。父pom中的大多数元素都能被子pom继承,想深入了解的同学可自行搜索学习~~
maven项目之间的继承关系通过表示。这里使用的开发框架是spring boot,默认继承spring-boot-starter-parent。
3、使用dependencyManagement管理依赖版本号
一般在项目最顶层的父pom中使用该元素,让所有子模块引用一个依赖而不用显式的列出版本号。maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。
4、使用properties控制依赖包的版本号,便于版本维护
在properties标签中,添加各依赖包的版本号,然后在dependency中直接引用该依赖版本号的值即可。
CodeLearnAndPractice/pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<? 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 >com.practice</ groupId > < artifactId >CodeLearnAndPractice</ artifactId > < version >0.0.1-SNAPSHOT</ version > <!--<packaging>jar</packaging>--> < packaging >pom</ packaging > <!--父模块打包类型必须为pom--> < modules > < module >springboot-dubbo-server</ module > < module >springboot-dubbo-client</ module > </ modules > < name >CodeLearnAndPractice</ name > < description >Practice the learned code</ description > <!-- parent指明继承关系,给出被继承的父项目的具体信息--> < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.8.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding > < java.version >1.8</ java.version > <!-- 在properties中统一控制依赖包的版本,更清晰--> < dubbo.version >2.5.3</ dubbo.version > < zkclient.version >0.10</ zkclient.version > </ properties > < dependencyManagement > <!--dependencyManagement用于管理依赖版本号--> < dependencies > <!-- 删除spring-boot-starter和spring-boot-starter-test, 因为parent中继承的祖先中已经有了,并且一般dependencyManagement管理的依赖都要写版本号 --> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-test</artifactId>--> <!--<scope>test</scope>--> <!--</dependency>--> <!--新增后续dubbo项目中所需依赖,dubbo、zk--> < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo</ artifactId > <!--<version>2.5.3</version>--> <!--使用properties中配置的版本号--> < version >${dubbo.version}</ version > < exclusions > < exclusion > < groupId >org.springframework</ groupId > < artifactId >spring</ artifactId > </ exclusion > </ exclusions > </ dependency > < dependency > < groupId >com.101tec</ groupId > < artifactId >zkclient</ artifactId > <!--<version>0.10</version>--> <!--使用properties中配置的版本号--> < version >${zkclient.version}</ version > </ dependency > </ dependencies > </ dependencyManagement > <!--该插件作用是打一个可运行的包,必须要写在需要打包的项目里。这里的父模块不需要打包运行,所以删掉该插件。--> <!--<build>--> <!--<plugins>--> <!--<plugin>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-maven-plugin</artifactId>--> <!--</plugin>--> <!--</plugins>--> <!--</build>--> </ project > |
2.2 子模块pom配置
1、继承设置
子模块的parent要使用顶层的父模块.
2、依赖设置
父模块pom中使用dependencyManagement来管理的依赖,在子模块pom中就不需要再写版本号了,exclusion元素也不需要再写。
springboot-dubbo-server\pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<? 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 >com.practice</ groupId > < artifactId >springboot-dubbo-server</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >springboot-dubbo-server</ name > < description >Demo project for Spring Boot</ description > <!-- 子模块的parent要使用顶层的父模块--> < parent > <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-parent</artifactId>--> <!--<version>1.5.8.RELEASE</version>--> <!--<relativePath/>--> < groupId >com.practice</ groupId > < artifactId >CodeLearnAndPractice</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > <!-- properties可删掉,会继承父模块的--> <!--<properties>--> <!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>--> <!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>--> <!--<java.version>1.8</java.version>--> <!--</properties>--> < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > <!--新增后续dubbo项目中所需依赖,dubbo、zk。 父模块pom中使用dependencyManagement来管理依赖版本号,子模块pom中不需要再写版本号,exclusion也不需要--> < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo</ artifactId > <!--<version>2.5.3</version>--> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> </ dependency > < dependency > < groupId >com.101tec</ groupId > < artifactId >zkclient</ artifactId > <!--<version>0.10</version>--> </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
springvoot-dubbo-client/pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<? 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 >com.practice</ groupId > < artifactId >springboot-dubbo-client</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >springboot-dubbo-client</ name > < description >Demo project for Spring Boot</ description > <!-- 子模块的parent要使用顶层的父模块--> < parent > <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-parent</artifactId>--> <!--<version>1.5.8.RELEASE</version>--> <!--<relativePath/>--> < groupId >com.practice</ groupId > < artifactId >CodeLearnAndPractice</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > <!-- properties可删掉,会继承父模块的--> <!--<properties>--> <!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>--> <!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>--> <!--<java.version>1.8</java.version>--> <!--</properties>--> < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > <!-- 该模块需要启动web服务,需要该依赖--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!--新增后续dubbo项目中所需依赖,dubbo、zk 父模块pom中使用dependencyManagement来管理依赖版本号,子模块pom中不需要再写版本号 父模块pom中里有exclusion,子模块pom中不要写exclusion--> < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo</ artifactId > <!--<version>2.5.3</version>--> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> </ dependency > < dependency > < groupId >com.101tec</ groupId > < artifactId >zkclient</ artifactId > <!--<version>0.10</version>--> </ dependency > <!--client模块需要依赖server模块--> < dependency > < groupId >com.practice</ groupId > < artifactId >springboot-dubbo-server</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
3、关于exclusions标签
当dependency A自身的依赖B,与其他dependency存在冲突的时候(最常见的就是版本冲突),我们就需要把B排除掉,这时就需要使用exclusions元素。
那么我们怎么知道一个dependency自身包含哪些依赖呢?
1、通过mvn dependency:tree命令查看依赖树
2、使用IDEA或其他IDA查看依赖树
点击IDEA右侧的Maven Projects,在每个模块的Dependencies中即可查看每个dependency内部的依赖及版本号,从来识别哪些依赖需要被排除掉。
以dubbo为例,我们先删除配置,点开Maven Projects,可以看到2.5.3版本的dubbo中使用的spring版本是2.5.6,这是一个很老的版本,有一些方法是没有的,现在在用的spring版本一般都是4.*的,所以我们需要把它排除掉,避免后续报错。
要查看当前项目中使用的spring版本,可以按住左键,然后点击父pom中的值,进入更上一层pom,再重复上步操作,可以看到spring的版本是4.3.12。

按住左键,然后点击父pom中的值,进入更上一层pom:

可以看到spring的版本是4.3.12:

3 测试
这里就先不写代码了,到下一章再写。直接编译一下,如果编译成功,说明pom文件的配置没有什么大问题。
点开右侧Maven Projects,双击父模块Lifecycle中的compile,进行代码编译,或者直接在Terminal中执行命令:mvn compile。
编译通过啦~~

到这里,Spring Boot多模块项目创建与配置就介绍完啦。
这样一个过程实践下来,再去看开发的代码结构,应该会轻松不少吧~~~
参考文档:
https://testerhome.com/topics/11359
Spring Boot 多模块项目创建与配置 (一) (转)的更多相关文章
- Spring Boot 多模块项目创建与配置 (一)
最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...
- Spring Boot 多模块项目创建与配置 (转)
转载:https://www.cnblogs.com/MaxElephant/p/8205234.html 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多 ...
- Spring boot 多模块项目 + Swagger 让你的API可视化
Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Maven 搭建spring boot多模块项目
Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...
- IDEA 创建 Spring Boot 多模块项目(Multi Modules)
本准备写点代码实例放到网站,太多的模板,反而每次新建工程的时候很麻烦.于是准备把这个章节的内容提前先讲讲.正好把这个代码也管理起来.话说这个多模块功能还挺爽. 写过 C# 项目用过 Visual St ...
- spring boot:多模块项目生成jar包(spring boot 2.3.3)
一,多模块项目的优点: 1,为什么要使用多模块项目? 相比传统的单体工程,使用Maven的多模块配置, 有如下优点: 帮助项目划分模块,鼓励重用, 防止POM变得过于庞大, 方便某个模块的构建,而不用 ...
- spring boot多模块项目找不到类
项目结构 mapper依赖pojo, service依赖mapper和pojo portal依赖pojo和service. 全都是maven模块 <groupId>com.haitian& ...
- Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版
Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版 百度很多博客都不详细,弄了半天才把 Spring Boot 多模块项目构建开发整的差不多,特地重新创建配置,记录 ...
随机推荐
- Sql Server 之 Merge
转载:http://blog.csdn.net/zmoneyz/article/details/38404111 现在我们来创建了两个表Person和UserLogin Person表如下: User ...
- Play on Words HDU - 1116(欧拉路判断 + 并查集)
题意: 给出几个单词,求能否用所有的单词成语接龙 解析: 把每个单词的首字母和尾字母分别看作两个点u 和 v,输入每个单词后,u的出度++, v的入度++ 最后判断是否能组成欧拉路径 或 欧拉回路,当 ...
- Centos7安装Zabbix4.0步骤
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 Centos7安装Zabbix4.0步骤 官方搭建zabbix4.0的环境要求: 1. 环境搭建L ...
- 学习Spring Boot:(二十一)使用 EhCache 实现数据缓存
前言 当多次查询数据库影响到系统性能的时候,可以考虑使用缓存,来解决数据访问新能的问题. SpringBoot 已经为我们提供了自动配置多个 CacheManager 的实现,只要去实现使用它就可以了 ...
- emwin 之使用键盘数据发送函数的注意事项
@2018-08-08 小记 键盘实现时,在发送键值时, 函数 GUI_SendKeyMsg(GUI_KEY_BACKSPACE, Pressed) 的参数 Pressed 在按键按下状态的 case ...
- K8s核心概念详解
kubernetes(通常简称为K8S),是一个用于管理在容器中运行的应用的容器编排工具. Kubernetes不仅有你所需要的用来支持复杂容器应用的所有东西,它还是市面上最方便开发和运维的框架. K ...
- Azure KeyVault设置策略和自动化添加secrets键值对
一. 关于Azure Key Vault Azure 密钥保管库可帮助保护云应用程序和服务使用的加密密钥和机密. 借助 Key Vault,可使用密钥来加密密钥和机密(例如身份验证密钥.存储帐户密钥. ...
- Jenkins中使用Azure Powershell连接Service Fabric报错not recognized的原因与解决办法
一.使用背景 在涉及Azure service Fabric的自动化应用场景中,依赖于Service Fabric的Azure Powershell cmdlets,我们可以使用Jenkins能实现c ...
- Codeforces Round #548
没打,简单补档 C.Edgy Trees 容斥,把黑边断掉数联通块,每个联通块贡献$siz^k$ #include<cstdio> #include<cstring> #inc ...
- es6/es7/es8常用新特性总结(超实用)
本文标题有误导性,因为我其实想写node8的新特性,说实话一下子从node v1.x跳跃到node 8.x+ 真有点受宠若惊的感觉.一直觉得node 数组. 对象.序列等的处理没有python方便,因 ...