关于Maven我应该知道的事

这里不是入门指南, 而是一些个人使用遇到的问题参考

一、环境

Maven 版本JDK要求

Maven Java
3.3 - 3.6 7
3.2 6
3.0 - 3.1 5
  • 1.0 - 2.x : End Of Life

官方文档参考 : Maven Releases History

二、Maven Plugins

  • clean : 构建后清理
  • compile : 编译 Java 源代码
  • deploy : 将构建的构件部署到远程存储库; 在集成或发布环境中完成,将最终包复制到远程存储库中,以便与其他开发人员和项目共享
  • install : 将构建的构件安装到本地存储库中; 作为本地项目的依赖项。
  • package : 使用已编译的代码,并将其打包成可部署格式,例如JAR。

官方文档参考 : Maven Plugins

三、 setting.xml 加载顺序

  • 文件位置: 官网推荐是 USER_HOME/.m2/settings.xml, 想想也是, 这样升级 Maven 时就不用担心配置文件被覆盖了.
    • 全局配置: ${M2_HOME}/conf/settings.xml
    • 用户配置: user.home/.m2/settings.xml
  • 优先级 : pom.xml > user settings > global settings
    • 应用运行时, 会合并这些配置内容, 如果重复, 则高优先级覆盖低优先级

参考文章: maven全局配置文件settings.xml详解

四、mirror 和 repository

如果遇到配置了mirrors 还是不能从镜像中下载依赖包就要考虑这两者之间的区别联系了

  • settings.xml
  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   | 这是用于从远程存储库下载工件的镜像列表。
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   | 它的工作方式是这样的:POM可以声明一个存储库以用于解决某些工件。
   | 但是,此存储库有时可能会遇到流量大的问题,因此人们已经镜像
   | 它到几个地方。
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   | 该存储库定义将具有唯一的ID,因此我们可以为此创建一个镜像引用
   | 存储库,用作备用下载站点。 镜像站点将是首选该存储库的服务器。
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     | 指定要使用的存储库镜像站点而不是给定的存储库。 该存储库
     | 此镜像服务的ID与该镜像的mirrorOf元素匹配。 使用ID
     | 用于继承和直接查找目的,并且在整个镜像集中必须唯一。
     
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors
  • pom.xml
<project>
  ...
  <repositories>
    <repository>
      <id>my-internal-site</id>
      <url>http://myserver/repo</url>
    </repository>
  </repositories>
  ...
</project>

重点

  • setting.xml 中的 <mirrorOf> 要与 pom.xml 文件中的 <repository?id 相对应;
  • 如果设置 <mirrorOf>*</mirrorOf> 则会覆盖其后的<mirror>, 所以一般会将 <mirrorOf>*</mirrorOf>的镜像配置放在 <mirrors> 的最后

参考文章: maven的mirror和repository加载顺序
官方: Guide to Mirror Settings
官方: Settings Reference
官方: POM Reference

五、文献资料

官方文档里面几乎可以找到你想要的内容


作者:小鸣的微笔记
出处:52liming.cnblogs.com
转载:文章可以转载,但是请标注原文链接
原文:https://www.cnblogs.com/52liming/p/12834485.html

posted @ 2020-05-08 00:07  小鸣Cycling  阅读(188)  评论(0编辑  收藏  举报