相信大家对于maven是不陌生的,在项目中需要经常用到,这次就记录一下在使用maven中, maven 的 settings.xml 文件配置的一些节点是什么意思。希望可以对你所有帮助!
1、settings.xml 格式
有些可配置也可不配置。
2、localRepository 配置
localRepository配置项用来配置maven的本地仓库地址,简单说就是用来存储通过maven下载下来的各种jar包。如果我们的项目pom.xml文件依赖了某个jar包,默认会先从我们本地仓库去查找,查不到的请求下,再去请求外部仓库。
默认值为:${user.home}/.m2/repository
<localRepository>D:\repository</localRepository>
3、servers 配置
远程仓库的身份验证配置,可以配置多个 server 身份。
<servers>
<!--
<server>
<id>serverId</id>
<privateKey>/private/key</privateKey>
<passphrase>XXXX</passphrase>
</server>
-->
<server>
<id>maven-central</id>
<username>account</username>
<password>pwd</password>
</server>
</servers>
id:对应 repository 或 mirror 的id,用来指定该配置项是针对哪个仓库的,必填。
username: 账号
password:密码
username 和 password 是成对出现
privateKey:访问仓库的私钥
passphrase:访问仓库的口令,可选
privateKey 和 passphrase 是成对出现,
和以上账号密码二选一即可。
4、mirrors 配置
远程仓库的镜像站点配置,镜像站点为首选。可以基于以下情况来考虑使用mirrors镜像配置。
1、分担远程仓库的访问压力,同时又不希望修改 repoistory 仓库的配置。
2、用自己公司的内部仓库替换特定的仓库,比如替换central仓库(中央仓库)。
<mirrors>
<mirror>
<id>alimaven-central</id>
<name>aliyun maven</name>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>maven-central</id>
<name>zj nexus</name>
<mirrorOf>*,!aliyun,!spring-cloud</mirrorOf>
<url>http://ip:18081/repository/maven-public/</url>
</mirror>
</mirrors>
id:镜像仓库的id,要保证唯一,不能和 mirrors 和 repositories 下面的id相同,因为镜像仓库本质上也是仓库,下载jar包时,会根据此 id 去 servers 结点下面查找 server 节点配置的认证信息。
name:镜像仓库的名称,方便用户识别该仓库。
mirrorOf:需要做镜像的仓库的id值,也就是取 repository 的id值,
<mirrorOf>central</mirrorOf>表示当前这个镜像将用于代替默认的中央仓库。
url:镜像仓库的地址。
5、profiles 配置
配置文件列表,可以通过多种方式激活。
profiles 用来配置一系列的配置信息,通过指定其激活条件来激活,可以配置多个 profile。
id:唯一
activation:指定激活条件
activeByDefault:没有其它的 profile 为激活状态时,该 profile 默认激活。
repositories: 配置远程仓库信息。
pluginRepositories:配置插件仓库。
<profile>
<id>jdk8</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<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>
5.1、repositories 配置
repositories 配置在 profiles 中的 profile 节点下,是用来配置远程仓库信息的。
id:远程仓库唯一标识符,要唯一
name:描述
url:远程仓库的url地址
layout:定位和排序构件的仓库布局类型
1、default(默认)
2、legacy(遗留)
releases:发布版本,true则表示下载的构件是releases版本的构件,禁止从公共仓库下载 snapshot构件是推荐的做法,这些构件不稳定,且不受你控制。
snapshots: 快照版本,false是表示不要下载snapshots版本的构件。
pluginRepositories 同理 repositories ,只不过是配置插件仓库信息的。配置节点也和 repositories 一样,都是配置仓库信息的。
<profile>
<id>maven-central</id>
<repositories>
<repository>
<id>maven-cen</id>
<url>http://ip:port/repository/maven-public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-central</id>
<url>http://ip:port/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
6、activeProfiles 配置
activeProfiles 配置项用来指定哪些 profile 生效,作用激活 profiles 下的 profile 。
activeProfile 对应的值就是 profile 的 id 。
<activeProfiles>
<activeProfile>maven-central</activeProfile>
</activeProfiles>
7、案例
<?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配置项用来配置maven的本地仓库地址,简单说就是用来存储通过maven下载下来的各种jar包。如果我们的项目pom.xml文件依赖了某个jar包,默认会先从我们本地仓库去查找,查不到的请求下,再去请求外部仓库。-->
<localRepository>D:\repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!--
配置各个远程仓库的认证信息
id:对应 repository 或 mirror 的id,用来指定该配置项是针对哪个仓库的,必填项
username: 账号
password:密码
username 和 password 是成对出现
privateKey:访问仓库的私钥
passphrase:访问仓库的口令,可选
privateKey 和 passphrase 是成对出现, 和以上账号密码二选一即可。
<server>
<id>serverId</id>
<privateKey>/private/key</privateKey>
<passphrase>XXXX</passphrase>
</server>
-->
<server>
<id>maven-central</id>
<username>account</username>
<password>pwd</password>
</server>
</servers>
<mirrors>
<!--
mirrors配置项主要是用来为远程仓库配置镜像站点。
id,镜像仓库的id,要保证唯一,不能和 mirrors 和 repositories 下面的id相同,因为镜像仓库本质上也是仓库,下载jar包时,会根据此 id 去 servers 节点点下面查找server结点配置的认证信息
name,镜像仓库的名称,方便用户识别该仓库
mirrorOf,需要做镜像的仓库的id值,也就是取 repository 的id值, <mirrorOf>central</mirrorOf>表示这个镜像将用于代替默认的中央仓库
url,镜像仓库的地址
-->
<mirror>
<id>alimaven-central</id>
<name>aliyun maven</name>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>maven-central</id>
<name>zj nexus</name>
<mirrorOf>*,!aliyun,!spring-cloud</mirrorOf>
<url>http://ip:18081/repository/maven-public/</url>
</mirror>
</mirrors>
<!--
profiles 用来配置一系列的配置信息,通过指定其激活条件来激活,可以配置多个 profile
id:唯一
activation:指定激活条件
activeByDefault:没有其它的 profile 为激活状态时,该 profile 默认激活。
repositories: 配置远程仓库信息。
pluginRepositories:配置插件仓库。
-->
<profiles>
<profile>
<id>jdk8</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<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>
<profile>
<id>maven-central</id>
<!--
repositories 用来配置maven的远程仓库信息。
id:远程仓库唯一标识符,要唯一
name:描述
url:远程仓库的url地址
layout:定位和排序构件的仓库布局类型
1、default(默认)
2、legacy(遗留)
releases:发布版本,true则表示下载的构件是releases版本的构件,禁止从公共仓库下载snapshot构件是推荐的做法,这些构件不稳定,且不受你控制。
snapshots: 快照版本,false是表示不要下载snapshots版本的构件。
-->
<repositories>
<repository>
<id>maven-cen</id>
<url>http://ip:port/repository/maven-public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<!--
pluginRepositories 配置插件仓库。
pluginRepository 下的节点与 repository 下的节点一样。都是配置仓库的
-->
<pluginRepositories>
<pluginRepository>
<id>maven-central</id>
<url>http://ip:port/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--
activeProfiles 配置项用来指定哪些 profile 生效,
activeProfile 对应的值就是 profile 的id
-->
<activeProfiles>
<activeProfile>maven-central</activeProfile>
</activeProfiles>
</settings>
注意:这是个案例,需要根据自己项目的实际情况调整配置。