POM(project Object Model) Maven包管理依賴 pom.xml文件


什么是POM

POM全稱為“Project Object Model”,意思是工程對象模型。Maven工程使用pom.xml來指定工程配置信息,和其他文本信息。該配置文件以xml為格式,使用xml語法表明信息。

快速預覽

一個pom.xml文件主要包括以下元素信息:

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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>
 
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
 
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
 
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
 
<!-- Environment Settings -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>

 

詳解

The Basics

Maven Coordinates(Maven坐標)

groupId:artifactId:version構成了Maven工程的坐標系統。

  • groundId: 組織標識,例如:org.codehaus.mojo,在M2_REPO目錄下,將是: org/codehaus/mojo目錄。
  • artifactId:項目名稱,例如:my-project,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project目錄。
  • version:版本號,例如:1.0,在M2_REPO目錄下,將是:org/codehaus/mojo/my-project/1.0目錄。
  • packaging:打包格式,可選值:jar(默認值), maven-pluginejbwarearrarpar

POM Relationships(POM關系)

Maven主要用於處理項目之間的關系,包括依賴關系(和過渡依賴)、繼承和聚合(多模塊項目)。

Dependencies(依賴)

大多數每個項目都需要依賴其他人構建的項目,Maven則可以對這些依賴進行管理 。在
dependencies標簽中指明所需要依賴的功能模塊,Maven會自動下載編譯和鏈接其依賴關系。另外,Maven屏蔽了這些依賴帶來的更多的依賴項(依賴傳遞),讓開發者只需要關注自己項目需要的依賴關系。

junit4

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 4 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 5 ...
 6 <dependencies>
 7 <dependency>
 8 <groupId>junit</groupId>
 9 <artifactId>junit</artifactId>
10 <version>4.0</version>
11 <type>jar</type>
12 <scope>test</scope>
13 <optional>true</optional>
14 </dependency>
15 ...
16 </dependencies>
17 ...
18 </project>

 

  • groupId, artifactId, version: 用於精准定位dependency
  • classifier: 用於區分名字相同但內容不同的POM。例如:jdk15jdk14指明目標版本;sourcesjavadoc指明部署的是源碼還是文檔。
  • type: 與packging中的type相對應。
  • scope: 用於指明dependency作用范圍,有5種值:
    1. compile: 默認的scope,表示dependency可以在所有的生命周期中使用。而且,這些dependencies會傳遞到依賴的項目中。適用於所有階段,會隨着項目一起發布。
    2. provided:跟compile相似,但是表明了dependencyJDK或者容器提供,例如Servlet API和一些Java EE APIs。另外該dependency 只能作用在編譯和測試時,同時沒有傳遞性。
    3. runtime:表示dependency不作用在編譯時,但會作用在運行和測試時,如JDBC驅動,適用運行和測試階段。
    4. test:表示dependency只在測試時使用,用於編譯和運行測試代碼。不會隨項目發布。
    5. system:跟provided相似,但是在系統中要以外部JAR包的形式提供,maven不會在repository查找它。
  • systemPath:只在scopesystem時有效,指明dependency路徑。
  • optional:指明該項目沒有該dependency,依然可以正確運行。
Dependency Version Requirement Specification(依賴版本規范)
  • 1.0: “軟性”要求 1.0 (推薦方式)
  • [1.0]: “硬性要求” 1.0
  • (,1.0]: <= 1.0
  • [1.2,1.3]: 1.2 <= x <= 1.3
  • [1.0,2.0): 1.0 <= x < 2.0
  • [1.5,): x >= 1.5
  • (,1.0],[1.2,): x <= 1.0 or x >= 1.2
  • (,1.1),(1.1,): 排除 1.1
Exclusions(排除)

排除掉dependency的依賴傳遞中的某個dependency。和optional不同,exclusions不會安裝和使用該dependency,並從依賴樹上去除它。例如,某個依賴樹種某dependency可能會導致錯誤,則應該排除掉。

Inheritance(繼承)

POM對象可以實現繼承,子POM對象將從父POM繼承各屬性。

Aggregation(聚類(多模塊))

一個pom打包項目通過聚合多個模塊來構建,開發者不需要考慮模塊間的依賴關系。

