這篇是 maven 項目管理的第二篇, 講解使用 maven 進行多個項目管理, 之前有一篇是 maven 的基礎知識.
SpringBoot系列: Eclipse+Maven環境准備
一個完整的解決方案通常都會包含多個項目, 這些項目往往會有一些公用的依賴, 比如都依賴 SpringBoot, 各個項目之間也有依賴關系. 顯然如果在每個項目都設置這些信息, 並不是很好, 一個明顯的缺點是, 當要統一升級某個基礎 jar 包, 所有項目 pom.xml 都需要更新.
對此 Maven 有很好的解決思路, 即創建 aggregate project(或 multi-module project). maven multi-module 包含一個 parent 項目和多個子項目, 所以從邏輯上講它們是有層次關系的. 但 Eclipse 中每個項目都是直接掛在 workspace 下的, 也就是說在文件系統上是沒有層級關系的, 所以我們可以在 parent 項目名稱上做點文章, 體現出它與眾不同, 比如起名為 myproject-parent .
下面是個 Eclipse 項目關系圖:
Workspace
├─ myproject-parent (通常僅僅包含一個 pom.xml 文件)
├─ myproject-webui
├─ myproject-api
└─ myproject-jobs
======================================
Parent 項目 pom.xml 一般包含的節點
======================================
0. packaging 節點, 對於 parent 項目, packaging 屬性值為 pom
1. parent 節點: 對於 SpringBoot 項目, parent artifactId 應該為 spring-boot-starter-parent, 指定版本.
2. dependencyManagement/dependencies/dependency 節點, 經常使用 dependencyManagement 作為 jar 包版本仲裁, 在 dependencyManagement 下聲明的依賴, 並不會被實際引入, 只有 dependencies/dependency 節點下的 jar 包才會被引入.
3. modules 節點, 設定本 parent 項目包含哪些 sub module 項目, 推薦在使用 profiles 節點下定義子模塊, 而不是直接在 modules 節點下設定子模塊.
4. properties 節點, 設定將所有項目共有的屬性設置在這里.
5. build/plugins/plugin 節點: 將公用的插件放到這里, 比如 spring-boot-maven-plugin
6. dependencies/dependency 節點, 更推薦使用 dependencyManagement/dependencies/dependency.
7. profiles 節點, 這個下面在詳細講解.
----------------------------------
dependencyManagement 仲裁 jar 包版本
----------------------------------
Parent 項目中, 統一了 spring-core 版本為 4.3.5.RELEASE.
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.5.RELEASE</version> </dependency> </dependencies> </dependencyManagement>
在子項目中, 需要引入 spring-core 時就不必再指定版本號.
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> </dependencies>
如果某個子項目中, 需要引入一個其他版本的 spring-core 時, 在 dependencies 節點下直接指定想要的版本即可.
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.1.RELEASE</version> </dependency> </dependencies>
----------------------------------
使用 profiles
----------------------------------
通常一個較大規模的 Parent 項目會包含好幾個 sub module 項目, 各個子項目之前可能有依賴, 可能沒有依賴, 為了縮短編譯時間, 最好設定不同的 profile , 不同 profile 中包含的不同的 sub-module 組合.
<profiles> <profile> <id>backend</id> <modules> <module>../myproject-api</module> <module>../myproject-webui</module> </modules> </profile> <profile> <id>all</id> <activation> <activeByDefault>true</activeByDefault> </activation> <modules> <module>../myproject-api</module> <module>../myproject-webui</module> <module>../myproject-jobs</module> </modules> </profile> </profiles>
上面定義了兩個 profile, 分別是 backend 和 all, maven 支持按 profile 進行編譯.
僅編譯 backend 相關的子模塊:
mvn clean package -Pbackend
編譯缺省的 profile:
mvn clean package
======================================
子項目中指定 parent pom 的位置
======================================
<parent> <groupId>com.harry</groupId> <artifactId>myproject-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../myproject-parent/pom.xml</relativePath> </parent>
======================================
參考
======================================
https://www.smartics.eu/confluence/display/BLOG/2013/07/22/Using+Aggregate+and+Parent+POMs
https://www.baeldung.com/maven