前言
maven 是目前java 常见的一款包管理工具,通过 maven 我们可以很方便的对项目进行编译、打包、部署等操作。
setting.xml 文件是 Maven 的主要配置文件,它包含了 Maven 运行时需要的配置信息。这个文件通常位于~/.m2/目录下。我们也可以手动指定,如下(idea举例):

通过 setting 配置文件,我们可以改变参数项,来控制以上操作的具体行为,接下来我们将具体讲讲 settting 配置文件
结构
setting.xml 整体结构如下:
xml
<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/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
下面是各个元素的含义:
- localRepository:本地仓库的位置,如果没有设置,Maven会使用默认的~/.m2/repository。
- interactiveMode:交互模式的开关,如果设置为true,Maven会在需要输入时提示用户。
- usePluginRegistry:是否使用插件注册表,如果设置为true,Maven会使用~/.m2/plugin-registry.xml文件。
- offline:离线模式的开关,如果设置为true,Maven不会尝试连接网络。
- pluginGroups:插件组列表,Maven会在这些组中搜索插件。
- servers:定义了一些服务器的配置,包括服务器的 ID、用户名和密码。这些服务器通常用于发布(deploy)项目。
- mirrors:镜像配置,可以设置Maven从哪个镜像站点下载依赖。
- proxies:代理配置,可以设置Maven通过哪个代理服务器连接网络。
- profiles:配置文件,可以包含一组特定的设置,如特定的代理服务器、镜像站点等。
- activeProfiles:激活的配置文件列表,Maven会使用这些配置文件中的设置。
值得注意的是,settings.xml文件中的设置可以被pom.xml文件中的设置覆盖。
重点结构说明
localRepository
用于指定本地仓库位置,即 jar 包下载存储的位置(使用时优先从本地仓库加载,没有时再去远程仓库加载)
xml
<localRepository>D:\xxx\mvn\repository</localRepository>
server
一般用于定义远程私服
的认证信息:
xml
<server>
<id>releases</id>
<username>zhangsan</username>
<password>123456</password>
</server>
在这个示例中,我们定义了一个服务器,其ID为releases,用户名为zhangsan,密码为123456。
然后,就可以在pom.xml文件中引用这个服务器。例如,如果你想从这个服务器下载依赖,可以这样配置:
xml
<repositories>
<repository>
<id>releases</id>
<url>http://example.com/repo</url>
</repository>
</repositories>
或者,如果你想把项目部署到这个服务器,你可以这样配置:
xml
<distributionManagement>
<repository>
<id>releases</id>
<url>scp://example.com/path/to/repo</url>
</repository>
</distributionManagement>
在这两个例子中,元素的值与settings.xml文件中的服务器ID相匹配,所以Maven会使用对应的用户名和密码进行身份验证。
注意:出于安全考虑,不建议在settings.xml文件中明文存储密码。你可以使用Maven的密码加密功能来加密密码。
如果定义了多个 repository
,maven 会如何选择?
-
Maven会按照它们在pom.xml文件中的顺序来使用这些仓库。
-
当Maven需要下载一个依赖或插件时,它会首先从第一个仓库尝试下载。如果第一个仓库中没有这个依赖或插件,或者下载失败,那么Maven会尝试从第二个仓库下载,以此类推,直到下载成功或者所有的仓库都尝试过。
mirrors
定义了一些镜像的配置,包括镜像的 ID、名称和 URL,通常用于下载依赖:
xml
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
在这个示例中,我们定义了一个镜像站点,其ID为alimaven,URL为maven.aliyun.com/nexus/conte...
当Maven需要从central仓库下载依赖时,它会检查settings.xml文件中的镜像配置,如果找到了匹配的镜像,它会从镜像站点下载依赖,而不是从原始仓库下载。
元素的值可以是一个仓库ID,也可以是一个仓库ID的列表,用逗号分隔。如果值为*
,表示这个镜像是所有仓库的镜像。
值得注意的是:如果你定义了多个匹配的镜像,Maven会使用第一个匹配的镜像。
通常用于配置公开的、不需要身份验证的镜像站点。这些镜像站点是公开仓库的复制品,用于加快下载速度和提高可用性,然而,有些镜像站点可能需要身份验证,如:
-
镜像站点是私有的,只对特定的用户或组织开放。
-
镜像站点提供了付费的高级服务,需要用户登录才能使用。
这里也借助 server 提供认证信息:
xml
<servers>
<server>
<id>alimaven</id>
<username>myuser</username>
<password>mypassword</password>
</server>
</servers>
Maven会根据mirror
元素的id
查找对应的server
元素,并使用其中的认证信息。
profiles
profiles
元素用于定义一组特定的项目设置,这些设置只有在特定的条件满足时才会生效。
每个profile
元素定义了一组设置,这些设置可以包括目标环境的特定配置、依赖管理、插件配置等
xml
<profiles>
<profile>
<id>development</id>
<repositories>
<repository>
<id>dev-repo</id>
<url>http://dev.example.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>dev-plugin-repo</id>
<url>http://dev.example.com/maven2</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
在这个示例中,我们在development配置文件中定义了一个repositories
元素和一个pluginRepositories
元素。
repositories
元素用于定义项目的依赖仓库。这里,我们定义了一个仓库dev-repo,其URL为dev.example.com/maven2 当Maven处理项目的依赖时,它会从这个仓库下载依赖。pluginRepositories
元素用于定义项目的插件仓库。这里,我们定义了一个插件仓库dev-plugin-repo,其URL为dev.example.com/maven2 当Maven处理项目的插件时,它会从这个仓库下载插件。- 其中,
releases
和snapshots
用于控制Maven是否从这个仓库下载release版本和snapshot版
activeProfiles
结合 pofiles
元素,activeProfiles
主要用于指定默认激活的Profile:
xml
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
即,id = development 的 profile 被激活使用了。