版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yu1014745867/article/details/88692730
一、什么是pom
pom代表项目对象模型,它是Maven中工作的基本组成单位。它是一个XML文件,始终保存在项目的基本目录中的pom.xml文件中。pom包含的对象是使用maven来构建的,pom.xml文件包含了项目的各种配置信息。
POM.xml包含:
- 项目组(groupId)
- 项目名(artifactId)
- 版本(version)
如图:
说明:以上属性在项目仓库是唯一标识的。需要特别注意,每个项目都只有一个pom.xml文件。
二、pom中的节点分布
<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>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<!-- 依赖配置 -->
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- 构建配置 -->
<build>...</build>
<reporting>...</reporting>
<!-- 项目信息 -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- 环境设置 -->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
三、各节点解释说明
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">
<!-- pom模型版本,maven2和3只能为4.0.0-->
<modelVersion>4.0.0</modelVersion>
<!-- 项目的组ID,用于maven定位-->
<groupId>com.company.bank</groupId>
<!-- 项目ID,通常是项目的名称,唯一标识符-->
<artifactId>parent</artifactId>
<!-- 项目的版本-->
<version>1.0</version>
<!-- 项目的打包方式-->
<packaging>war</packaging>
<project>
节点 | 解释说明 |
---|---|
modelVersion | pom模型版本,maven2和3只能为4.0.0 |
groupId | 这是项目组的编号,这在组织或项目中通常是独一无二的。 |
artifactId | 项目名称 |
version | 项目的版本。与groupId一起使用,artifact在存储库中用于将版本彼此分离。 |
packaging | 项目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par |
modules
有些maven项目会做成多模块的,这个标签用于指定当前项目所包含的所有模块。之后对这个项目进行的maven操作,会让所有子模块也进行相同操作。
<modules>
<module>my-projec1</module>
<module>my-projec2</module>
<module>my-projec3</module>
</modules>
说明:Maven 3支持Maven项目的多模块(multi-modules)结构。这样的Maven项目也被称为聚合项目,通常由一个父模块和若干个子模块构成。
其中,父模块必须以pom
打包类型,如下:
<packaging>pom</packaging>
parent
用于确定父项目的坐标位置。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
节点 | 解释说明 |
---|---|
groupId | 父项目的组Id标识符 |
artifactId | 父项目的唯一标识符 |
version | 父项目的版本 |
relativePath | Maven首先在当前项目中找父项目的pom,然后在文件系统的这个位置(relativePath),然后在本地仓库,再在远程仓库找。可以不填写。 |
artifactId
标签:spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
...
这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了。
spring-boot-starter-parent
是一个特殊的starter,它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签。
当我们搭建web应用的时候,可以像下面这样添加spring-boot-starter-web
依赖,如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
properties
用于定义pom常量.
如:
1、
<properties>
<java.version>1.7</java.version>
</properties>
上面这个常量可以在pom
文件的任意地方通过${Java.version}来引用。
2、
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
</properties>
依赖配置dependencies
项目相关依赖配置,如果在父项目写的依赖,会被子项目引用。一般会在父项目中定义子项目中所有共用的依赖。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
写在最后
参考文章:
1、maven之pom.xml配置文件详解
https://www.jianshu.com/p/0e3a1f9c9ce7