Maven pom.xml配置详解
pom.xml是什么?
pom.xml(项目对象模型-Project Object Model的缩写)是Maven项目的核心配置文件,用于管理项目的依赖、插件、构建配置等。
举例说明:以阿里巴巴的fastjson为例:
<?xml version="1.0" encoding="UTF-8"?>
<!-- xml的版本和编码-->
<!-- xml 可扩展标记语言(EXtensible Markup Language)-->
<!-- xml 被设计用来传输和存储数据 html 被设计用来显示数据--><!-- xmlns-命名空间,类似包名,因为xml的标签可以自定义,所以需要命名空间来区分-->
<!-- xmlns:xsi-xml遵循的标签规范-->
<!-- xsi:schemaLocation-用来定义xmlschema的地址,也就是xml书写时需要遵循的语法,
两部分组成,前面部分就是命名空间的名字,后面是xsd(xmlschema)的地址-->
<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作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。
主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,
组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。--><!-- pom模型的版本 --><modelVersion>4.0.0</modelVersion>
<!-- 父级项目,parent的引用是固定的-->
<!-- 当我们创建一个 Spring Boot 工程时,可以继承自一个 fastjson2-parent-->
<!-- 父项目中定义了依赖的版本,我们继承了它,所以依赖dependence可以不写版本号,
若不用父项目中的版本号则自己用<version>标签指定--><parent><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-parent</artifactId><version>2.0.23</version><relativePath>../pom.xml</relativePath></parent><!-- 项目坐标,信息 --><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><name>fastjson1-compatible</name><description>Fastjson is a JSON processor (JSON parser + JSON generator) written in Java</description><packaging>jar</packaging><url>https://github.com/alibaba/fastjson2</url><inceptionYear>2022</inceptionYear><licenses> <!-- 许可证 --><license><name>Apache 2</name><url>https://www.apache.org/licenses/LICENSE-2.0.txt</url><distribution>repo</distribution><comments>A business-friendly OSS license</comments></license></licenses><scm> <!-- 源代码管理 --><url>https://github.com/alibaba/fastjson2</url><connection>scm:git:https://git@github.com/alibaba/fastjson2.git</connection></scm><organization> <!-- 组织 --><name>Alibaba Group</name><url>https://github.com/alibaba</url></organization><developers> <!-- 开发人员信息 --><developer><id>wenshao</id><name>wenshao</name><email>shaojin.wensj(at)alibaba-inc.com</email><roles><role>Developer</role><role>Tech Leader</role></roles><timezone>+8</timezone><url>https://github.com/wenshao</url></developer><developer><id>oldratlee</id><name>Jerry Lee</name><email>oldratlee(at)gmail.com</email><roles><role>Developer</role><role>CI/SCM Engineer</role></roles><timezone>+8</timezone><url>https://github.com/oldratlee</url></developer><developer><id>VictorZeng</id><name>Victor Zeng</name><email>Victor.Zxy(at)outlook.com</email><roles><role>Developer</role></roles><timezone>+8</timezone><url>https://github.com/VictorZeng</url></developer><developer><id>kraity</id><name>陆之岇</name><email>kat(at)krait.cn</email><roles><role>Developer</role></roles><timezone>+8</timezone><url>https://github.com/kraity</url></developer></developers><!-- 一些属性的设置-->
<!-- <properties> -->
<!-- JDK的版本--><!-- <java.version>1.8</java.version> --><!-- </properties> --><!-- <groupId>-主要用来唯一标识一个项目或者一组项目,通常是java包名的全称,例如上面的com.upc.pipeline-->
<!-- <artifactId>-用来标识同一groupId下不同的项目,例如spring-boot-starter-thymeleaf,都是这种格式的-->
<!-- <version>-用来标识一个artifact的版本,格式有8.0.21 0.0.1-SNAPSHOT 等-->
<!-- <scope>-用来表示当前的这个依赖(通过pom加载进来的包)所作用的场景,就是说应该把它添加到哪个环境当中,例如只在测试时此jar包生效,取值主要有compile-编译时(若未指定则为该默认值) runtime-运行时 test-测试时 等 -->
<!-- <optional>-标记依赖是否可以传递,默认值是false,可以用来减少项目之间jar包的冲突--><dependencyManagement> <!-- 申请子项目需要的依赖项 --><dependencies><!-- spring libs --><dependency><groupId>org.springframework</groupId><artifactId>spring-framework-bom</artifactId><type>pom</type><version>${springframework5.version}</version><scope>import</scope></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-bom</artifactId><type>pom</type><version>2021.2.5</version><scope>import</scope></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-bom</artifactId><type>pom</type><version>5.7.5</version><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies> <!-- 依赖项 --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension</artifactId><version>${project.version}</version></dependency><dependency><groupId>com.squareup.retrofit2</groupId><artifactId>retrofit</artifactId><scope>provided</scope></dependency><dependency><groupId>io.airlift</groupId><artifactId>slice</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>javax.ws.rs</groupId><artifactId>javax.ws.rs-api</artifactId><version>2.1.1</version><scope>provided</scope><optional>true</optional></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><scope>provided</scope></dependency><dependency><groupId>org.glassfish.jersey.core</groupId><artifactId>jersey-common</artifactId><scope>provided</scope></dependency><dependency><groupId>org.javamoney.moneta</groupId><artifactId>moneta-core</artifactId><scope>provided</scope></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-messaging</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-websocket</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><scope>provided</scope></dependency><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><scope>test</scope></dependency><dependency><groupId>com.aliyun.odps</groupId><artifactId>odps-sdk-udf</artifactId><scope>test</scope></dependency><dependency><groupId>com.chinamobile.cmos</groupId><artifactId>sms-core</artifactId><scope>test</scope></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><scope>test</scope></dependency><dependency><groupId>com.fasterxml.jackson.jaxrs</groupId><artifactId>jackson-jaxrs-json-provider</artifactId><scope>test</scope></dependency><dependency><groupId>com.fasterxml.jackson.module</groupId><artifactId>jackson-module-afterburner</artifactId><scope>test</scope></dependency><dependency><groupId>com.fasterxml.jackson.module</groupId><artifactId>jackson-module-kotlin</artifactId><scope>test</scope></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><scope>test</scope></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><scope>test</scope></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java-util</artifactId><scope>test</scope></dependency><dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId><scope>test</scope></dependency><dependency><groupId>com.jayway.jsonpath</groupId><artifactId>json-path</artifactId><scope>test</scope></dependency><dependency><groupId>com.jsoniter</groupId><artifactId>jsoniter</artifactId><scope>test</scope></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><scope>test</scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-spring-web</artifactId><scope>test</scope></dependency><dependency><groupId>net.minidev</groupId><artifactId>json-smart</artifactId><scope>test</scope></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><classifier>jdk15</classifier><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><scope>test</scope></dependency><!-- jvm lang --><dependency><groupId>org.clojure</groupId><artifactId>clojure</artifactId><version>1.11.1</version><scope>test</scope></dependency><dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy</artifactId><version>3.0.13</version><scope>test</scope></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><scope>test</scope></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-webapp</artifactId><scope>test</scope></dependency><dependency><groupId>org.gitlab4j</groupId><artifactId>gitlab4j-api</artifactId><scope>test</scope></dependency><dependency><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet</artifactId><scope>test</scope></dependency><dependency><groupId>org.glassfish.jersey.core</groupId><artifactId>jersey-client</artifactId><scope>test</scope></dependency><dependency><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-json-jackson</artifactId><scope>test</scope></dependency><dependency><groupId>org.glassfish.jersey.test-framework.providers</groupId><artifactId>jersey-test-framework-provider-jdk-http</artifactId><scope>test</scope></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><scope>test</scope></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><scope>test</scope></dependency><!-- spring libs --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><scope>test</scope></dependency></dependencies><build> <!-- 项目构建 --><plugins> <!-- 插件 --><plugin><artifactId>maven-surefire-plugin</artifactId><configuration><includes><include>com/alibaba/fastjson/**/*.java</include><include>com/alibaba/fastjson/**/*.kt</include></includes><systemPropertyVariables><user.timezone>Asia/Shanghai</user.timezone></systemPropertyVariables></configuration></plugin></plugins></build>
</project>
1.modules
modules 标签用于声明当前 Maven 项目包含的模块子项目,每个子项目都是一个独立的 Maven 项目,具有自己的 pom.xml 文件,可以进行独立构建和测试。在父项目的 pom.xml 文件中,使用 标签来列出所有子项目的名称,如下所示:
<project><groupId>com.example.parent</groupId><artifactId>parent-project</artifactId><version>1.0.0</version><packaging>pom</packaging><modules><module>child1</module><module>child2</module><module>child3</module></modules>
</project>
上述代码表示当前项目是一个 Maven 的多模块项目:它包含了三个子项目 child1、child2 和 child3,这三个子项目与 parent-project 有相同的 groupId 和 version,但是 artifactId 不同,它们的 pom.xml 都位于 parent-project 的根目录下。当使用 Maven 命令在 parent-project 下执行构建时,Maven 会对每个子模块执行构建,最终生成子项目的构件并复制到 parent-project 的 target 目录下
2.parent
parent 标签用于声明当前 Maven 项目的父项目,它可以将若干个 Maven 项目组织成一个整体,指定版本号,插件版本号等,便于管理和维护,在一个 Maven 项目中,使用标签来引用父项目,如下所示:
<project><groupId>com.example.child</groupId><artifactId>child-project</artifactId><version>1.0.0</version><packaging>jar</packaging><parent><groupId>com.example.parent</groupId><artifactId>parent-project</artifactId><version>1.0.0</version><relativePath>../parent-project/pom.xml</relativePath></parent>
</project>
上述代码表示当前项目 child-project 是 parent-project 的子项目,它的 groupId 和 version 都继承自 parent-project。元素是一个可选项,它的值是父项目 pom.xml 文件到子项目 pom.xml 文件的相对路径,如果子项目 pom.xml 和父项目 pom.xml 在同一目录下,则可以省略此元素。
3.properties
properties 严格来说,并不一定是项目本身的信息,而是人为设置的属性或者说宏,这个标签用来定义和管理项目中所需要的属性,其作用有以下几个:
统一管理项目中的常用属性,比如版本号、路径、插件版本等,方便统一修改和管理。
可以在配置过程中使用 ${…}占位符引用这些属性,使得配置更加灵活和便捷。
避免硬编码,提高代码的可维护性和可读性
<properties><project.name>demo-project</project.name><project.version>1.0.0</project.version><jdk.version>1.8</jdk.version>
</properties>
....省略其余部分
<dependency><groupId>com.example.demo</groupId><artifactId>${project.name}-api</artifactId><version>${project.version}</version>
</dependency>
四、项目的依赖列表
1.dependency
与项目的依赖列表相关的标签最外层由 <dependencies>来囊括,内部包含了各种具体的依赖<dependency>,该标签用于指定一个依赖项,它包含以下几个子标签
<groupId>:指定依赖项的groupId,项目的组名
<artifactId>:指定依赖项的artifactId,项目的唯一标识符
<version>:指定依赖项的版本号。
<scope>:指定依赖项在项目中的使用范围。
其中的 <scope>一般包含以下几种范围:常用的有compile、test、provided 和 runtime
compile:依赖库默认的 scope,表示该依赖库在编译、测试、运行时均需要使用。
provided:表示该依赖库只在编译和测试时需要使用,而在运行时已经被系统或者容器提供,所以不需要打包到最终的应用程序中。
runtime:表示该依赖库只在运行时需要使用,而在编译和测试时则不需要。
test:表示该依赖库只在测试时需要使用,而在编译和运行时则不需要。
比如说我们引入了 junit 包,但显然这个包我们不需要在打包时包含,只是用于测试,那么我们就可以将 junit 的 scope 设置为 test。
2.repository
当然,我们还能在pom文件中支持指定Maven仓库,即使用 <repositories> 和 <repository>标签,<repository>用于指定一个Maven仓库,它包含以下几个子标签:
<id>:指定Maven仓库的ID。
<name>:指定Maven仓库的名称。
<url>:指定Maven仓库的URL
有一个好的文章,供大家参考:
全面详解Maven的配置文件pom.xml(含常用plugin)_maven配置pom.xml-CSDN博客