id 标签的作用
不同的功能标签下,都可以配置 id 标签,该 id 值是自定义的,当不同的功能标签下存在同值的 id 标签,那么将这些功能标签视为一组配置。
例,配置 private-repo-1
私服的鉴权信息:
<servers>
<server>
<id>private-repo-1</id>
<username>repouser</username>
<password>repopwd</password>
</server>
</servers>
<repositories>
<repository>
<id>private-repo-1</id>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
</repositories>
id:central
id:central 是一个特殊的 id 值,Maven 的中央仓库的默认ID正是 central。当所有配置的 repositories 中都搜索不到依赖时,就会走 central 从中央仓库中进行拉取。
server
服务器配置,设置 repositories 鉴权信息的地方。当们配置私服时,在此处指定私服的鉴权信息。分为两种配置方式:1.账号密码、2.私钥文件
<servers>
<server>
<id>private-repo-1</id>
<username>repouser</username>
<password>repopwd</password>
</server>
<server>
<id>private-repo-2</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
</servers>
repositories
依赖仓库,下载 jar 依赖的仓库配置。分为本地仓库、远程仓库两种。
搜索规则:
- jar 依赖包下载时从配置的所有 repositories 逐一查找,查询顺序为配置的顺序。当所有的 repositories 都找不到需要的依赖,那么会提示找不到依赖的异常。
- 先检查 local repository,如果 local repository 有则直接返回,否则会向 remote repositories 请求,并缓存到 local repository。
当我们项目里面需要设置第三方的仓库时,建议 <repositories>
的第一个 <repository>
写为 id 为 center 的国内镜像仓库,后面再写第三方的<repository>
。这样优先国内镜像仓库搜索,其次三方仓库搜索,速度要更快(当然也可以配置 mirrorOf 为 central 的镜像)。
<repositories>
<!-- central -->
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
<!-- other repository -->
<!-- ... -->
<repositories>
mirrors
mirror 镜像,重定向 remote repositories 请求地址的配置。它会拦截对 remote repositories 的相关请求,把请求里的 remote repositories url 标签值,重定向到mirror里配置的 url。
例,通过 mirrorOf
标签,配置转发 private-repo-1 私服的请求至 private-repo-2:
<mirrors>
<mirror>
<id>private-repo-2-mirror</id>
<url>https://private-repo-2-mirror/repository/central</url>
<mirrorOf>private-repo-1</mirrorOf>
</mirror>
</mirrors>
<repositories>
<repository>
<id>private-repo-1</id>
<url>https://private-repo-1-mirror/repository/central</url>
</repository>
</repositories>
另一个常见的例子,settings.xml 中修改阿里云镜像仓库:
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
mirrOf 指定的正是 central
,这意味着告诉 Maven 将中央仓库的请求重定向到指定的阿里云镜像。
依赖 deploy 推送配置
全局配置 (settings.xml):
<!-- 全局部署推送配置 -->
<properties>
<!-- 1. 快照 -->
<altReleaseDeploymentRepository>xxx-snapshot-ssMnl0::default::https://packages.aliyun.com/xxx/maven/xxx-snapshot-ssmnl0/</altReleaseDeploymentRepository>
<!-- 2. 发行 -->
<!-- <altSnapshotDeploymentRepository></altSnapshotDeploymentRepository> -->
</properties>
局部配置 (pom.xml):
<distributionManagement>
<repository>
<id>my-repository</id>
<url>https://my.repository.url/repo</url>
</repository>
</distributionManagement>
云效私服配置案例
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>E:\dev\maven\apache-maven-repo</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<servers>
<!-- 1. 云效私服-快照仓库 -->
<server>
<id>xxx-snapshot-ssMnl0</id>
<username>username</username>
<password>password</password>
</server>
<!-- 2. 云效私服-发行仓库 -->
<!--
<server>
<id></id>
<username></username>
<password></password>
</server>
-->
</servers>
<profiles>
<profile>
<id>default</id>
<!-- 全局依赖推送配置 -->
<properties>
<!-- 1. 快照 -->
<altSnapshotDeploymentRepository>
xxx-snapshot-ssMnl0::default::https://packages.aliyun.com/xxx/maven/xxx-snapshot-ssmnl0
</altSnapshotDeploymentRepository>
<!-- 2. 发行 -->
<!-- <altReleaseDeploymentRepository></altReleaseDeploymentRepository> -->
</properties>
<!-- 云效私服仓库 -->
<repositories>
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
<!-- 1. 云效私服-快照仓库 -->
<repository>
<id>xxx-snapshot-ssMnl0</id>
<url>https://packages.aliyun.com/xxx/maven/xxx-snapshot-ssmnl0</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<!-- 2. 云效私服-发行仓库 -->
<!--
<repository>
<id></id>
<url></url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
-->
</repositories>
<!-- 插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>snapshots</id>
<url>https://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>xxx-snapshot-ssMnl0</id>
<url>https://packages.aliyun.com/xxx/maven/xxx-snapshot-ssmnl0</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
<!--
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
-->
</settings>
坑
1. 记得配置 deploy 插件
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
<build>
<plugins>
<!-- ... -->
<!-- deploy -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</build>
2. 注意 settings.xml 命名的文件
[FATAL] Non-parseable settings E:\dev\maven\apache-maven-3.8.8\conf\settings.xml: end tag name </activeProfile> must match start tag name <activeProfiles> from line 32 (position: TEXT seen ...<activeProfiles>\r\n </activeProfile>... @33:19) @ E:\dev\maven\apache-maven-3.8.8\conf\settings.xml, line 33, column 19
当想配置全局推送配置,不想配置 distributionManagement
标签,并存在多个 settings.xml 文件时,即使 idea 中指定了使用的 settings-xxx.xml 文件,也会读取默认配置文件 settings.xml
中的 activeProfiles
相关标签。默认文件改个名: