1. 程式人生 > >SpringBoot系列: Maven多專案管理

SpringBoot系列: Maven多專案管理

這篇是 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