Properties(屬性)

Maven中的Properties是占位符,可以在POM中任何一個地方使用符號${X},其中X是該Property。有以下5中形式:

  • env.X:系統變量,區分大小寫。例如:${env.PATH}表示系統系統路徑變量。
  • project.x:POM中相應的值。例如:<project><version>1.0</version></project>可以通過${project.version}訪問。
  • settings.x settings.xml中相應的值。例如,<settings><offline>false</offline></settings>可以通過${settings.offline}訪問。
  • Java System Properties:例如:${java.home}
  • x:POM中<properties />標簽中的值。例如:<properties><someVar>value</someVar></properies>可以通過${someVar}訪問。

Build Settings

Build(構建)

build元素分為兩塊:”project build”和”profile build”。

build

1 <!-- "Project Build" contains more elements than just the BaseBuild set -->
2 <build>...</build>
3  
4 <profiles>
5 <profile>
6 <!-- "Profile Build" contains a subset of "Project Build"s elements -->
7 <build>...</build>
8 </profile>
9 </profiles>

 

The BaseBuild Element Set 

BaseBuild

1 <build>
2 <defaultGoal>install</defaultGoal>
3 <directory>${basedir}/target</directory>
4 <finalName>${artifactId}-${version}</finalName>
5 <filters>
6 <filter>filters/filter1.properties</filter>
7 </filters>
8 ...
9 </build>

 


defaultGoal

    :默認執行行為。
  • directory:構建目錄。
  • finalName:文件名。
  • filter:過濾器。

The Build Element Set

Build:

<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
...
</build>

  


定義工程目錄。

Reporting(報告)

生成工程報告,如javadoc。

Reporting

<reporting>
<plugins>
<plugin>
...
<reportSets>
<reportSet>
<id>sunlink</id>
<reports>
<report>javadoc</report>
</reports>
<inherited>true</inherited>
<configuration>
<links>
<link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
</links>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>

 

 


 

More Project Information

  • name:工程名。
  • description:工程描述。
  • url:工程URL。
  • inceptionYear:開始時間。

Licenses(許可)

Licenses

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>

 


name, url和comments

    :許可描述。
  • distribution:指明工程如何分布。repo,代表可以從Maven repository下載;manual,代表必須手動安裝。

Organization(組織)

大多數項目都是由某組織(公司、私人組織等)管理。這是最基本的信息。

Organization

1
2
3
4
<organization>
<name>Codehaus Mojo</name>
<url>http://mojo.codehaus.org</url>
</organization>

 

Developers(開發者)

Developers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<developers>
<developer>
<id>jdoe</id>
<name>John Doe</name>
<email>jdoe@example.com<email>
<url>http://www.example.com/jdoe</url>
<organization>ACME</organization>
<organizationUrl>http://www.example.com</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>America/New_York</timezone>
<properties>
<picUrl>http://www.example.com/jdoe/pic</picUrl>
</properties>
</developer>
</developers>
  • id, name, email:開發者身份標識。
  • organization, organizationUrl:個人從屬組織信息。
  • roles:開發者在項目中的角色。
  • timezone:時區。
  • properties:個人屬性。

Contributors(參與者)

開源項目往往有很多Contributors參與,基本元素節點信息與Developers類似。

Environment Settings

Issue Management(問題管理)

定義了缺陷跟蹤系統(Bugzilla、TestTrack ClearQuest等)的使用,主要用於生成項目文檔。

Continuous Integration Management(連續集成管理)

用於自動化構建。

Mailing Lists(郵件列表)

項目開發人員聯系方式。

SCM(軟件配置管理)

SCM(Software Configuration Management,也叫Source Code/Control Management),用於版本控制。

Prerequisites(先決條件)

工程構建的先決條件。

Repositories(倉庫)

Repositories

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<repositories>
<repository>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
  • releases, snapshots:正式版和快照版(開發版)。
  • enabled:是否啟用。
  • updatePolicy:更新頻率。
  • checksumPolicy:校驗政策。
  • layout:Maven倉庫布局。

Activation(激活)

Activation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
</project>

在某些條件下修改工程配置。

 

reference

[1] https://sawyersun.github.io/2016/06/14/pom/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM