Maven 的 pom.xml 相关知识总结【持续更新】

2019年04月01日 10:22 · 阅读(1685) ·

[目录]

结合实例看 maven 传递依赖与优先级

结合实例看 maven 传递依赖与优先级

Maven 安装与配置

Maven 安装与配置

Maven POM

Maven 工程结构和内容被定义在一个 xml 文件中 - pom.xml,是 Project Object Model (POM) 的简称,此文件是整个 Maven 系统的基础组件。

所有 POM 文件都需要 project 元素和三个必需字段:groupIdartifactIdversion

节点 描述
project 工程的根标签。
modelVersion 模型版本需要设置为 4.0
groupId 这是工程组的标识。它在一个组织或者项目中通常是唯一的。例如,一个银行组织 com.companyname.project-group 拥有所有的和银行相关的项目。
artifactId 这是工程的标识。它通常是工程的名称。例如,消费者银行。groupId 和 artifactId 一起定义了 artifact 在仓库中的位置。
version 这是工程的版本号。在 artifact 的仓库中,它用来区分不同的版本。例如: com.company.bank:consumer-banking:1.0 , com.company.bank:consumer-banking:1.1

例子:

  1. <project xmlns = "http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <!-- 模型版本 -->
  6. <modelVersion>4.0.0</modelVersion>
  7. <!-- 公司或者组织的唯一标志,并且配置时生成的路径也是由此生成, 如com.companyname.project-group,maven会将该项目打成的jar包放本地路径:/com/companyname/project-group -->
  8. <groupId>com.companyname.project-group</groupId>
  9. <!-- 项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的 -->
  10. <artifactId>project</artifactId>
  11. <!-- 版本号 -->
  12. <version>1.0</version>
  13. </project>

父(Super)POM

父(Super)POM是 Maven 默认的 POM。所有的 POM 都继承自一个父 POM(无论是否显式定义了这个父 POM)。父 POM 包含了一些可以被继承的默认设置。因此,当 Maven 发现需要下载 POM 中的 依赖时,它会到 Super POM 中配置的默认仓库 http://repo1.maven.org/maven2 去下载。

Maven 使用 effective pom(Super pom 加上工程自己的配置)来执行相关的目标,它帮助开发者在 pom.xml 中做尽可能少的配置,当然这些配置可以被重写。

使用以下命令来查看 Super POM 默认配置:

mvn help:effective-pom

Maven 构建配置文件

构建配置文件是一系列的配置项的值,可以用来设置或者覆盖 Maven 构建默认值。使用构建配置文件,你可以为不同的环境,比如说生产环境(Producation)和开发(Development)环境,定制构建方式。

配置文件在 pom.xml 文件中使用 activeProfiles 或者 profiles 元素指定,并且可以通过各种方式触发。配置文件在构建时修改 POM,并且用来给参数设定不同的目标环境(比如说,开发(Development)、测试(Testing)和生产环境(Producation)中数据库服务器的地址)。

pom.xml 中定义和使用变量

如下

properties 中定义了 mybatisplus.version

version 中使用

  1. <properties>
  2. <mybatisplus.version>3.1.0</mybatisplus.version>
  3. </properties>
  4. <dependencies>
  5. <!--MyBatis-Plus-->
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus</artifactId>
  9. <version>${mybatisplus.version}</version>
  10. </dependency>
  11. </dependencies>

pom.xml 的 parent 引用

pom.xml 的 parent 引用

Maven 重新导入依赖

先清除缓存

点击 Invalidate and Restart

pom.xml 右键,Maven-Reimport

或者在右侧 Maven-Reimport All Maven Project

查看 Maven 依赖

参考:https://www.cnblogs.com/lvbinbin2yujie/p/10726122.html

右侧 Maven-Show Dependencies

解决 jar 包冲突

手动分析

比如项目中应用了下面两个

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-webmvc</artifactId>
  4. <version>6.0.9</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-aop</artifactId>
  9. <version>5.3.23</version>
  10. </dependency>

使用 mvn dependency:tree 查看依赖关系或者通过 Maven-项目-依赖项查看依赖关系

这里如果 spring-aop 冲突了,我想排除掉 spring-webmvc 中的 spring-aop,可以手动修改 pom

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-webmvc</artifactId>
  4. <version>6.0.9</version>
  5. <exclusions>
  6. <exclusion>
  7. <artifactId>spring-aop</artifactId>
  8. <groupId>org.springframework</groupId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-aop</artifactId>
  15. <version>5.3.23</version>
  16. </dependency>

使用插件 Maven Helper

参考:https://www.cnblogs.com/lvbinbin2yujie/p/10726122.html

Plugin 中搜索 Maven Helper

安装,重启就可以使用了。

Maven Helper 使用方式:安装成功以后,打开 pom 文件,黄色箭头中内容 Dependency Analyzer 出现就是安装成功。

切换到依赖分析面板 Dependency Analyzer,然后搜索冲突的 jar,这里我搜索 eureka

All Dependencies as Tree 界面就可以 Exclude,很方便的找到冲突的 jar

Exclude 最终也是在对应引用上添加 <exclusion> 来解决冲突

DependencyManagement 和 Dependencies

DependencyManagement 和 Dependencies

distributionManagement

  1. <!--
  2. 使用分发管理将本项目打成 jar 包,直接上传到指定服务器
  3. 用来发布 jar 到 maven 仓库,repoisotry 的 id 需要与 maven 的 setting.xml 保持一致
  4. 通过命令 mvn clean package, mvn deploy 发布
  5. -->
  6. <distributionManagement>
  7. <!-- 正式版本 -->
  8. <repository>
  9. <!-- nexus 服务器中用户名:在 setting.xml 中 <server> 的 id -->
  10. <id>internal.releases</id>
  11. <!-- 这个名称自己定义 -->
  12. <name>internal repository for releases</name>
  13. <url>http://localhost/nexus/content/repositories/thirdparty/</url>
  14. </repository>
  15. <!-- 快照 -->
  16. <snapshotRepository>
  17. <id>internal.snapshots</id>
  18. <name>internal repository for snapshots</name>
  19. <url>http://localhost/nexus/content/repositories/thirdparty-snapshots/</url>
  20. </snapshotRepository>
  21. </distributionManagement>