java开发者的依赖管理工具—Maven
概念:Maven是java的一款依赖管理工具,它包括两方面的功能:版本一致管理、传递依赖关系。你可以使用他管理项目的版本和依赖元素,极大提高生产力。
如何使用Maven?
第一步,请在你的电脑上安装Maven环境,安装教程在这一篇博文打开:
第二步,请在你的Intellij IDEA 的集成开发环境中,通过File-New-Project-选择Maven Project-Next-Finish 建立一个基础的Maven项目。
建立好的Maven项目的根目录下会产生一个pom.xml文件,就是Maven依赖的配置文件,如下:
另外,你在IDEA中需要进入File-Setting-Maven 修改IDEA采用的Maven安装版本路径,以及Maven仓库地址。
Pom.xml是一个xml文件,它更定义了一个项目的所有依赖相关的配置。
一个pom文件应该要具备五个基本的标签,才能成为一个项目依赖配置。
(1) parent
所有pom项目的根。这个标签有3个可配置的属性。xmlns 命名空间 表示唯一的标识。xmlns:xsi 命名空间约束的唯一标识。xsi:schemaLocation 命名空间约束的使用手册地址。
(2)modelVersion
pom约束的版本,目前默认都是4.0.0
(3)groupId
组Id。一个组包含多个项目,是在本地仓库里属于同一个组的项目的根路径,一般可以设置成公司的标识。如com.xxx,org.xxx.
(4)artifactId
项目Id。项目的名称。一般用‘xxx-xxx-xxx’的格式。groupId.artifactId必须是本地唯一的。
(5)version
项目版本号。项目的开发周期一般是迭代,所以版本数量会随着迭代而增加。一个版本对应一个项目源码。
其他的标签可以参考官网的文档:Maven Model – Maven (apache.org)
为什么要版本统一?
同一个迭代周期的大项目下,各个子项目可以正常运行,互相和谐通信。但是不同迭代周期下的大项目下的各个子项目,可能会出现代码差异导致运行不正常,通信异常从而导致服务错误。所以,开发提倡同一个迭代周期应该用统一的版本管理,因此通过判断版本的差异,从而正常发布服务。
Pom如何实现版本统一?
BOM(Bills of Meterials) 物料清单可以帮助我们实现版本一致。
BOM的使用让一个集成很多子项目的大项目对外暴露唯一的版本。引用这个大项目的bom依赖,当其他项目需要使用这个项目子项目依赖时,无需再自己内部pom对子项目额外声明版本,轻松的做到了版本管理,因此也避免如上述所所版本冲突等版本不一致带来的各种问题。
使用Bom的方式需满足两个条件:
(一)bom项目的配置文件必须是type为pom的pom.xml文件
(二)bom项目必须有<dependencyManagement>标签,里面的内容是需要声明的依赖版本。
(三)引用bom项目中统一的版本声明,需要在<dependencyManagement>的内容的<dependency>定义<type>pom</type> ; <scope>import</scope>
(四)需要统一的版本需要在bom项目里集体管理,<properties>versionName</properties>标签定义各个子项目的版本号,可以在bom中统一修改,而无需再子项目里再修改,因为这些属性是可以通过${versionName}传递到子项目里,原理就是Maven项目的子项目继承父项目属性,所以再子项目以及bom的版本声明里统一引用${versionName}就是properties里声明的版本。
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.guo</groupId> <artifactId>guo-spring-frm-parent</artifactId> <version>2.0</version> <packaging>pom</packaging> <properties> <java.version>8</java.version> <spring.version>2.7.5</spring.version> <mybatis-frm.version>2.0</mybatis-frm.version> </properties> <!--一个maven项目里如果需要通过版本传递来达到版本统一的目的,可以使用dependencyManageMent 把引入的jar/pom等类型的maven项目导入,当且只有引入的项目里有dependencyManagement标签才可以声明,且 只有声明过的才可以传递到下一个maven项目以及他的子项目中--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-bom</artifactId> <version>2023.0.0</version> <type>bom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.guo</groupId> <artifactId>mybatis-frm</artifactId> <version>${mybatis-frm.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> </dependencies> </dependencyManagement> <modules> <module>mybatis-frm</module> </modules> </project>
上面这是bom项目的依赖配置。
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.guo</groupId> <artifactId>guo-spring-frm-parent</artifactId> <version>2.0</version> </parent> <artifactId>mybatis-frm</artifactId> <version>${mybatis-frm.version}</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>${spring.version}</version> </dependency>--> </dependencies> </project>
上面这是子项目的配置信息。
<?xml version="1.0" encoding="UTF-8"?> <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>org.example</groupId> <artifactId>maven-frm</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.guoziqi</groupId> <artifactId>bom</artifactId> <version>1.0</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.guo</groupId> <artifactId>guo-spring-frm-parent</artifactId> <version>2.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.guoziqi</groupId> <artifactId>spring-cloud-eureka</artifactId> </dependency> <dependency> <groupId>com.guo</groupId> <artifactId>mybatis-frm</artifactId> </dependency> </dependencies> </project>
上面这是我另一个项目引用bom的配置信息。引用后,在<dependencies></dependencies>里调用bom清单的依赖项目,无需再声明版本号了。
另外,由于bom项目和继承的子项目需要继承属性。所以要满足父子关系的声明,定义在子项目的<parent></parent>里。当然,bom项目也需要通过maven的命令管理子项目,将需要在bom父项目里添加<modules><moduels>块添加子项目的artifactId.子项目的artifactId最好和项目名称保持一致。
就这样粗糙的思路,记录一下,供大家取用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理