1. 程式人生 > 程式設計 >分模組構建Maven工程的方法步驟

分模組構建Maven工程的方法步驟

分模組構建Maven工程Demo

1.分模組構建Maven分析

在企業專案開發過程中,由於專案規模龐大,業務複雜,參與的人員比較多,一般會通過合理的模組拆分將一個大型的專案拆分為N多個小模組,分別進行開發。而且拆分出的模組可以非常容易的被其他模組複用。

常見的拆分方式有兩種:

  • 第一種:按照業務模組進行拆分,每個模組拆分成一個maven工程,例如將一個專案分為使用者模組、訂單模組、購物車模組等,每個模組對應就是一個maven工程
  • 第二種:按照進行拆分,譬如持久層、業務層、表現層等,每個層對應就是一個maven工程
  • 不管是上面哪種拆分方式,通常都會提供一個父工程,將一些公共的程式碼和配置提取到父工程中進行統一管理和配置。

在這裡插入圖片描述

1.1 Maven工程的繼承

在Java語言中,類之間是可以繼承的,通過繼承,子類就可以引用父類中非private的屬性和方法。

同樣,在maven工程之間也可以繼承,子工程繼承父工程後,就可以使用在父工程中引入的依賴。 繼承的目的是為了消除重複程式碼

被繼承的Maven專案中的pom.xml檔案中的定義是:

<groupId>com.hj</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--父工程的打包方式必須為pom-->
  <packaging>pom</packaging>

被繼承的maven工程通常稱為父工程,父工程的打包方式必須為pom,
所以我們區分某個maven工程是否為父工程就看這個工程的打包方式是否為pom。 繼承的Maven專案中的pom.xml檔案中的定義是否為pom。

 <parent>
    <artifactId>parent</artifactId>
    <groupId>com.hj</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>

繼承其他maven父工程的工程通常稱為子工程, 在pom.xml檔案中通過parent標籤進行父工程的繼承。

1.2 maven工程的聚合

在maven工程的pom.xml檔案中可以使用<modules>標籤將其他maven工程聚合到一起,聚合的目的是為了進行統一操作。例如拆分後的maven工程有多個,如果要進行打包,就需要針對每個工程分別執行打包命令, 操作起來非常繁瑣。這時就可以使用<modules>標籤將這些工程統一聚合到maven工程中,需要打包的時候,只需要在此工程中執行一次打包命令,其下被聚合的工程就都會被打包了。

在這裡插入圖片描述

在<modules>標籤中新增被聚合的Maven工程

1.3分模組構建maven工程具體實現

此案例分模組構建 整合一下SSM測試

在這裡插入圖片描述

1.3.1首先建立父工程

![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020101723124332.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDkxMzkyNg==,size_16,color_FFFFFF,t_70#pic_center

建立完畢會自動開啟到pom.xml檔案

首先宣告<packaging>pom</packaging>
父工程的打包方式必須為pom。

在這裡插入圖片描述

隨後加入SSM框架所需座標的jar包版本鎖定:
父工程中的pom檔案中呢只用於jar包版本的鎖定,
子工程用什麼直接寫座標不用寫version

<!--指定版本-->
 <properties>
    <spring.version>5.0.5.RELEASE</spring.version>
    <springmvc.version>5.0.5.RELEASE</springmvc.version>
    <mybatis.version>3.4.5</mybatis.version>
  </properties>
 <!--鎖定jar版本-->
  <dependencyManagement>
    <dependencies>
      <!-- Mybatis -->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>
      <!-- springMVC -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springmvc.version}</version>
      </dependency>
      <!-- spring -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

1.3.2. 構建子工程 maven_pojo

父工程上右鍵new -->Module- ->Maven

在這裡插入圖片描述

構建完畢後建立實體類物件

在這裡插入圖片描述

構建完畢 子工程pom檔案中顯示:

<!--表示當前maven工程繼承了 maven_parent父工程-->
<parent>
  <artifactId>maven_parent</artifactId>
  <groupId>com.hj</groupId>
  <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.hj</groupId>
<artifactId>maven_pojo</artifactId>

此時父工程maven_parent的pom檔案中就會自動的加入:
用於用於聚合其他其他maven工程的<modues>標籤

在這裡插入圖片描述

子工程已經聚合在父工程中。

1.3.3. 構建子工程 maven_dao

構建dao層

在這裡插入圖片描述

構建完畢後建立相關類,新增配置檔案;

在這裡插入圖片描述

在maven_dao模組的pom檔案中 新增依賴

<dependencies>
 <!--在dao層的pom檔案中加入pojo的座標依賴-->
  <dependency>
      <groupId>com.hj</groupId>
      <artifactId>maven_pojo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
  <!-- Mybatis和mybatis與spring的整合 -->
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
  </dependency>
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
  </dependency>
  <!-- MySql驅動 -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.32</version>
  </dependency>
  <!-- druid資料庫連線池 -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.9</version>
  </dependency>
  <!-- spring相關 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
  </dependency>
  <!-- junit測試 -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
  </dependency>
  </dependencies>

1.3.4. 構建子工程 maven_service

構建service層

在這裡插入圖片描述

構建完畢後建立相關類,新增配置檔案

在這裡插入圖片描述

在maven_service模組的pom檔案中 新增依賴

<!--這裡用到了maven的依賴傳遞的特點,
 service直接依賴dao的座標就無須再定義座標-->
  <dependencies>
    <dependency>
      <groupId>com.hj</groupId>
      <artifactId>maven_dao</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--新增springMvc用到的座標-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    </dependency>
  </dependencies>

1.3.5. 構建子工程 maven_web

最後構建web模組

在這裡插入圖片描述

構建完畢後建立相關類,新增配置檔案

在這裡插入圖片描述

在maven_web的pom檔案中加入依賴

<!--這裡用到了maven的依賴傳遞的特點 
web直接依賴service的座標就無須再定義座標-->
  <dependency>
   <groupId>com.hj</groupId>
   <artifactId>maven_service</artifactId>
   <version>1.0-SNAPSHOT</version>
  </dependency>
 </dependencies>

在這裡插入圖片描述

1.3.6 專案整體結構

專案整體結構如右圖所示:
1:maven_parent為父工程,其餘工程為子工程,都繼承了
父工程maven_parent

2:maven_parent工程將其子工程都進行了聚合
3:子工程之間存在依賴關係,比如maven_dao依賴
maven_pojo、maven_service依賴maven_dao、
maven_web依賴maven_service

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

1.3.7 測試執行

把maven_web模組新增到tomcat中

在這裡插入圖片描述

分模組構建Maven工程,測試執行成功。

在這裡插入圖片描述

到此這篇關於分模組構建Maven工程的方法步驟的文章就介紹到這了,更多相關分模組構建Maven 內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!