Maven 本地仓库,远程仓库,中央仓库,Nexus私服,镜像 详解
一. 本地仓库
本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库。这样在你下次使用的时候就不需要从远程下载了。如果你所需要的jar包版本在本地仓库没有,而且也不存在于远程仓库,Maven在构建的时候会报错,这种情况可能发生在有些jar包的新版本没有在Maven仓库中及时更新。
Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。当然你可以通过修改${user.home}/.m2/settings.xml 配置这个地址:
Xml代码
<settings>
<localRepository> E:/repository/maven/repos</localRepository>
</settings>
如果你想让所有的用户使用统一的配置那么你可以修改Maven主目录下的setting.xml:
${M2_HOME}/conf/setting.xml
二. 远程仓库
除本地仓库以外的仓库都叫做远程仓库
本地仓库配置在:
<localRepository> E:/repository/maven/repos</localRepository>
远程仓库配置在:
<profiles>
<profile>
<id></id>
<repositories>
<repository>远程仓库配置</repository>
</repositories>
</profile>
</profiles>
三. 中央仓库
中央仓库也属于远程仓库的一种,特征就是
<id>central</id> id名为 central,
Maven的中央仓库地址默认是:https://repo.maven.apache.org/maven2/,可以通过修改settings.xml文件来修改默认的中央仓库地址:
<profile>
<id>repository-profile</id>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</profile>
要注意的是如果修改的是中央仓库地址,那么repository下面的id标签值一定得是central,此外,还需要激活这个profile才能生效,这里的标签值就是profile标签下面的id标签值
<activeProfiles>
<activeProfile>repository-profile</activeProfile>
</activeProfiles>
四. Nexus私服
私服也属于远程仓库的一种,只是这个远程仓库的地址是本地服务器而已
Xml代码
<project> ... <!-- 配置私服地址 -->
<repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
</pluginRepositories> ... <project>
Xml代码
<settings> ... <profiles>
<profile>
<id>profile-nexus</id> <repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles> ... </settings>
Maven的镜像是指如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像,也就是说任何一个可以从仓库Y获得的依赖,都能够从它的镜像中获取。比如阿里的Maven仓库http://maven.aliyun.com/nexus/content/groups/public就可以理解为是中央仓库https://repo.maven.apache.org/maven2/在中国的镜像,由于地理位置的因素,该镜像往往能够提供比中央仓库更快的服务。要为一个仓库配置镜像只需要修改settings.xml文件,如下,其中的mirrorOf标签值就是仓库的id标签值,中央仓库就是默认就是central,表示这是为中央仓库配置的镜像,以后所有的依赖下载都会从这个镜像中进行下载。
<mirror>
<id>central-repository-mirror</id>
<name>Central Repository Mirror</name>
<mirrorOf>central</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
repository是指在局域网内部搭建的repository,它跟central repository, jboss repository等的区别仅仅在于其URL是一个内部网址
mirror则相当于一个代理,它会拦截去指定的远程repository下载构件的请求,然后从自己这里找出构件回送给客户端。配置mirror的目的一般是出于网速考虑。
不过,很多internal repository搭建工具往往也提供mirror服务,比如Nexus就可以让同一个URL,既用作internal
repository,又使它成为所有repository的mirror。
高级的镜像配置:
1.<mirrorOf>*</mirrorOf>
匹配所有远程仓库。这样所有pom中定义的仓库都不生效
2.<mirrorOf>external:*</mirrorOf>
匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。
3.<mirrorOf>repo1,repo2</mirrorOf>
匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。
4.<mirrorOf>*,!repo1</miiroOf>
匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。
mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性,id是唯一标识一个mirror就不多说了,name貌似没多大用,相当于描述,url是官方的库地址,mirrorOf代表了一个镜像的替代位置,例如central就表示代替官方的中央库。
我本以为镜像库是一个分库的概念,就是说当a.jar在第一个mirror中不存在的时候,maven会去第二个mirror中查询下载。但事实却不是这样,当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询。
后来终于知道,maven的mirror是镜像,而不是“分库”,只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。
还有,mirror也不是按settings.xml中写的那样的顺序来查询的。
所谓的第一个并不一定是最上面的那个。
当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询。
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers>
<server>
<id>repo-iss</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers> <mirrors>
<!-- osc镜像 -->
<mirror>
<!-- 镜像所有远程仓库,但不包括指定的仓库 -->
<id>mirror-osc</id>
<mirrorOf>external:*,!repo-osc-thirdparty,!repo-iss</mirrorOf>
<url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
<!--
<mirror>
<id>mirror-iss</id>
<mirrorOf>external:*</mirrorOf>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
</mirror>
-->
</mirrors> <profiles>
<profile>
<id>profile-default</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>repo-osc-thirdparty</id>
<url>http://maven.oschina.net/content/repositories/thirdparty/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>profile-iss</id>
<repositories>
<repository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo-iss</id>
<url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles> <activeProfiles>
<activeProfile>profile-default</activeProfile>
<!--<activeProfile>profile-iss</activeProfile>-->
</activeProfiles> <!--
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>10.10.204.160</host>
<port>80</port>
</proxy>
</proxies>
-->
</settings>
Reference:
https://my.oschina.net/zhanghaiyang/blog/606130
Maven 本地仓库,远程仓库,中央仓库,Nexus私服,镜像 详解的更多相关文章
- Maven入门系列(二)--设置中央仓库的方法
原文地址:http://www.codeweblog.com/maven入门系列-二-设置中央仓库的方法/ Maven仓库放在我的文档里好吗?当然不好,重装一次电脑,意味着一切jar都要重新下载和发布 ...
- 清晰的教你如何将 Maven 项目上传至 中央仓库以及版本更新
项目中总会依赖一些大牛的开源库,例如 Android 开发 通过以下方式就可以方便的引入库.那么如何将我们自己的库也能这样来使用呢?下面我们将详细的讲解每一步的过程: implementation ' ...
- Eclipse使用Maven时,修改默认中央仓库后的配置报错找不到包的问题解决
一般在公司内容配置Maven时会在settings.xml文件下配置私服nexus地址,那么修改完之后在Eclipse中如果不指定用户目录级别的settings.xml文件会出现找不到包的问题. se ...
- 使用nexus搭建maven私服教程详解
私服是什么 私服,私有服务器,是公司内部Maven项目经常需要的东东,不总结一下,不足以体现出重视.Nexus是常用的私用Maven服务器,一般是公司内部使用.下载地址是http://www.sona ...
- Maven的相关问题(一)——settings.xml配置详解
工作中第一次正式接触maven,虽然之前在学习时有遇到过,但是对于maven的认识和理解确实太浅薄,仅仅局限于机械式的操,纸上得来终觉浅,绝知此事要躬行···古人诚不欺我也~ 下面先贴一个找到的一个非 ...
- (转)maven镜像详解
背景:一直以来,对maven镜像不是特别的了解,这里通过对网上资料的收集,做个详细的记录. 镜像介绍 如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像.换句话说,任何一个可以从仓 ...
- Maven系列第6篇:生命周期和插件详解,此篇看过之后在maven的理解上可以超越同级别90%的人!
maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第6篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...
- 对于maven创建spark项目的pom.xml配置文件(图文详解)
不多说,直接上干货! http://mvnrepository.com/ 这里,怎么创建,见 Spark编程环境搭建(基于Intellij IDEA的Ultimate版本)(包含Java和Scala版 ...
- 怎么将本地项目放到码云(gitee)上面?图文详解
git的好处什么的,在此就不多赘述.现在很多公司都在用git了. 那么怎么将本地已经有的项目放到码云(gitee)上呢? 前置条件说明: 1:原来项目所在位置:D:\workspace01\class ...
随机推荐
- 卢卡斯定理&扩展卢卡斯定理
卢卡斯定理 求\(C_m^n~mod~p\) 设\(m={a_0}^{p_0}+{a_1}^{p_1}+\cdots+{a_k}^{p_k},n={b_0}^{p_0}+{b_1}^{p_1}+\cd ...
- restfull api交互常用状态码
2xx (成功类别) 200 Ok:标准的 HTTP 响应,表示 GET.PUT 或 POST 的处理成功. 201 Created:在创建新实例时,应返回此状态代码.例如,使用 POST 方法创建一 ...
- git同步远程已删除的分支和删除本地多余的分支
使用 git branch -a 可以查看本地分支和远程分支情况 但远程分支(红色部分)删除后,发现本地并没有同步过来. 一. 同步本地的远程分支 查看本地分支和追踪情况: git remote sh ...
- 【 HDU2966 】In case of failure(KD-Tree)
BUPT2017 wintertraining(15) #5E HDU - 2966 题意 给平面直角坐标系下的n个点的坐标,求离每个点和它最近点的距离的平方.\(2 \le n \le 10^5\) ...
- JS判断手机还是电脑访问网站
function check() { let browser = navigator.userAgent;//用户访问的类型 let phone = ["Android", &qu ...
- codeforces Hello 2019(未写完)
A. Gennady and a Card Game a题惯例签到题 题意:给你一张牌,再给你5张牌,判断能不能出一次牌... 所以只要检查第二行中的某个卡是否与第一行中的卡具有共同字符 有就输出YE ...
- MVC之 自定义过滤器(ActionFilterAttribute)
一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttribute类的方法签名. //表示所有操作 ...
- 被addPropertyChangeListener("...",this)差点搞崩溃
以前常用的是addPropertyChangeListener(this)方法 记得有一天我发现还有另一种写法: addPropertyChangeListener(String propertyNa ...
- 数据库之sql语句汇总20180616
/*******************************************************************************************/ 一.SQL简 ...
- 第二十五篇-Android 应用资源
这里介绍android的一些资源文件. 以一个登录界面为例. layout.xml <?xml version="1.0" encoding="utf-8" ...