Maven的settings.xml文件,mirror标签和profile里的repository标签都配置了仓库,如果激活这个profile,下载jar包的时候用哪一个?这两个配置有什么差别?
通过 maven 的 profile 功能,可以个性化启用不同的配置。例如:在家使用 Maven 做个人项目开发的时候,使用阿里云仓库,添加一个 id 为 aliyun 的配置:
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun-repo</id>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-repo-plugin</id>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
在公司使用 Maven 做企业项目开发的时候,使用内部搭建的私服(这里使用 maven.net.cn 代替私服,仅做演示,实际使用时换成自己公司内部的私服即可),添加一个 id 为 netcn(这里是随便取的一个名称) 的配置:
<profile>
<id>netcn</id>
<repositories>
<repository>
<id>netcn-repo</id>
<url>http://maven.net.cn/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>netcn-repo-plugin</id>
<url>http://maven.net.cn/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
在执行 Maven 命令时,通过 -Pname 参数可以开启指定的配置(注意P和后面的配置名,没有空格)。例如以下命令,在 clean 操作时,启用 aliyun 配置(最好是自己运行来观察,不要光是看,不动手):
mvn clean -Paliyun
在启用不同的配置时,Maven 会根据配置中的仓库地址来下载指定的插件(clean 就是一个插件),请运行观察。除了在 settings.xml 配置文件中指定以外,还可以在 pom.xml 中指定,效果是一样的。
镜像很好理解,在互联网上经常可以看到一个名词叫做“镜像站点”,Maven 中的镜像也是同一个原理。如果仓库 A 可以提供仓库 B 存储的所有内容,那么可以认为 A 是 B 的一个镜像。例如:阿里云仓库能够提供中央仓库的所有内容,那么阿里云仓库就是中央仓库的一个镜像。
理解这一点,再回到 settings 配置文件中,就容易理解镜像的含义了。mirrorOf 用来配置拦截规则,<mirrorOf>central</mirrorOf> 的意 思是拦截到所有访问 central(中央仓库)的请求,都从该镜像指定的地址返回。
如果你使用的是 <mirrorOf>central</mirrorOf> 这样的拦截规则,表示的是拦截中央仓库的请求。mirrorOf 中的内容指的是仓库ID,但是可以使用通配符和逗号分隔符。除了配置为 central 之外,还有其他几种配置方式:
1、<mirrorOf>*</mirrorOf>:表示匹配所有,不管你配置了多少个 repository(仓库)。
2、<mirrorOf>repo1,repo2</mirrorOf>:表示匹配 repo1、repo2 这两个仓库的请求。
3、<mirrorOf>*,!repo1</mirrorOf>:表示匹配所有,但是 repo1 这个仓库除外。
简单总结就是:<mirrorOf>哪些仓库ID要被镜像</mirrorOf>。形象一点,可以参考下图说明:
看看maven官网不就知道了,不过我也不太清楚,嘿嘿