news 2026/4/23 11:46:20

pom.xml

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
pom.xml

pom.xml

简介

pom.xml 文件是 Maven 项目的核心配置文件,全称是 “Project Object Model”(项目对象模型)。包含了项目的各种配置信息,如依赖管理、构建过程、插件配置等。Maven 使用 pom.xml 来管理和构建项目。

每个 Maven 项目都有且仅有一个 pom.xml 文件,它是 Maven 工作的基础。

主要功能:

1.项目基本信息:

groupId:项目的组ID,通常是公司或组织的域名反写。
artifactId:项目的唯一标识符,通常是项目的名称。
version:项目的版本号。
packaging:项目的打包方式,常见的有 jar、war 等。

2.依赖管理:

dependencies:定义项目所需的外部库和它们的版本。
dependencyManagement:集中管理依赖的版本,子模块可以直接引用而不需要指定版本。

3.构建配置:

build:配置项目的构建过程,包括编译、测试、打包等。
plugins:定义构建过程中使用的插件及其配置。

4.属性配置:

properties:定义一些常用的属性,如 Java 版本、编码等。

5.仓库配置:

repositories:定义从哪里下载依赖。
distributionManagement:定义发布构件的位置。

6.模块管理:

modules:多模块项目中定义子模块。

pom.xml 详细讲解

1. pom.xml 基本结构

一个典型的 pom.xml 文件具有如下基本结构:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.example</groupId><artifactId>my-app</artifactId><version>1.0.0</version><packaging>jar</packaging><!-- 项目信息 --><name>My Application</name><description>A sample Maven project</description><url>http://www.example.com</url><!-- 属性定义 --><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 依赖管理 --><dependencies><!-- 依赖项定义 --></dependencies><!-- 构建配置 --><build><!-- 插件配置 --><plugins><!-- 插件定义 --></plugins></build></project>

2. 项目坐标详解

Maven 使用一组坐标来唯一标识一个项目,这些坐标组成了项目的"地址"。

2.1 groupId(组ID)

  • 通常表示项目所属的组织或公司
  • 推荐使用反向域名的方式命名,例如:com.googleorg.apache
  • 示例:<groupId>com.company.project</groupId>

2.2 artifactId(构件ID)

  • 项目的唯一标识符
  • 通常与项目名称相同
  • 示例:<artifactId>my-webapp</artifactId>

2.3 version(版本号)

  • 项目的版本信息
  • 快照版本以-SNAPSHOT结尾
  • 示例:<version>1.0.0</version><version>2.1.0-SNAPSHOT</version>

2.4 packaging(打包方式)

  • 指定项目的打包类型
  • 常见:jar(默认)、warpomear
  • 示例:<packaging>war</packaging>

3. 属性(Properties)

属性提供了在 POM 中其他地方可以引用的占位符值。

<properties><!-- 项目源码编码 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- Java 版本 --><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><!-- 依赖版本管理 --><junit.version>5.8.2</junit.version><spring.version>5.3.21</spring.version></properties>

引用方式:${property.name},例如${junit.version}

4. 依赖管理(Dependencies)

依赖管理是 Maven 最核心的功能之一,它自动处理项目所需的第三方库。

4.1 基本依赖结构

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>

4.2 依赖范围(Scope)

Scope描述编译classpath测试classpath运行classpath示例
compile默认值,编译和运行都需要YYYSpring Core
provided编译和测试需要,运行时由容器提供YYNServlet API
runtime编译不需要,运行时需要NYYJDBC Driver
test仅测试时需要NYNJUnit
system类似 provided,需指定本地路径YYN本地 jar 包

4.3 依赖传递

Maven 支持依赖传递机制:

  • 当项目 A 依赖 B,B 依赖 C,则 A 自动依赖 C
  • 可以通过 exclusions 排除不需要的传递依赖
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.3.21</version><exclusions><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency>

5. 依赖管理(Dependency Management)

用于统一管理依赖版本,常用于父 POM:

<dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies></dependencyManagement>

子模块只需声明 groupId 和 artifactId,版本会从父 POM 继承。

6. 构建配置(Build)

构建配置控制项目的编译、测试、打包等过程。

6.1 基本构建配置

<build><!-- 项目最终名称 --><finalName>myapp</finalName><!-- 源码目录 --><sourceDirectory>src/main/java</sourceDirectory><!-- 资源目录 --><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><!-- 测试源码目录 --><testSourceDirectory>src/test/java</testSourceDirectory><!-- 输出目录 --><outputDirectory>target/classes</outputDirectory><testOutputDirectory>target/test-classes</testOutputDirectory></build>

