美文网首页
Maven系列--settings.xml文件解读

Maven系列--settings.xml文件解读

作者: 爱恨_交加 | 来源:发表于2021-04-27 18:37 被阅读0次

概述

settings.xml文件里的settings元素包含用于定义值的元素,这些值以各种方式配置Maven的执行,与pom.xml类似,但不应该捆绑到任何特定的项目,或分发给受众。

可以把settings.xml文件放在以下两个位置:
全局配置: ${maven.home}/conf/settings.xml
用户配置: ${user.home}/.m2/settings.xml
PS:

注意:
1、PS:全局配置优先于用户配置
2、在settings.xml中指定${maven.home}、${user.home}变量是无效的。
3、一般情况下,我们不会使用这两个位置,而是在IDEA或者Eclipse等编辑器上指定settings.xml的位置。如下图所示:

settings.xml位置指定_IDEA社区2021版
4、也可以在执行命令行(CLI)时指定使用哪个位置的settings.xml文件(基本不用这种方式,可忽略):
覆盖用户配置: -s /path/to/user/settings.xml
覆盖全局配置: -gs /path/to/global/settings.xml

顶级元素<settings>下的直接子元素一览(欲知详情,请继续往下阅读):

    <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>

详情

<?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
   | 用于存储artifacts的本地仓库路径。
   | Default: ${user.home}/.m2/repository。windows系统下是C盘路径,一般都会指定到其它盘符。
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!-- interactiveMode
   | 表示maven是否需要和用户交互以获得输入
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | 确定maven在执行构建时是否应该尝试连接到网络。这将对artifact下载、artifact部署和其他方面产生影响。
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   |当通过前缀解析插件时(没有显式提供groupId),Maven会从该列表中搜寻。
   |默认情况下,该列表包含了 org.apache.maven.plugins 和 org.codehaus.mojo。
   |如调用命令"mvn prefix:goal", Maven将自动添加 org.apache.maven.plugins和 org.codehaus.mojo
-->
  <pluginGroups>
    <!-- pluginGroup
     | 指定一个用于插件查找的groupId
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | 本机器连接到网络的可用代理列表
   | 除非另有指定(由系统属性或命令行开关),否则将使用列表中第一个标记为活动的代理规范。
   |-->
  <proxies>
    <!-- proxy
     | 一个用于连接到网络的代理的规范。
     |
     | id 代理的唯一标识符(可选),用来区分不同的代理元素
     | active  是否激活当前代理。true|false
     | == 协议://主机名:端口 ==
     | protocol 代理的协议
     | host 代理的主机名
     | port 代理的端口
     | nonProxyHosts 不该被代理的主机名列表,使用竖线分隔符分割
     | == 用户名和密码表示代理服务器认证的登录名和密码 ==
     | username 代理的用户名
     | password代理的密码
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
      <username>proxyuser</username>
      <password>proxypass</password>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | 一般,仓库的下载和部署是在pom.xml文件中的repositories和distributionManagement元素中定义的。
   | 然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在settings.xml中。
   |
   | 这是一个身份验证配置文件列表,由系统中使用的服务器id进行键控(即要与系统中使用的服务器id一致)。
   | 当maven必须建立到远程服务器的连接时,可以使用身份验证配置文件。
   |-->
  <servers>
    <!-- server
     | 指定连接到特定服务器时使用的身份验证信息,该信息由系统内的唯一名称标识(通过下面的'id'属性引用)。
     | NOTE: 你应该同时指定username/password或者privateKey/passphrase,这是成对出现的
     |
     | id server的id(注意不是用户登陆的id),该id与POM文件中distributionManagement中repository元素的id相匹配。
     | username 用户名
     | username 密码
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>

    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | 用于从远程仓库下载artifacts的镜像列表
   | 工作方式是这样: POM可以声明一个用于解析特定artifacts存储库,
   | 然而,该存储库有时可能会出现流量过大的问题,因此需要进行镜像到这些地方(即通过镜像加速下载)
   |
   | 该存储库定义将有一个惟一的id,因此我们可以为该存储库创建一个镜像引用,作为备用下载站点使用。镜像站点将成为该存储库的首选服务器。
   |-->
  <mirrors>
    <!-- mirror
     | 指定要使用的存储库镜像站点,而不是给定的存储库

     | id 用于继承和直接查找目的,并且在一组镜像中必须是唯一的 
     | mirrorOf 被镜像的存储库的服务ID
     | name 可选。对当前镜像的描述
     | url 镜像存储库地址
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>

  <!-- profiles
   | 作用:根据环境参数来调整构建配置的列表。
   | settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
   | 它包含了id、activation、repositories、pluginRepositories和 properties元素。(其实还有个settings元素)
   | 这里的profile元素只关心构建系统这个整体,而非单独的项目对象模型设置
   | 这里的profile元素旨在提供特定于本地机器的路径和存储库位置,以允许在本地环境中进行构建
   | 如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。
   |
   | 可以通过多种方式激活profile,并且可以在构建过程中修改profile
   | 激活profile的方式:
   |        1、通过指定settings.xml中<activeProfiles>的<activeProfile>的值(值:profile的唯一id)
   |        2、依赖于settings.xml中<profiles>的<profile>的<activation>中的值(触发条件)
   |        3、可以通过命令行指定需要激活的profile列表
   |
   | NOTE: 对于settings.xml中定义的profiles, 只能指定工件存储库、插件存储库和自由形式的属性作为POM中插件的配置变量
   |-->
  <profiles>
    <!-- profile
     | id profile的唯一标识
     | activation 自动触发profile的条件逻辑
     | properties  扩展属性列表
     | repositories  远程仓库列表
     | pluginRepositories 插件仓库列表
     |
     | 激活profile的样例:通过JDK版本去激活,并提供JDK-specific repo
    <profile>
      <id>jdk-1.4</id>
      <activation>
        <jdk>1.4</jdk>
      </activation>
      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | 当系统属性target-env='dev',激活profile时,从而提供Tomcat的路径变量tomcatPath
     | 
     | 假设插件的配置是这样的:
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | 所有构建都处于活动状态的profile列表
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

相关文章

网友评论

      本文标题:Maven系列--settings.xml文件解读

      本文链接:https://www.haomeiwen.com/subject/uwdprltx.html