当前位置: 首页 > news >正文

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显示依赖树
http://www.cnnetsun.cn/news/88491.html

相关文章:

  • Qwen3-VL-30B 4bit量化版发布:单卡部署突破
  • FLUX.1-ControlNet自定义控制模式全解
  • Windows server 2019 离线安装docker容器
  • springboot基于uniapp的有机农产品商城电商平台_4747f8w7-小程序
  • 用Dify构建文生视频工作流:从输入到输出
  • 基于Android的高校教室预约管理平台系统(源码+lw+部署文档+讲解等)
  • 开源不输商用!LobeChat媲美ChatGPT的用户体验实测
  • 十三、Kafka基础环境实战
  • EmotiVoice 安装与环境配置指南
  • LobeChat能否实现AI专利检索?技术创新辅助工具开发
  • vue基于spring boot的乡村民宿预订周边旅游管理系统
  • 网安零基础必冲!upload-labs 文件上传漏洞保姆级通关教程
  • vue基于Springboot框架 新能源充电桩报修管理系统
  • v3基于SpringBoot的酒店管理系统
  • Git安装Windows版本并配置清华镜像用于TensorFlow贡献开发
  • Langchain-Chatchat 0.3.1 Windows本地部署指南
  • 私有云ACK:企业智能化转型的安全基座与算力引擎
  • Docker部署Qwen3-14B及GPU加速实战
  • SWIR相机
  • vLLM 0.11.0 发布:全面移除 V0 引擎,性能与多模态支持再升级
  • 从零开始:使用Git安装TensorRT及其依赖组件
  • 模块十八.集合
  • FLUX.1-dev服装生成LoRA模型体验
  • 使用nexus3搭建自己的制品服务器
  • 38、Linux 邮件与网页浏览实用指南
  • 41、互联网服务实用指南
  • LLaMA-Factory微调与模型中断续训实战
  • GitHub项目实践:Fork并定制你的个性化Anything-LLM前端界面
  • pythonstudy Day37
  • Linly-Talker结合RAG技术实现知识增强型虚拟客服系统