1. Maven工程繼承關(guān)系
-
繼承概念
Maven 繼承是指在 Maven 的項目中,讓一個項目從另一個項目中繼承配置信息的機制。繼承可以讓我們在多個項目中共享同一配置信息,簡化項目的管理和維護工作。
-
繼承作用
在父工程中統(tǒng)一管理項目中的依賴信息。
它的背景是:
-
對一個比較大型的項目進行了模塊拆分。
-
一個 project 下面,創(chuàng)建了很多個 module。
-
每一個 module 都需要配置自己的依賴信息。
它背后的需求是:
-
在每一個 module 中各自維護各自的依賴信息很容易發(fā)生出入,不易統(tǒng)一管理。
-
使用同一個框架內(nèi)的不同 jar 包,它們應(yīng)該是同一個版本,所以整個項目中使用的框架版本需要統(tǒng)一。
-
使用框架時所需要的 jar 包組合(或者說依賴信息組合)需要經(jīng)過長期摸索和反復(fù)調(diào)試,最終確定一個可用組合。這個耗費很大精力總結(jié)出來的方案不應(yīng)該在新的項目中重新摸索。 通過在父工程中為整個項目維護依賴信息的組合既保證了整個項目使用規(guī)范、準確的 jar 包;又能夠?qū)⒁酝慕?jīng)驗沉淀下來,節(jié)約時間和精力。
-
-
繼承語法
-
父工程
-
<groupId>com.atguigu.maven</groupId>
<artifactId>pro03-maven-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 當(dāng)前工程作為父工程,它要去管理子工程,所以打包方式必須是 pom -->
<packaging>pom</packaging>
2.子工程
<!-- 使用parent標簽指定當(dāng)前工程的父工程 -->
<parent>
<!-- 父工程的坐標 -->
<groupId>com.atguigu.maven</groupId>
<artifactId>pro03-maven-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- 子工程的坐標 -->
<!-- 如果子工程坐標中的groupId和version與父工程一致,那么可以省略 -->
<!-- <groupId>com.atguigu.maven</groupId> -->
<artifactId>pro04-maven-module</artifactId>
<!-- <version>1.0-SNAPSHOT</version> -->
父工程依賴統(tǒng)一管理
-
父工程聲明版本
<!-- 使用dependencyManagement標簽配置對依賴的管理 -->
<!-- 被管理的依賴并沒有真正被引入到工程 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>6.0.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.10</version>
</dependency>
</dependencies>
</dependencyManagement>
?子工程引用版本
<!-- 子工程引用父工程中的依賴信息時,可以把版本號去掉。 -->
<!-- 把版本號去掉就表示子工程中這個依賴的版本由父工程決定。 -->
<!-- 具體來說是由父工程的dependencyManagement來決定。 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
</dependencies>
?
2. Maven工程聚合關(guān)系
-
聚合概念
Maven 聚合是指將多個項目組織到一個父級項目中,以便一起構(gòu)建和管理的機制。聚合可以幫助我們更好地管理一組相關(guān)的子項目,同時簡化它們的構(gòu)建和部署過程。
-
聚合作用
-
管理多個子項目:通過聚合,可以將多個子項目組織在一起,方便管理和維護。
-
構(gòu)建和發(fā)布一組相關(guān)的項目:通過聚合,可以在一個命令中構(gòu)建和發(fā)布多個相關(guān)的項目,簡化了部署和維護工作。
-
優(yōu)化構(gòu)建順序:通過聚合,可以對多個項目進行順序控制,避免出現(xiàn)構(gòu)建依賴混亂導(dǎo)致構(gòu)建失敗的情況。
-
統(tǒng)一管理依賴項:通過聚合,可以在父項目中管理公共依賴項和插件,避免重復(fù)定義。
-
-
聚合語法
父項目中包含的子項目列表。
<project>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>child-project1</module>
<module>child-project2</module>
</modules>
</project>
聚合演示
通過觸發(fā)父工程構(gòu)建命令、引發(fā)所有子模塊構(gòu)建!產(chǎn)生反應(yīng)堆!文章來源:http://www.zghlxwxcb.cn/news/detail-772787.html
ps:來源尚硅谷文章來源地址http://www.zghlxwxcb.cn/news/detail-772787.html
到了這里,關(guān)于新年快樂!學(xué)習(xí)Java第84天,Maven工程繼承和聚合關(guān)系的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!