<?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">
<!-- 本地仓库的位置 -->
<localRepository>${user.home}/.m2/repository</localRepository>
<!-- Apache Maven 配置 -->
<pluginGroups/>
<proxies/>
<!-- 私服发布的用户名密码 -->
<servers>
<server>
<id>releases</id>
<username>deployment</username>
<password>He2019</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>He2019</password>
</server>
</servers>
<!-- 阿里云镜像 -->
<mirrors>
<!-- 将 central 的请求重定向到阿里云的公共 Maven 仓库 -->
<!-- 其它的不重定向到阿里云 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<!-- https://maven.aliyun.com/repository/public/ -->
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- 配置: java8, 先从阿里云下载, 没有再去私服下载 -->
<!-- 测试结果: 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 -->
<profiles>
<!-- 全局JDK1.8配置 -->
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<!-- 这里的 repositories 如果不配置的话,默认会有一个 Maven 中央仓库的配置,
同样 pluginRepositories 中如果没有配置的话,默认也是有一个 Maven 中央仓库的配置。
还有!如果 repositories 中没有配置 repository.id 是 central 的 repository,
会自动增加一个 Maven 中央仓库的配置,并且是以追加的方式,也就是配置在 repositories 的最后一个。
所以如果只配置了私服的 repository 情况下,就会先去私服中下载,私服中下载不到时再去追加上来的 Maven 中央仓库中下载-->
<!-- Nexus私服配置: 第三方jar包下载, 比如oracle的jdbc驱动等 -->
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.100.241:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.100.241:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<!-- 阿里云配置: 提高国内的jar包下载速度 -->
<profile>
<id>ali</id>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活配置 -->
<activeProfiles>
<activeProfile>jdk1.8</activeProfile>
<activeProfile>dev</activeProfile>
<activeProfile>ali</activeProfile>
</activeProfiles>
</settings>
maven中mirror与repositories的关系
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<!-- https://maven.aliyun.com/repository/public/ -->
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
- 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查询
- mirror 匹配 * 匹配所有远程仓库,则其他配置都不起作用,以镜像配置为准
- mirror 匹配 某个repository的id 则搜索此库时执行镜像代理,使用逗号分隔多个远程仓库。
- 配置
*,!具体仓库id
,则除开该仓库都被代理 - 配置
external:*
,则除开本地仓库都被代理,匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库 具体仓库id
是指settings_profile_repo
、pom_profile_repo
、pom_repositories
中的repository
标签中的id
标签的值
例如:
<mirrors>
<!--国内阿里云提供的镜像,非常不错-->
<mirror>
<!--This sends everything else to /public -->
<id>aliyun_nexus</id>
<!--对所有仓库使用该镜像,除了一个名为maven_nexus_201的仓库除外-->
<!--这个名为maven_nexus_201的仓库可以在javamaven项目中配置一个repository-->
<mirrorOf>*,!maven_nexus_201</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
maven项目下的pom.xml配置一个远程仓库
<repositories>
<!-- 192.168.0.201远程仓库 -->
<repository>
<id>maven_nexus_201</id>
<name>maven_nexus_201</name>
<layout>default</layout>
<url>http://192.168.0.201:8081/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
关于pom.xml 的repository 和settings.xml 的repository?
maven获取真正起作用的repository集合流程:首先会获取pom.xml里的repository集合,然后在settings.xml里找mirrors元素,
如果repository的id和mirror的mirrorOf的值相同,则该mirror替代该repository,如果该repository找不到对应的mirror,
则使用其本身,依此可以得到最终起作用的repository集合
settings文件是个什么东西
从settings.xml的文件名就可以看出,它是用来设置maven参数的配置文件。并且,
settings.xml是maven的全局配置文件。而pom.xml文件是所在项目的局部配置。
Settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。
settings.xml文件位置
settings.xml文件一般存在于两个位置:
全局配置: ${M2_HOME}/conf/settings.xml
用户配置: 𝑢𝑠𝑒𝑟.ℎ𝑜𝑚𝑒/.𝑚2/𝑠𝑒𝑡𝑡𝑖𝑛𝑔𝑠.𝑥𝑚𝑙𝑛𝑜𝑡𝑒:
settings和pom之间的关系
settings偏向于全局配置
一般pom的优先高于settings,但是他们之间的信息是相互引用的
Servers是个什么东西
如果需要用户名和密码就需要配置下server,不适合放在pom中,一般定义在settings中,由pom去引用
仓库的下载和部署是在pom.xml文件中的repositories和distributionManagement元素中定义的。
server如何跟repository关联
该 id 与 distributionManagement 中 repository 元素的 id 相匹配
repository/profile 标签
profile可以再settings和pom中配置
repository无法单独在settings中配置,不报错不生效,但是可以配置到profile中
repository和profile的关系
profile会用到repository
repository也可以单独配置