-
什麼是pom
-
pom代表項目對象模型,它是Maven中工作的基本組成單位。它是一個XML文件,始終保存在項目的基本目錄中的pom.xml文件中。pom包含的對象是使用maven來構建的,pom.xml文件包含了項目的各種配置信息,需要特別注意,每個項目都只有一個pom.xml文件。
-
-
項目配置信息
-
project:工程的根標籤
-
modelVersion:pom模型版本,maven2和3只能爲4.0.0
-
-
artifactId:這是工程的標識。它通常是工程的名稱。例如,消費者銀行。groupId 和 artifactId 一起定義了 artifact 在倉庫中的位置
-
version:這是工程的版本號。在 artifact 的倉庫中,它用來區分不同的版本
-
packaging:定義 Maven 項目的打包方式,有 JAR 、WAR 和 EAR 三種格式
-
-
依賴配置信息
-
dependencies<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
-
parent<parent><groupId>xd.class<groupId><artifactId>demo-parent</artifactId><relativePath>/</relativePath><version>1.0</version></parent>
-
groupId: 父項目的組Id標識符
-
artifactId:父項目的唯一標識符
-
relativePath:Maven首先在當前項目中找父項目的pom,然後在文件系統的這個位置(relativePath),然後在本地倉庫,再在遠程倉庫找。
-
version: 父項目的版本
-
-
modules
-
有些maven項目會做成多模塊的,這個標籤用於指定當前項目所包含的所有模塊。之後對這個項目進行的maven操作,會讓所有子模塊也進行相同操作。<modules><module>com-a</module><module>com-b</module><module>com-c</module></modules>
-
-
properties
-
用於定義pom常量<properties><java.version>1.7</java.version></properties>
-
-
dependencyManagement
-
應用場景
-
當我們的項目模塊很多的時候,我們依賴包的管理就會出現很多問題,爲了項目的正確運行,必須讓所有的子項目使用依賴項的同一版本,確保應用的各個項目的依賴項和版本一致,才能保證測試的和發佈的是相同的結果。
-
-
使用的好處
-
在父模塊中定義後,子模塊不會直接使用對應依賴,但是在使用相同依賴的時候可以不加版本號,這樣的好處是,可以避免在每個使用的子項目中都聲明一個版本號,這樣想升級或者切換到另一個版本時,只需要在父類容器裏更新,不需要任何一個子項目的修改
-
-
父項目:<dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.2.0</version><scope>test</scope></dependency></dependencies></dependencyManagement>子項目1:<dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency>子項目2:<dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency>子項目3: //如果子項目指定了版本,默認會是使用子項目的版本,如果沒有指定版本纔會使用父項目的版本<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>5.0</version></dependency>
-
和dependencies的區別
-
dependencies即使在子項目中不寫該依賴項,那麼子項目仍然會從父項目中繼承該依賴項(全部繼承)
-
dependencyManagement裏只是聲明依賴,並不實現引入,因此子項目需要顯示的聲明需要用的依賴。如果不在子項目中聲明依賴,是不會從父項目中繼承下來的;只有在子項目中寫了該依賴項,並且沒有指定具體版本,纔會從父項目中繼承該項,並且version和scope都讀取自父pom;另外如果子項目中指定了版本號,那麼會使用子項目中指定的jar版本。
-
-
-