Maven实战-settings配置文件解析

97 阅读3分钟

settings文件解析

介绍

settings配置文件有多种作用域,全局范围,用户范围。

  • 安装目录conf/settings.xml:是全局范围
  • 用户家目录.m2/settings.xml:是用户范围

建议从安装目录拷贝一份settings到家目录,在用户范围内修改,有以下好处:

  • 便于后续maven升级,不需要把conf/settings.xml拷贝出来,升级后再拷贝进去
  • 影响范围不会影响到其他用户

元素介绍

settings

配置文件根元素

localRepository

本地仓库地址,默认在${user.home}/.m2/repository
可修改为自己指定的路径

interactiveMode

交互模式,默认值 true
如果设置为false,maven会使用合理的默认值去执行

offline

离线模式,默认值false
如果设置为true,maven在构建的时候会尝试连接网络

pluginGroups

插件组

当通过前缀解析插件时,将会搜索这些插件组

示例:

  <pluginGroups>
    <pluginGroup>com.your.plugins</pluginGroup>
  </pluginGroups>

"org.apache.maven.plugins" 和 "org.codehaus.mojo" 会自动添加并查找

proxies

代理模式,如果不能直接访问网络,需要走代理的话,可以在此配置

示例:

  <proxies>
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
  </proxies>
  • id 代理的唯一id
  • active 当前配置的是否激活
  • protocol 代理的协议
  • host 主机
  • port 端口
  • nonProxyHosts 不需要代理的主机,|可以分隔多个,*可以匹配多个字符

可以配置多个代理

servers

配置网络请求的认证信息,通过server.id属性匹配

示例:

<servers>
    <!-- 使用用户名密码的 -->
    <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

镜像列表。有仓库A,如果仓库B有A的全部内容,则可称B是A的镜像仓库。

场景:比如访问中央仓库受限,但是国内某个公司镜像了中央仓库,那么访问国内的就比较快了。

示例:

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

该例中,任何对repositoryId仓库的请求都会转到http://my.repository.com/repo/path镜像地址。

  • mirrorOf 代表谁的镜像。
    可能的配置
    • * 匹配所有远程仓库
    • external:* 匹配所有不在本机上的远程仓库
    • external:http:* 用于初始通过 HTTP 镜像外部存储库的伪存储库
    • rep1,rep2 匹配rep1和rep2
    • *,!rep1 匹配所有,除了rep1

profiles

可以在构建的时候修改POM的一个子集,或添加额外的配置元素。

示例:

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

该示例中,当JDK版本为1.4时,会激活该profile。

在settings中的profile可使用的元素:

repositories, pluginRepositories, properties

激活方式:

  1. 命令行激活

    • -P 激活profile,如mvn clean install -Pprofile1,profile2
  2. settings文件显示激活
    见下一节内容

  3. 属性激活

    <profiles>
        <profile>
          <id>jdk-1.4</id>
    
          <activation>
            <property>
              <name>test</name>
              <value>x</value>
            </property>
          </activation>
    
          ...
        </profile>
    </profiles>
    

    当属性test=x时,该profile会被激活。

  4. 操作系统激活

    <profiles>
        <profile>
          <id>jdk-1.4</id>
    
          <activation>
            <os>
              <name>test</name>
              <family>x</family>
              <arch>y</arch>
              <version>z</version>
            </os>
          </activation>
    
          ...
        </profile>
    </profiles>
    
  5. 文件激活

    <profiles>
        <profile>
          <id>jdk-1.4</id>
    
          <activation>
            <file>
              <missing>test</missing>
              <exists>x</exists>
            </file>
          </activation>
    
          ...
        </profile>
    </profiles>
    
  6. 默认激活

    <profiles>
        <profile>
          <id>jdk-1.4</id>
    
          <activation>
            <activeByDefault>true</activeByDefault>
          </activation>
    
          ...
        </profile>
    </profiles>
    

    默认的激活会被其他激活方式覆盖。

activeProfiles

激活profiles

示例:

  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>

该示例中,alwaysActiveProfileanotherAlwaysActiveProfile会被激活

参考文献

  1. 《Maven实战》-许晓斌