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