Maven配置部署指北
Snapshots和release仓库
二者区别是什么
Snapshot[2],快照,是一种特殊的版本,指定了某个当前的开发进度的副本。不同于常规的版本,Maven 每次构建都会在远程仓库中检查新的快照。比如发布了一个app-1.0-SNAPSHOT版本,那么maven构件的时候,每次都会去仓库检查是否有最新的版本已经被发布。如果有,则替换。如果是release仓库,那么除非版本升级,否则maven构件的时候不会去检查最新的jar包,即便是在nexus仓库中已经更新了。
maven中的仓库分为两种[3],snapshot快照仓库和release发布仓库。snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本。
定义一个组件/模块为快照版本,只需要在pom文件中在该模块的版本号后加上-SNAPSHOT即可(注意这里必须是大写),如下:
1 | <groupId>com.schdri</groupId> |
Snapshot版本更新策略
maven会在什么时候检查更新jar包呢?可以在
也可以在使用maven的时候,通过命令行参数强制更新:
如何在POM中使用多个仓库
在POM中声明
只对当前工程生效[5]
1 | <project> |
每一个工程的POM都会继承自Super POM, 除非显示的声明不继承。
Super POM中的仓库包含了默认的maven 中央仓库如下[1]:
1 | <repositories> |
在settings.xml中配置
配置如下[6]
${user.home}/.m2/settings.xml or ${maven.home}/conf/settings.xml中声明仓库:
1 | <settings> |
仓库解析的顺序
Remote repository URLs are queried in the following order for artifacts until one returns a valid result:
effective settings:
Global settings.xml
User settings.xml
local effective build POM:
Local pom.xml
Parent POMs, recursively
Super POM
effective POMs from dependency path to the artifact.
For each of these locations, the repositories within the profiles are queried first in the order outlined at Introduction to build profiles.
Before downloading from a repository, mirrors configuration is applied.
Effective settings and local build POM, with profile taken into account, can easily be reviewed to see their repositories order with mvn help:effective-settings and mvn help:effective-pom -Dverbose.
仓库镜像mirror
mirror[4]配置只能存在于settings.xml中,用来指定对某个repository的镜像,一个repository只能指定一个mirror,如果配置多个mirror,那么maven不会做聚合,并且选择匹配的第一个。
如果强制要将所有的仓库都镜像的话,可以将
一些更多的匹配规则,从maven 2.0.9+版本以后开始支持:
- *星号,匹配所有
- external:*, 除了使用localhost和file://开始的仓库
- repo1,repo2 多个
- *,!repo1 非
Nexus中Maven仓库类型
hosted 本地存储。像官方仓库一样提供本地私库功能
proxy 提供代理其它仓库的类型
group 组类型,能够组合多个仓库为一个地址提供服务
如何将artifact发布到nexus仓库
增加Maven POM中的发布配置
1 | <distributionManagement> |
在settings.xml中增加鉴权
如果使用IDEA,那么请查看
找到对应的settings.xml中,增加如下配置:
1 | <!--配置服务端的一些设置。一些设置如安全证书不应该和pom.xml一起分发。这种类型的信息应该存在于构建服务器上的settings.xml文件中。--> |
如果使用mvn命令行,请参考MVN_HOME环境变量配置。