一. 本地仓库

本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库。这样在你下次使用的时候就不需要从远程下载了。如果你所需要的jar包版本在本地仓库没有,而且也不存在于远程仓库,Maven在构建的时候会报错,这种情况可能发生在有些jar包的新版本没有在Maven仓库中及时更新。

Maven缺省的本地仓库地址为${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。当然你可以通过修改${user.home}/.m2/settings.xml 配置这个地址:

Xml代码

  1. <settings>

  2. <localRepository> E:/repository/maven/repos</localRepository>

  3. </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从外网的哪个地方下载jar包
Maven的安装目录中,在lib目录下,maven-model-builder-3.1.0.jar中,有一个默认的pom.xml文件
其中就配置了Maven默认连接的中心仓库

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私服

私服也属于远程仓库的一种,只是这个远程仓库的地址是本地服务器而已

配置在局域网环境中,为局域网中所有开发人员提供jar包的统一管理
本地仓库(本机)--->私服(局域网)--->中心仓库(外部网络)
 
私服的安装
1.下载NEXUS,http://www.sonatype.org
2.解压
3.配置环境变量:
新建环境变量:NEXUS_HOME = E:\soft\nexus-2.5.1-01
加入到path中:%NEXUS_HOME%\bin;
4.打开CMD命令行
C:\Users\Administrator>nexus install     安装服务
C:\Users\Administrator>nexus start        启动服务
C:\Users\Administrator>nexus uninstall  卸载服务
5.访问私服
使用默认账户:admin 密码:admin123
NEXUS内部使用Jetty作为服务器
 http://localhost:8081/nexus   【界面用extjs开发的】
仓库的分类
查看Repository

host仓库--->内部项目的发布仓库
Snapshots   发布内部snapshots版本的仓库
Releases     发布内部release版本的仓库
3rd party      发布第3方jar包的仓库,如oracle数据库驱动,open-189.jar
 
proxy仓库--->从远程中心仓库查找jar包的仓库
Apache Snapshots    查找Apache项目的快照版本的仓库
Central   中心仓库http://repo1.maven.org/maven2/
Codehaus Snapshots    查找Codehaus 的快照版本的仓库
 
group仓库--->把仓库按组划分,以组为单位进行管理
 
virtual仓库  
 
私服的配置/ Repository的配置
在parent模块的pom.xml中加入私服的配置,让Maven从私服下载jar包,而不直接去远程仓库下载。
默认情况下,Maven下载jar包将直接连接到外网http://repo1.maven.org/maven2/去下载;
安装私服之后,让Maven下载jar包先从私服查找,如果没有,再从外网下载并保存在私服上
在POM在加入下面的配置,其中url为NEXUS私服的Public Repository对外的地址
以后,Maven下载构建(jar包或插件)都将从这里开始下载

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>
 
通过settings.xml来配置私服
由于所有的Maven项目都会用settings.xml中的配置进行解析,如果将Repository配置到这个文件中,那么对所有的Maven项目都将生效。
此时,Maven项目中的POM文件就不需要再配置私服地址了!
注意:修改settings.xml文件时,看IDE中关联的是哪个settings文件。
如C:\user\.m2目录下可能存在,Maven的解压目录下也存在,具体修改哪个根据实际情况而定。如,Eclipse下,查看Maven的User Settings选项即能看到关联。
我的IDE关联的是Maven\conf目录下的settings.xml:
E:\soft\apache-maven-3.1.0\conf\settings.xml
首先,通过<profile/>添加Repository和pluginRepository

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>
然后,使用<activeProfiles>对上面的配置进行激活(通过配置的id标识进行激活)

 
现在,本地机器上创建Maven项目,都会使用settings中有关仓库的配置了
本地仓库:
<localRepository>E:/repository/maven/repos</localRepository>
本地Maven下载的依赖包和插件都将放到E:/repository/maven/repos目录中
私服:
本地所有Maven项目,下载构建都统一从http://localhost:8081/nexus/content/groups/public/ 下载!
【私服上不存在某个构建时,再从远程仓库下载】
远程仓库:
如果远程仓库连接不上,则通过nexus修改central的地址即可!
当前使用Maven的默认配置:http://repo1.maven.org/maven2/

五. 镜像


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私服,镜像 详解的更多相关文章

  1. Maven入门系列(二)--设置中央仓库的方法

    原文地址:http://www.codeweblog.com/maven入门系列-二-设置中央仓库的方法/ Maven仓库放在我的文档里好吗?当然不好,重装一次电脑,意味着一切jar都要重新下载和发布 ...

  2. 清晰的教你如何将 Maven 项目上传至 中央仓库以及版本更新

    项目中总会依赖一些大牛的开源库,例如 Android 开发 通过以下方式就可以方便的引入库.那么如何将我们自己的库也能这样来使用呢?下面我们将详细的讲解每一步的过程: implementation ' ...

  3. Eclipse使用Maven时,修改默认中央仓库后的配置报错找不到包的问题解决

    一般在公司内容配置Maven时会在settings.xml文件下配置私服nexus地址,那么修改完之后在Eclipse中如果不指定用户目录级别的settings.xml文件会出现找不到包的问题. se ...

  4. 使用nexus搭建maven私服教程详解

    私服是什么 私服,私有服务器,是公司内部Maven项目经常需要的东东,不总结一下,不足以体现出重视.Nexus是常用的私用Maven服务器,一般是公司内部使用.下载地址是http://www.sona ...

  5. Maven的相关问题(一)——settings.xml配置详解

    工作中第一次正式接触maven,虽然之前在学习时有遇到过,但是对于maven的认识和理解确实太浅薄,仅仅局限于机械式的操,纸上得来终觉浅,绝知此事要躬行···古人诚不欺我也~ 下面先贴一个找到的一个非 ...

  6. (转)maven镜像详解

    背景:一直以来,对maven镜像不是特别的了解,这里通过对网上资料的收集,做个详细的记录. 镜像介绍 如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像.换句话说,任何一个可以从仓 ...

  7. Maven系列第6篇:生命周期和插件详解,此篇看过之后在maven的理解上可以超越同级别90%的人!

    maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第6篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...

  8. 对于maven创建spark项目的pom.xml配置文件(图文详解)

    不多说,直接上干货! http://mvnrepository.com/ 这里,怎么创建,见 Spark编程环境搭建(基于Intellij IDEA的Ultimate版本)(包含Java和Scala版 ...

  9. 怎么将本地项目放到码云(gitee)上面?图文详解

    git的好处什么的,在此就不多赘述.现在很多公司都在用git了. 那么怎么将本地已经有的项目放到码云(gitee)上呢? 前置条件说明: 1:原来项目所在位置:D:\workspace01\class ...

随机推荐

  1. django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

    报错现象 django 启动的时候报错 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 报错解决 记不清是我有毛 ...

  2. AtCoder ExaWizards2019题解

    AtCoder ExaWizards2019题解 AtCoder (因为代码直接用模板写的,可能有点冗长) A.Regular Triangle 给你三根棍子的长度,问你能否用他们组成等边三角形. 什 ...

  3. 【BZOJ1185】[HNOI2007]最小矩形覆盖(凸包,旋转卡壳)

    [BZOJ1185][HNOI2007]最小矩形覆盖(凸包,旋转卡壳) 题面 BZOJ 洛谷 题解 最小的矩形一定存在一条边在凸包上,那么枚举这条边,我们还差三个点,即距离当前边的最远点,以及做这条边 ...

  4. numpy nan值的判断

    我发现在数据处理中非常常见的就是nan值的判断,筛选数据尤为常见, 判断数据是否为nan,前提是np.float类型数组,但在应用于对象数组时会引发TypeError # 返回bool类型 np.is ...

  5. C#模拟POST表单提交 --- WebClient

    string postString = "arg1=a&arg2=b";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进 ...

  6. 第五篇 - Selenium突破反爬获取qq邮件标题

    from selenium import webdriver from selenium.webdriver import ActionChains #1.打开登陆页面 wd = webdriver. ...

  7. Python之常用模块--collections模块

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...

  8. 移动端click事件延时

    在移动端使用click事件会产生300ms的延迟 问题的产生: 移动端存在双击放大的问题,所以在移动端点击事件发生时,为了判断用户的行为(到底是要双击还是要点击),浏览器通常会等待300ms,如果30 ...

  9. Jquery Mobile表单

    三个前提: 1.每个form必须设置method和action属性 2.每个form必须有页面范围内唯一的id标识 3.每个form必须有一个label标签,通过设置它的for属性来匹配元素的id & ...

  10. 枚举类 enum,结构体类 struct

    1.枚举类型的值,直观易于理解,见词知意. 格式: enum 枚举类名:值类型 { 值1, 值2, 值n } 每个值默认(省略“:值类型”)以int型数据存储,从0开始. 使用格式:枚举类名 变量=枚 ...