Maven項(xiàng)目繼承實(shí)現(xiàn)過(guò)程圖解
多個(gè)maven項(xiàng)目之間難免有重復(fù)的pom配置,重復(fù)的配置沒(méi)必要重復(fù)寫,maven提供了父子繼承的關(guān)系,重復(fù)的依賴直接放在父項(xiàng)目的pom中。
所以不希望每個(gè)開(kāi)發(fā)者隨意定義maven版本依賴,可以在父項(xiàng)目中進(jìn)行說(shuō)明,然后子項(xiàng)目沿用即可。
idea創(chuàng)建父項(xiàng)目(這是一個(gè)父項(xiàng)目,也是一個(gè)空項(xiàng)目,只需要pom.xml,編寫相關(guān)的依賴, 父項(xiàng)目必須用pom打包的方式):
編輯父項(xiàng)目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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.linewell</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> <!--父項(xiàng)目必須是pom--> <packaging>pom</packaging> <!--定義參數(shù)--> <properties> <common.version>2.6</common.version> <spring.version>4.3.6.RELEASE</spring.version> </properties> <!--這邊的依賴子項(xiàng)目會(huì)繼承--> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${common.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>spring-context-support</groupId> <artifactId>org.springframework</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
這邊需要說(shuō)明下,dependencyManagement,這邊的依賴不會(huì)被繼承,如果子項(xiàng)目導(dǎo)入了這個(gè)依賴,可以不用寫版本號(hào),會(huì)以父項(xiàng)目的為主,因?yàn)橛械淖禹?xiàng)目不一定會(huì)用父項(xiàng)目中的所有依賴。個(gè)別子項(xiàng)目依賴到的包可以放在這里,然后不需要寫版本號(hào),會(huì)自動(dòng)引用父項(xiàng)目。
創(chuàng)建一個(gè)子項(xiàng)目,編輯子項(xiàng)目的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.linewell</groupId> <artifactId>maven-children</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>com.linewell</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../mavenparent/pom.xml</relativePath> </parent> </project>
可以看到commons-io進(jìn)來(lái)了,spring-context-support沒(méi)進(jìn)來(lái)。
我現(xiàn)在不添加spring-context-support的版本,然后看下結(jié)果,是會(huì)以父項(xiàng)目的版本為主??梢钥吹饺缦乱氲囊彩歉疙?xiàng)目中的4.3.6
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.linewell</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../mavenparent/pom.xml</relativePath> </parent> <groupId>com.linewell</groupId> <artifactId>maven-children</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> </dependencies> </project>
那么問(wèn)題來(lái)了,如果子項(xiàng)目指定了版本會(huì)怎么樣?
編輯子項(xiàng)目pom.xml, 如下可以發(fā)現(xiàn),如果子項(xiàng)目有明確指定依賴以及具體版本,與父項(xiàng)目發(fā)生沖突會(huì)以子項(xiàng)目的依賴為準(zhǔn)。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.linewell</groupId> <artifactId>maven-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../mavenparent/pom.xml</relativePath> </parent> <groupId>com.linewell</groupId> <artifactId>maven-children</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.5.RELEASE</version> </dependency> </dependencies> </project>
ps:如果父項(xiàng)目中執(zhí)行了mvn install安裝到了本地倉(cāng)庫(kù),然后子項(xiàng)目中引入父GAV的時(shí)候可以不用寫路徑relativePath屬性。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流
這篇文章主要介紹了spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Mybatis-plus通過(guò)添加攔截器實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)權(quán)限
系統(tǒng)需要根據(jù)用戶所屬的公司,來(lái)做一下數(shù)據(jù)權(quán)限控制,具體一點(diǎn),就是通過(guò)表中的company_id進(jìn)行權(quán)限控制,項(xiàng)目使用的是mybatis-plus,所以通過(guò)添加攔截器的方式,修改查詢sql,實(shí)現(xiàn)數(shù)據(jù)權(quán)限,本文就通過(guò)代碼給大家詳細(xì)的講解一下,需要的朋友可以參考下2023-08-08SpringBoot集成Jpa對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢和過(guò)濾操作
這篇文章主要介紹了SpringBoot集成Jpa對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢和過(guò)濾操作,主要使用Jpa連接數(shù)據(jù)庫(kù)對(duì)數(shù)據(jù)進(jìn)行排序、分頁(yè)、條件查詢和過(guò)濾操作,需要的朋友可以參考下2023-05-05java后端訪問(wèn)https證書的問(wèn)題及解決
這篇文章主要介紹了java后端訪問(wèn)https證書的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10SpringBoot整合PageHelper實(shí)現(xiàn)分頁(yè)查詢功能詳解
PageHelper是mybatis框架的一個(gè)插件,用于支持在mybatis執(zhí)行分頁(yè)操作。本文將通過(guò)SpringBoot整合PageHelper實(shí)現(xiàn)分頁(yè)查詢功能,需要的可以參考一下2022-03-03利用Spring Session和redis對(duì)Session進(jìn)行共享詳解
這篇文章主要給大家介紹了關(guān)于利用Spring、Session和redis對(duì)Session進(jìn)行共享的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09SpringBoot--- SpringSecurity進(jìn)行注銷權(quán)限控制的配置方法
這篇文章主要介紹了SpringBoot--- SpringSecurity進(jìn)行注銷,權(quán)限控制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01