POM( Project Object Model,項目對象模型 ) 是 Maven 工程的基本工作單元,它是一個 XML 文件,包含了項目的基本信息,用於描述項目如何構建,聲明項目依賴等等。執行任務或目標時,Maven 會在當前目錄中查找並讀取 POM,獲取所需的配置信息,然後執行目標。 1、基本 ...
POM( Project Object Model,項目對象模型 ) 是 Maven 工程的基本工作單元,它是一個 XML 文件,包含了項目的基本信息,用於描述項目如何構建,聲明項目依賴等等。執行任務或目標時,Maven 會在當前目錄中查找並讀取 POM,獲取所需的配置信息,然後執行目標。
1、基本配置
<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.mycompany</groupId> <artifactId>myproject</artifactId> <version>1.0.0</version> <description>項目描述</description> <name>項目名稱</name> <url>https://www.mycompany.com/myproject</url> <packaging>jar</packaging> <inceptionYear>2023</inceptionYear> </project>
配置說明:
節點 | 說明 |
project | 工程的根標簽 |
modelVersion | 模型版本,設置為:4.0.0;必需 |
groupId | 工程組標識,它在一個組織或者項目中通常是唯一的;必需 |
artifactId | 工程標識,它通常是工程的名稱;必需 |
version | 工程版本號;必需 |
description | 工程描述 |
name | 工程名稱 |
url | 工程主頁 |
packaging | 打包類型,常見類型為:pom、jar、war、ear;預設為:jar |
inceptionYear | 項目創建年份,4 位數字;版權信息生成時需使用這個配置 |
2、parent
所有的 POM 都繼承自一個父 POM(無論是否顯式定義了這個父 POM),父 POM 包含了一些可以被繼承的預設設置。
<parent> <groupId>com.mycompany</groupId> <artifactId>parent-project</artifactId> <version>1.0</version> <relativePath/> </parent>
配置說明:
節點 | 說明 |
groupId | 父工程組標識;必需 |
artifactId | 父工程標識;必需 |
version | 父工程版本號;必需 |
relativePath | 父工程 pom.xml 文件的相對路徑;預設值為:../pom.xml。Maven 首先在當前工程的目錄尋找父工程的 pom,其次在 relativePath,然後在本地倉庫,最後在遠程倉庫尋找父項目的 pom |
3、prerequisites
prerequisites 元素用於描述構建該項目的先決條件。
<prerequisites> <maven>3.8.0</maven> </prerequisites>
配置說明:
節點 | 說明 |
maven | 構建項目或使用該插件所需要的 Maven 最低版本 |
4、issueManagement
issueManagement 元素用於描述項目的問題管理系統。
<issueManagement> <system>jira</system> <url>http://10.39.196.10:8080/banseon</url> </issueManagement>
配置說明:
節點 | 說明 |
system | 問題系統名稱 |
url | 問題系統 URL |
5、ciManagement
ciManagement 元素用於描述項目的持續集成系統。
<ciManagement> <system></system> <url></url> <notifiers> <notifier> <type></type> <sendOnError>true</sendOnError> <sendOnFailure>true</sendOnFailure> <sendOnSuccess>true</sendOnSuccess> <sendOnWarning>true</sendOnWarning> <address></address> <configuration/> </notifier> </notifiers> </ciManagement>
配置說明:
節點 | 說明 |
system | 集成系統名稱 |
url | 集成系統 URL |
notifiers | 構建完成時,需通知的人員列表,包括被通知者信息和通知條件 |
notifiers/notifier | 一個被通知人員配置信息 |
notifiers/notifier/type | 使用何種方法通知 |
notifiers/notifier/sendOnError | 發生錯誤時是否通知 |
notifiers/notifier/sendOnFailure | 構建失敗時是否通知 |
notifiers/notifier/sendOnSuccess | 構建成功時是否通知 |
notifiers/notifier/sendOnWarning | 發生警告時是否通知 |
notifiers/notifier/address | 通知發送到哪裡,該配置已過期 |
notifiers/notifier/configuration | 擴展配置項 |
6、mailingLists
mailingLists 元素用於描述項目相關的郵件信息。
<mailingLists> <mailingList> <name>jack</name> <post>[email protected]</post> <subscribe>[email protected]</subscribe> <unsubscribe>[email protected]</unsubscribe> <archive></archive> </mailingList> </mailingLists>
配置說明:
節點 | 說明 |
mailingList | 一個郵件信息 |
mailingList/name | 郵件名稱 |
mailingList/post | 用於接受外部信息的郵件地址或鏈接;如果是郵件地址,那麼在創建文檔時將自動創建一個 mailto 鏈接。 |
mailingList/subscribe | 用於訂閱的郵件地址或鏈接;如果是郵件地址,那麼在創建文檔時將自動創建一個 mailto 鏈接。 |
mailingList/unsubscribe | 用於取消訂閱的郵件地址或鏈接;如果是郵件地址,那麼在創建文檔時將自動創建一個 mailto 鏈接。 |
mailingList/archive | 可以瀏覽郵件信息的 URL |
7、licenses
licenses 元素用於列出該項目的 license 列表。
<licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> <distribution>repo</distribution> <comments></comments> </license> </licenses>
配置說明:
節點 | 說明 |
license | 表示一個 license |
name | license 的名稱 |
url | license 頁面 URL |
distribution | 項目的分發方式:repo,可以從 Maven 倉庫下載;manual 用戶須手動下載和安裝依賴 |
comments | license 的補充信息 |
8、developers
developers 用於列出項目開發者。
<developers> <developer> <id>123</id> <name>abc</name> <email>[email protected]</email> <url>https://www.abc.com</url> <roles> <role>Project Manager</role> </roles> <organization>xyz</organization> <organizationUrl>https://www.xyz.com</organizationUrl> <properties> <dept>aa</dept> </properties> <timezone>8</timezone> </developer> </developers>
配置說明:
節點 | 說明 |
developer | 表示一個開發者 |
id | 項目開發者的 id |
name | 項目開發者的姓名 |
項目開發者的 email | |
url | 項目開發者的個人主頁 |
roles、role | 項目開發者在項目中扮演的角色 |
organization | 項目開發者所屬組織 |
organizationUrl | 項目開發者所屬組織的 URL |
properties | 項目開發者屬性 |
timezone | 項目開發者所在時區,-11 到 12 範圍內的整數 |
9、contributors
contributors 元素描述尚未成為提交者的項目貢獻者。
<contributors> <contributor> <name>abc</name> <email>[email protected]</email> <url>https://www.abc.com</url> <roles> <role>Project Tester</role> </roles> <organization>xyz</organization> <organizationUrl>https://www.xyz.com</organizationUrl> <properties> <dept>aa</dept> </properties> <timezone>8</timezone> </contributor> </contributors>
配置說明:
節點 | 說明 |
contributor | 表示一個貢獻者 |
name | 貢獻者的姓名 |
貢獻者的 email | |
url | 貢獻者的個人主頁 |
roles、role | 貢獻者在項目中扮演的角色 |
organization | 貢獻者所屬組織 |
organizationUrl | 貢獻者所屬組織的 URL |
properties | 貢獻者屬性 |
timezone | 貢獻者所在時區,-11 到 12 範圍內的整數 |
10、scm
SCM(Source Control Management)元素用於配置項目的代碼庫。
<scm> <url>https://github.com/abc/abc-common</url> <connection>https://github.com/abc/abc-common.git</connection> <developerConnection>https://github.com/abc/abc-common-dev.git</developerConnection> <tag></tag> </scm>
配置說明:
節點 | 說明 |
url | 可瀏覽代碼庫的 URL |
connection | 可以連接到代碼庫的 URL,該連接只讀 |
developerConnection | 開發者連接到代碼庫的 URL,該連接可寫 |
tag | 當前代碼的標簽,在開發階段預設為 HEAD |
11、organization
organization 元素用於描述項目所屬組織。
節點 | 說明 |
name | 組織的全名 |
url | 組織的主頁 URL |
12、properties
properties 用於定義鍵值對。
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.2.24.RELEASE</spring.version> </properties>
13、dependencies
dependencies 用於定義項目的依賴。
<dependencies> <dependency> <groupId>com.abc</groupId> <artifactId>commons</artifactId> <version>1.0.0</version> <type>jar</type> <classifier></classifier> <scope>compile</scope> <!--systemPath></systemPath--> <exclusions> <exclusion> <groupId>com.xyz</groupId> <artifactId>xxx</artifactId> </exclusion> </exclusions> </dependency> <optional>true</optional> </dependencies>
配置說明:
節點 | 說明 |
dependency | 表示一個依賴 |
groupId | 依賴工程組標識 |
artifactId | 依賴工程標識 |
version | 依賴工程版本 |
type | 依賴類型,預設為 jar;它通常表示依賴文件的擴展名 |
classifier | 依賴的分類器,它可以區分屬於同一個 POM,但不同構建方式生成的構件。分類器名被附加到文件名的版本號後面。 |
scope | 依賴範圍,可以為:compile、test、runtime、provided、system;預設為:compile |
systemPath | 僅供 system 範圍使用。該元素為依賴指定了文件系統上的路徑,需要絕對路徑而不是相對路徑。 |
exclusions | 排除該依賴工程不需要的依賴 |
exclusion | 表示一個排除的依賴。groupId:排除的依賴工程組標識;artifactId:排除的依賴工程標識,可以用 * 表示任意個字元 |
optional | 可選依賴,它阻斷依賴的傳遞性。對於該依賴的依賴將不會被引入。 |
14、dependencyManagement
dependencyManagement 元素用於對依賴進行管理;這部分的依賴信息不會被立即解析,而是當在 dependencies 中聲明一個依賴(groupId 和 artifactId 必須),如果 groupId 和 artifactId 以外的一些信息沒有描述,則通過 groupId 和 artifactId 匹配到這裡的依賴,並使用這裡的依賴信息。
<dependencyManagement> <dependencies> <dependency> ... </dependency> </dependencies> </dependencyManagement>
15、modules
<modules> <module>module-a</module> <module>module-b</module> <module>module-c</module> </modules>
modules 元素用於列出項目的模塊(子項目)。
配置說明:
節點 | 說明 |
module | 一個模塊,元素值為指向該模塊的相對路徑 |
16、reporting
reporting 元素用於生成報表,當執行"mvn site",報表就會按照配置生成。
<reporting> <excludeDefaults>false</excludeDefaults> <outputDirectory>${project.build.directory}/site</outputDirectory> <plugins> <plugin> <groupId>com.abc</groupId> <artifactId>xyz</artifactId> <version>1.0.0</version> <inherited>true</inherited> <configuration></configuration> <reportSets> <reportSet> <id>123</id> <configuration/> <inherited>true</inherited> <reports> <report></report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting>
配置說明:
節點 | 說明 |
excludeDefaults | 是否排除預設報表,預設:false |
outputDirectory | 報表生成目錄,預設:${project.build.directory}/site |
plugins | 報表插件列表 |
plugins/plugin | 一個報表插件 |
plugins/plugin/groupId | 插件 groupId |
plugins/plugin/artifactId | 插件 artifactId |
plugins/plugin/version | 插件版本 |
plugins/plugin/inherited | 配置是否傳播到子 POM |
plugins/plugin/configuration | 插件配置 |
plugins/plugin/reportSets | 報表規範列表,每個規範(可能)具有不同的配置,一個規範(報表集)對應一個執行目標 |
plugins/plugin/reportSets/reportSet | 一個報表規範 |
plugins/plugin/reportSets/reportSet/id | 報表規範唯一標識符 |
plugins/plugin/reportSets/reportSet/configuration | 報表規範配置 |
plugins/plugin/reportSets/reportSet/inherited | 配置是否傳播到子 POM |
plugins/plugin/reportSets/reportSet/reports | 該規範使用到的報表列表 |
plugins/plugin/reportSets/reportSet/reports/report | 一個報表 |
17、build
build 元素用於描述項目如何構建。
<build> <sourceDirectory>src/main/java</sourceDirectory> <scriptSourceDirectory></scriptSourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <outputDirectory>target/classes</outputDirectory> <testOutputDirectory>target/test-classes</testOutputDirectory> <directory>target</directory> <finalName>${project.artifactId}-${project.version}</finalName> <defaultGoal>clean package</defaultGoal> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>*.xml</include> </includes> <excludes> <exclude>*.abc</exclude> </excludes> <targetPath></targetPath> <filtering>false</filtering> </resource> </resources> <testResources> <testResource> <directory>src/test/resources</directory> <includes> <include>*.xml</include> </includes> <excludes> <exclude>*.abc</exclude> </excludes> <targetPath></targetPath> <filtering>false</filtering> </testResource> </testResources> <filters> <filter></filter> </filters> <extensions> <extension> <groupId>com.abc</groupId> <artifactId>my-extension</artifactId> <version>1.0.0</version> </extension> </extensions> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <extensions>false</extensions> <executions> <execution> <id>attach-source</id> <phase></phase> <goals> <goal>jar-no-fork</goal> </goals> <inherited>true</inherited>