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
<?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
<?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
<?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 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...
- 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 多模块项目构建开发整的差不多,特地重新创建配置,记录 ...
随机推荐
- Notes of Daily Scrum Meeting(12.5)
最近各种大作业催的比较紧,而且也因为Beta阶段刚刚开始,大家的进展很缓慢,周四因为课业的原因大部分队员 没有做我们的项目,所以就在今天一起总结,我们的问题反馈给学姐之后,学姐也还在看,目前还没有回复 ...
- hibernate 创建工厂类
package cn.hibernate; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; / ...
- ios UnitTest 学习笔记
一.运行第一个单元测试: 1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestC ...
- 从零开始学Kotlin-数据类型(2)
从零开始学Kotlin基础篇系列文章 基本数据类型 Kotlin 的基本数值类型包括 Byte.Short.Int.Long.Float.Double 等: 数据-------位宽度 Double-- ...
- Alpha 冲刺四
团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 实现后端聊天接收,搜 ...
- NetScaler 10.1的配置以及结合StoreFront的部署
工作需要,所以英文+中文,绝壁不是装逼...(关于这点勿喷) This post will cover only the basics for getting NetScaler up and run ...
- C语言变长数组data[0]
1.前言 在刷题时遇到一个结构中包含char data[0],第一次见到时感觉很奇怪,数组的长度怎么可以为零呢?于是上网搜索一下这样的用法的目的,发现在linux内核中,结构体中经常用到data[0] ...
- Python面向对象高级编程:@property--把方法变为属性
为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数: >>> ...
- PHPSQL注入
什么是SQL注入? 就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 例如一个简单的登录表单(这里把密码写成明文方便说明): 当在表 ...
- 【转】在SpringMVC Controller中注入Request成员域
原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...