最近因为公司 maven 私服出现问题,在修复问题的过程当中顺便整理了下 maven 私服配置的两种方式,在此记录下。maven
如下配置均在 settings.xml 中配置,私服配置不建议配置在某个项目的 pom.xml 文件中。url
maven 在默认状况下是从中央仓库下载构建,也就是 id 为 central 的仓库。若是没有特殊需求,通常只须要将私服地址配置为镜像,同时配置其代理全部的仓库就能够实现经过私服下载依赖的功能。镜像配置以下:代理
<mirror> <id>Nexus Mirror</id> <name>Nexus Mirror</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror>
当按照以上方式配置以后,全部的依赖下载请求都将经过私服下载。code
除了镜像方式的配置,咱们还可使用覆盖默认 central 仓库的方式来配置私服。xml
此配置中的远程仓库 id 是 central,也就是中央仓库的 id,这么配置的缘由就是使用当前配置的私服地址覆盖默认的中央仓库地址,配置以下:it
<profile> <activation> <!-- 默认激活此 profile --> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>central</id> <name>central</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>central</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
以上两种方式均可以实现配置 maven 私服的功能,我的建议使用镜像的方式配置,最方便简单,不易出错,同时将访问外部仓库的职责所有丢给私服,方便私服统一管理。io
PS:当碰到私服服务下载远程仓库 jar包,或者本地 deploy jar 到私服报 500 错误时,多半是由于私服的存储目录 nexus 没有写权限致使。class