文件结构
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
- localRepository: 配置本地存储库的位置,默认为
${user.home}/.m2/repository
- interactiveMode: 是否与用户开启交互模式,默认为 true
- offline: 离线模式,默认为 false
- pluginGroups: 比如
<pluginGroup>org.eclipse.jetty</pluginGroup>
, 默认有org.apache.maven.plugins and org.codehaus.mojo
。 - servers: 配置私服的用户名和密码
- mirrors: mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
- proxies: 代理配置
- profiles: 配置环境
- activeProfiles: 配置默认激活的环境
1-配置用户名和密码
<!-- 访问私服需要的用户名和密码 -->
<servers>
<server>
<id>repo-releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>repo-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
2-配置profile
下面的私服地址是假的。
<!-- 配置 zero-rdc-repo -->
<profile>
<id>me-repo</id>
<repositories>
<!-- 配置的顺序决定了下载 jar 包的顺序 -->
<!-- 阿里云的 release 版本 -->
<repository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- 私服的 release 版本 -->
<repository>
<id>repo-releases</id>
<url>https://repo.rdc.aliyun.com/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- 私服的 snapshot 版本 -->
<repository>
<id>repo-snapshots</id>
<url>https://repo.rdc.aliyun.com/repository/snapshot/</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<!-- 阿里云插件的 release 版本 -->
<pluginRepository>
<id>central</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
这里的 repositories 如果不配置的话,默认会有一个 Maven 中央仓库的配置,同样 pluginRepositories 中如果没有配置的话,默认也是有一个 Maven 中央仓库的配置。
3-配置 mirror
<!-- 配置拦截 repository 内的 url 进行重定向 -->
<mirrors>
<!-- 将 central,!rdc-releases,!rdc-snapshots 的请求重定向到阿里云的公共 Maven 仓库 -->
<!-- 其它的不重定向到阿里云 -->
<mirror>
<id>Nexus-aliyun</id>
<mirrorOf>central,!rdc-releases,!rdc-snapshots</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
这里的 mirror 类似于重定向操作,改变 repository 的 url 属性。
注意,这里写的是central,!rdc-releases,!rdc-snapshots
而不是*,!rdc-releases,!rdc-snapshots
,是因为我们只想把 Maven 中央仓库的请求重定向到阿里云上,而不是把所有的请求都重定向到阿里云上。Maven 中央仓库仅仅是一个仓库,打开 https://mvnrepository.com/repos发现我们经常使用的https://repo1.maven.org/maven2/
仅仅是众多仓库中的一个,只不过这个是比较大而全的仓库而已。如果我们把所有的请求都重定向到这个仓库,那么就会有依赖找不到。
疑问,求解答
还有一个问题就是当时我配置的是*,!rdc-releases,!rdc-snapshots
,所以有https://repo.spring.io/libs-milestone/
仓库的 jar 包下载不到,那么问题来了,我如果配置的是central,!rdc-releases,!rdc-snapshots
,而 repositories 中也没有配置https://repo.spring.io/libs-milestone/
这个 repository,它是如何找到的呢???