【maven】多 module項目搭建

1. 前言

本着 “ 乾貨的定義,往往都是基於某種特定場景 ” 的原則,開篇之前,先來點前戲:

  • 只講 maven 工程實踐,不談 maven 原理
  • 主要內容
    • 多 module 項目搭建
    • bom 模塊搭建
  • 環境介紹
    • IDE: Intellij IDEA
    • 語言: Java
    • maven 已安裝

2. 單一 module 項目

2.1. IDEA 創建項目

話不多說,上圖:
在這裏插入圖片描述
創建 maven 或者 Spring Initializer 均可。我這裏採用後者進行創建。

2.2. maven 命令創建

進入自己的工作目錄,輸入命令:

mvn archetype:generate \
 -DgroupId=club.chenlinghong.demo \
 -DartifactId=maven-demo \
 -Dversion=0.0.1-SNAPSHOT \
 -Dpackage=club.chenlinghong.demo.maven

參數說明:

  • groupId: 組織ID,建議用 “域名反寫” ,如: club.chenlinghong.demo [域名:chenlinghong.club, 項目域名:demo]
  • artifactId: 建議採用項目名稱
  • version: 版本號
  • package: 包名,包路徑

2.3. maven 項目結構

創建項目後,大概是:
在這裏插入圖片描述
圖片中,還包含了一些關於 IDE 特有的信息,我一般都會直接刪除。

各文件介紹:

  • src 【源碼文件夾】
    • main【主文件夾】
      • java 【Java 源碼】
      • resources 【配置文件夾】
    • test【測試文件夾】
      • Java 【Java 測試代碼】
      • resources 【測試配置】
  • .gitignore【項目採用了 git 管理,用於忽略一些本地文件】
  • pom.xml 【maven 配置文件】

3. 多 module 項目

3.1. 創建頂層 module

可以延用前文 “單一 module 項目”,咱們把 src 刪除。

在 頂層 module 中,咱們一般用於聚合多 module,較少放 src 代碼。 當然,也是可以有 src 源碼的哈

刪除 src 後, 就只有一個 pom.xml

然後,修改 pom.xmlpackagingpom, 默認是 jar.

完整 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>club.chenlinghong.demo</groupId>
    <artifactId>maven-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
</project>

3.2. 添加子 module

這個步驟比較簡單,直接基於 頂層 module 創建即可,類似於創建單一 module 項目.

此處演示,創建三個 module:

  • maven-demo
    • maven-demo-web
    • maven-demo-service
    • maven-demo-dao

經典的三層架構:

  • maven-demo: 主項目
  • maven-demo-web: web層
  • maven-demo-service:業務邏輯層
  • maven-demo-dao: 持久化層

聚合項目配置:

在父 module: maven-demo 中配置:

<modules>
    <module>maven-demo-web</module>
    <module>maven-demo-service</module>
    <module>maven-demo-dao</module>
</modules>

在各個子 module 中配置:

<parent>
    <groupId>club.chenlinghong.demo</groupId>
    <artifactId>maven-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

此時,maven 多 module 項目簡版就算是搭建完成。

  • 頂層 pom 聚合了三個無依賴關係的 maven 項目

3.3. module 版本管理

接着上邊的項目,如果我想在 maven-demo-web module 中引用 maven-demo-service module,可以採用:

<dependency>
    <groupId>club.chenlinghong.demo</groupId>
    <artifactId>maven-demo-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

咱們在同一個項目中,其他 module 修改了版本號,還需要在依賴 module 中修改,實在是不符合咱們程序員改變世界的初衷呀(萬事從簡嘛)

來,在頂層 pom 中配置下 dependenciesManagement

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>club.chenlinghong.demo</groupId>
            <artifactId>maven-demo-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

鑑於篇幅原因,配置一個來表示下。那咱們就可以在 maven-demo-web 模塊中,簡單引入:

<dependency>
    <groupId>club.chenlinghong.demo</groupId>
    <artifactId>maven-demo-service</artifactId>
</dependency>

至此,咱們就把多 module 之間的依賴關係給配置了,並且進行了依賴管理。

怎麼測試呢?直接在最底層的 module 中創建一個 Class ,然後在依賴的 module 中嘗試 new 一下,就 ok。

4. bom 項目

4.1. bom 搭建

想必,接觸過 Spring, SpringBoot 項目的同學,都看到過,在創建 Spring 項目後,就會有一個是 spring 提供的 pom 或者叫做 maven 座標的 parent.。具體爲:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

看到沒?其實很熟悉是吧?

上文中在創建多 module 的時候,也有在子 module 中寫過這個 parent 的標籤。

其實,這兩個的實現原理,是一樣的。咱們也來搞一個試試:咱們這裏偷個懶,就不單獨創建項目了,直接在 maven-demo 項目下創建 maven-demo-bom 即可:

1、此項目,不需要 src,直接幹掉。

2、配置 pom.xml,設置 packagingpom, 這步,很關鍵、很關鍵,能否被其他項目採用 parent 方式引入,全靠這個配置。

此時,咱們的 bom 已經制作完成,只不過,啥都沒有。 咱們來加一些 dependency, dependenciesManagement

示例 bom :

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>club.chenlinghong.demo</groupId>
        <artifactId>maven-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>maven-demo-bom</artifactId>

    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.18</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

4.2. 打包

現在 bom 已經制作好了,但是現在只能在 當前項目中 採用 parent 方式引入。

若期望其他項目,其他人也能夠引入,那就需要把 bom 發佈到 maven 倉庫中(本地倉庫、遠程倉庫均可,本地倉庫則本地訪問,遠程倉庫則其他人都可以訪問)。

直接採用 mvn install 即可發佈本地倉庫,mvn deploy 發佈遠程倉庫。如果想發佈到 maven 官方中央倉庫,那就需要去 maven 官網申請賬號。

ok,完美收官!!!

5. 相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章