6.2 插件管理

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version><configuration><source>11</source><target>11</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M7</version><configuration><skipTests>false</skipTests></configuration></plugin></plugins></build>

6.3 插件管理(Plugin Management)

类似于依赖管理,用于统一插件版本:

<pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version></plugin></plugins></pluginManagement>

7. 继承与聚合

7.1 继承(Parent)

子项目可以从父项目继承配置:

<parent><groupId>com.example</groupId><artifactId>parent-project</artifactId><version>1.0.0</version><relativePath>../parent/pom.xml</relativePath></parent>

7.2 聚合(Modules)

一个项目可以聚合多个子模块:

<modules><module>module-a</module><module>module-b</module><module>module-c</module></modules>

8. Profiles(环境配置)

Profiles 允许根据不同的环境激活不同的配置:

<profiles><profile><id>dev</id><properties><database.url>jdbc:mysql://localhost:3306/dev</database.url></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>prod</id><properties><database.url>jdbc:mysql://prod-server:3306/prod</database.url></properties></profile></profiles>

激活方式:

  • 命令行:mvn clean install -P prod
  • 环境变量:MAVEN_ARGS=-P prod

9. 常用 Maven 命令

命令功能说明
mvn clean清理编译结果
mvn compile编译项目
mvn test运行测试
mvn package打包项目
mvn install安装到本地仓库
mvn deploy发布到远程仓库
mvn site生成项目站点文档
mvn dependency:tree显示依赖树
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:48:45

Git克隆TensorRT仓库时 submodule 初始化方法

Git克隆TensorRT仓库时 submodule 初始化方法 在深度学习模型部署的实际工程中&#xff0c;一个看似简单的操作——git clone&#xff0c;却常常成为开发者“卡住”的第一道门槛。尤其是当目标项目如 NVIDIA 的 TensorRT 采用复杂的 submodule 结构时&#xff0c;若不加以注意&…

作者头像 李华
网站建设 2026/4/23 12:53:40

Seed-Coder-8B-Base与Codex对比:本地化AI编程的突围之路

Seed-Coder-8B-Base与Codex对比&#xff1a;本地化AI编程的突围之路 在智能编码工具席卷开发者的今天&#xff0c;GitHub Copilot 已经成为无数程序员键盘旁的“默认配置”。只需输入一段注释&#xff0c;模型便能自动生成函数、补全类结构&#xff0c;甚至写出完整的测试用例…

作者头像 李华
网站建设 2026/4/23 12:54:05

有道开源EmotiVoice:支持2000+音色的AI情感语音合成引擎

EmotiVoice&#xff1a;让AI语音真正“有声有色”的开源引擎 在智能语音助手还在用千篇一律的语调念天气预报时&#xff0c;你有没有想过——有一天&#xff0c;家里的音箱能用妈妈的声音温柔提醒你带伞&#xff0c;游戏里的NPC会因为被击败而带着哭腔求饶&#xff0c;甚至一段…

作者头像 李华
网站建设 2026/4/23 12:49:09

Docker安装过程中遇到权限问题?正确配置清华镜像可规避

Docker安装过程中遇到权限问题&#xff1f;正确配置清华镜像可规避 在日常使用 Docker 的过程中&#xff0c;不少开发者都曾遭遇过这样的尴尬&#xff1a;明明已经用 sudo 执行命令&#xff0c;或者自认为已加入 docker 用户组&#xff0c;却依然收到一条令人困惑的错误提示&am…

作者头像 李华
网站建设 2026/4/23 13:38:15

LobeChat能否支持RSS订阅?资讯聚合AI推送服务

LobeChat 与 RSS&#xff1a;构建智能资讯聚合的实践路径 在信息爆炸的时代&#xff0c;我们每天被成千上万条新闻、博客和论文包围。尽管获取信息变得前所未有的容易&#xff0c;但“读不过来”反而成了新的瓶颈。你有没有过这样的经历&#xff1f;收藏夹里堆满了未读文章&…

作者头像 李华
网站建设 2026/4/23 9:27:50

设备管理必备:USB ID 查询站点推荐

USB设备识别利器&#xff1a;从VID:PID到精准定位 你有没有遇到过这样的情况&#xff1f;插入一个USB摄像头&#xff0c;系统却只提示“未知设备”&#xff1b;或者在批量部署终端时&#xff0c;发现某些U盘根本无法被识别。这时候&#xff0c;那串看似无意义的 1a2b:3c4d 编码…

作者头像 李华