分模塊構(gòu)建Maven工程的方法步驟
分模塊構(gòu)建Maven工程Demo
1.分模塊構(gòu)建Maven分析
在企業(yè)項(xiàng)目開發(fā)過(guò)程中,由于項(xiàng)目規(guī)模龐大,業(yè)務(wù)復(fù)雜,參與的人員比較多,一般會(huì)通過(guò)合理的模塊拆分將一個(gè)大型的項(xiàng)目拆分為N多個(gè)小模塊,分別進(jìn)行開發(fā)。而且拆分出的模塊可以非常容易的被其他模塊復(fù)用。
常見的拆分方式有兩種:
- 第一種:按照業(yè)務(wù)模塊進(jìn)行拆分,每個(gè)模塊拆分成一個(gè)maven工程,例如將一個(gè)項(xiàng)目分為用戶模塊、訂單模塊、購(gòu)物車模塊等,每個(gè)模塊對(duì)應(yīng)就是一個(gè)maven工程
- 第二種:按照層進(jìn)行拆分,譬如持久層、業(yè)務(wù)層、表現(xiàn)層等,每個(gè)層對(duì)應(yīng)就是一個(gè)maven工程
- 不管是上面哪種拆分方式,通常都會(huì)提供一個(gè)父工程,將一些公共的代碼和配置提取到父工程中進(jìn)行統(tǒng)一管理和配置。

1.1 Maven工程的繼承
在Java語(yǔ)言中,類之間是可以繼承的,通過(guò)繼承,子類就可以引用父類中非private的屬性和方法。
同樣,在maven工程之間也可以繼承,子工程繼承父工程后,就可以使用在父工程中引入的依賴。 繼承的目的是為了消除重復(fù)代碼。
被繼承的Maven項(xiàng)目中的pom.xml文件中的定義是:
<groupId>com.hj</groupId> <artifactId>parent</artifactId> <version>1.0-SNAPSHOT</version> <!--父工程的打包方式必須為pom--> <packaging>pom</packaging>
被繼承的maven工程通常稱為父工程,父工程的打包方式必須為pom,
所以我們區(qū)分某個(gè)maven工程是否為父工程就看這個(gè)工程的打包方式是否為pom。 繼承的Maven項(xiàng)目中的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文件中通過(guò)parent標(biāo)簽進(jìn)行父工程的繼承。
1.2 maven工程的聚合
在maven工程的pom.xml文件中可以使用<modules>標(biāo)簽將其他maven工程聚合到一起,聚合的目的是為了進(jìn)行統(tǒng)一操作。例如拆分后的maven工程有多個(gè),如果要進(jìn)行打包,就需要針對(duì)每個(gè)工程分別執(zhí)行打包命令, 操作起來(lái)非常繁瑣。這時(shí)就可以使用<modules>標(biāo)簽將這些工程統(tǒng)一聚合到maven工程中,需要打包的時(shí)候,只需要在此工程中執(zhí)行一次打包命令,其下被聚合的工程就都會(huì)被打包了。

在<modules>標(biāo)簽中添加被聚合的Maven工程
1.3分模塊構(gòu)建maven工程具體實(shí)現(xiàn)
此案例分模塊構(gòu)建 整合一下SSM測(cè)試

1.3.1首先創(chuàng)建父工程

創(chuàng)建完畢會(huì)自動(dòng)打開到pom.xml文件
首先聲明<packaging>pom</packaging>
父工程的打包方式必須為pom。

隨后加入SSM框架所需坐標(biāo)的jar包版本鎖定:
父工程中的pom文件中呢只用于jar包版本的鎖定,
子工程用什么直接寫坐標(biāo)不用寫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. 構(gòu)建子工程 maven_pojo
父工程上右鍵new -->Module- ->Maven

構(gòu)建完畢后創(chuàng)建實(shí)體類對(duì)象

構(gòu)建完畢 子工程pom文件中顯示:
<!--表示當(dāng)前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>
此時(shí)父工程maven_parent的pom文件中就會(huì)自動(dòng)的加入:
用于用于聚合其他其他maven工程的<modues>標(biāo)簽

子工程已經(jīng)聚合在父工程中。
1.3.3. 構(gòu)建子工程 maven_dao
構(gòu)建dao層

構(gòu)建完畢后創(chuàng)建相關(guān)類,添加配置文件;

在maven_dao模塊的pom文件中 添加依賴
<dependencies>
<!--在dao層的pom文件中加入pojo的坐標(biāo)依賴-->
<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驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- druid數(shù)據(jù)庫(kù)連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- spring相關(guān) -->
<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測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
1.3.4. 構(gòu)建子工程 maven_service
構(gòu)建service層

構(gòu)建完畢后創(chuàng)建相關(guān)類,添加配置文件

在maven_service模塊的pom文件中 添加依賴
<!--這里用到了maven的依賴傳遞的特點(diǎn),
service直接依賴dao的坐標(biāo)就無(wú)須再定義坐標(biāo)-->
<dependencies>
<dependency>
<groupId>com.hj</groupId>
<artifactId>maven_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--添加springMvc用到的坐標(biāo)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
1.3.5. 構(gòu)建子工程 maven_web
最后構(gòu)建web模塊

構(gòu)建完畢后創(chuàng)建相關(guān)類,添加配置文件

在maven_web的pom文件中加入依賴
<!--這里用到了maven的依賴傳遞的特點(diǎn) web直接依賴service的坐標(biāo)就無(wú)須再定義坐標(biāo)--> <dependency> <groupId>com.hj</groupId> <artifactId>maven_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>

1.3.6 項(xiàng)目整體結(jié)構(gòu)
項(xiàng)目整體結(jié)構(gòu)如右圖所示:
1:maven_parent為父工程,其余工程為子工程,都繼承了
父工程maven_parent
2:maven_parent工程將其子工程都進(jìn)行了聚合
3:子工程之間存在依賴關(guān)系,比如maven_dao依賴
maven_pojo、maven_service依賴maven_dao、
maven_web依賴maven_service



1.3.7 測(cè)試運(yùn)行
把maven_web模塊添加到tomcat中

分模塊構(gòu)建Maven工程,測(cè)試運(yùn)行成功。

到此這篇關(guān)于分模塊構(gòu)建Maven工程的方法步驟的文章就介紹到這了,更多相關(guān)分模塊構(gòu)建Maven 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java 包掃描實(shí)現(xiàn)和應(yīng)用(Jar篇)
這篇文章主要介紹了詳解Java 包掃描實(shí)現(xiàn)和應(yīng)用(Jar篇),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門
MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過(guò)程和高級(jí)映射的優(yōu)秀持久層框架,這篇文章主要給大家分享MyBatis入門學(xué)習(xí)教程(一)-MyBatis快速入門,需要的朋友可以參考下2015-08-08
idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法
這篇文章主要介紹了idea日志亂碼和tomcat日志亂碼問(wèn)題的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Spring Boot 數(shù)據(jù)校驗(yàn)@Valid+統(tǒng)一異常處理的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot 數(shù)據(jù)校驗(yàn)@Valid+統(tǒng)一異常處理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法
本文主要介紹了詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Struts2開發(fā) 基本配置與類型轉(zhuǎn)換
本篇文章,小編將為大家介紹關(guān)于Struts2開發(fā) 基本配置與類型轉(zhuǎn)換,有需要的朋友可以參考一下2013-04-04
springboot實(shí)現(xiàn)圖片大小壓縮功能
這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)圖片大小壓縮功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

