maven settings.xm详解

settings.xml详解

###1. 前言
setting.xml是maven中设置默认本地仓库、中央仓库、镜像仓库、插件仓库、代理的地方,是一个全局配置。先介绍一下基本概念

  • 本地仓库
    即缓存maven项目的各类依赖组件的目录;
  • 中央仓库
    一个公用的库,存放了各类开源的组件,默认maven项目的依赖都会去该仓库下载依赖项的pom.xml和jar包;
  • 镜像仓库
    可以理解为中央仓库的备机,一般当中央仓库不可达时或者不稳定时则配置镜像仓库屏蔽中央仓库。
  • 插件仓库
    类似中央仓库,插件也是jar包,所以本质和中央仓库没有什么区别,只是逻辑上概念不一样。
  • 代理
    就是maven构建过程中需要访问互联网,如果主机不可直达外网,则需要配置代理访问外网。

2. settings.xml的详细介绍

2.1. 一级元素介绍

这里只介绍了我们平常会用的配置,对于<interactiveMode/>、&ltpluginGroups/>、<offline/>、<usePluginRegistry/>没有列出。

<settings>
   <localRepository></localRepository>
   <profiles></profiles>
   <activeProfiles></activeProfiles>
   <mirrors></mirrors>
   <proxies></proxies>
   <servers></servers>
</settings>
  • localRepository
    该配置中用以设置本地仓库的路径,在该路径下缓存中央仓库依赖项。
    <localRepository>/usr/local/maven/repository/</localRepository>
  • profiles
    该项用以配置多组maven的中央仓库、插件仓库以及他们的使用时机。
  • activationProfiles
    profiles中存在多组配置,该项用以配置自动生效的配置项。
<activeProfiles>
  <activeProfile>id_of_profile</activeProfile>
</activeProfiles>
  • mirrors
    当某个中央仓库不稳定或者不可达时,在该配置项中配置
  • proxies
    配置maven使用的代理
  • servers
    配置仓库、部署服务器的密令参数,有些仓库需要密令,另外项目部署到多台服务器时也需要密令,这里用以配置服务器密令。

2.2. profiles

<profiles>
    <profile>
	  <id>test</id>
		<!-- 配置激活的条件设置 -->
		<activation></activation>
		<!-- 远程仓库配置 -->
		<repositories></repositories>
		<!-- 远程插件仓库配置 -->
		<pluginRepository></pluginRepository>
    </profile>
</profiles>
  • id
    配置的唯一标志
  • activation
    配置激活的条件
<activation>
    <activeByDefault>true</activeByDefault>
	<file>
	    <!-- 文件存在则激活-->
	    <exits>file_name_2</exits>
		<!-- 文件不存在则激活-->
		<mssing>file_name_2</mssing>
	 </file>
	<!-- jdk1.8则激活配置-->
	<jdk>1.8</jdk>
	<!-- 操作系统的激活条件 -->
	<os>
	    <!-- 系统架构 -->
	    <arch>x86-64</arch>
		<!-- 族系 -->
		<family>Linux</family>
		<!-- 名称 -->
		<name>Centos</name>
		<!-- 版本 -->
		<version>7.0</version>
	</os>
	<!-- 定义变量激活,如果存在变量hello且值为world,则激活,另外如果value为空,则只要存在变量hello则激活 -->
	<property>
	   <name>hello</name>
	   <value>world</value>
	</property>
</activation>
  • repositories
    配置仓库
<repositories>
    <repository>
	    <!-- 仓库的id和名,都是自定义的 -->
	  <id>central</id>
		<name>the central repo</name>
		<!-- 仓库的url -->
		<url>https://repo.maven.apache.org/maven2/</url>
		<!-- 指定更新该库中release版本依赖的策略-->
		<releases>
		    <!-- 是否开启更新-->
		    <enable>true</enable>
			<!-- 更新策略,always, daily, interval:5,never; interval的单位是minute,never表示的是如果本地库不存在也不会去下载,默认为daily -->
			<updatePolicy>always</updatePolicy>
      <!-- 如果校验插件的校验和失败采取的动作 -->
			<checksumPolicy>warn</checksumPolicy>
		</releases>
    <!-- snapshots版本的依赖项更新策略,和releases类似-->
		<snapshots>
		  <enabled>false</enabled>
		  <updatePolicy>daily</updatePolicy>
      <checksumPolicy>error</checksumPolicy>
		</snapshots>
    <!-- maven仓库的布局,即目录的组织格式,目前都已统一,都为default -->
    <layout>default</layout>
	</repository>
</repositories>
  • pluginRepositories
    配置插件仓库

      <pluginRepository>
          <id>central</id>
          <name>central plugin repository</name>
          <url>https://repo.maven.apache.org/maven2/</url>
          <releases>
            <enabled>true</enabled>
            <!--半天更新一次 -->
            <updatePolicy>interval:720</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
            <layout>default</layout>
          </releases>
          <snapshots></snapshots>
      </pluginRepository>
    

    2.3. mirrors

    配置仓库镜像

       <mirrors>
         <mirror>
           <id>mirror_test</id>
           <name> mirror of central </name>
           <!-- 镜像的url-->
           <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
           <!-- 被镜像的库的id -->
           <mirrorOf>central</mirrorOf>
         </mirror>
       </mirrors
    

    2.4. proxies

    配置访问网络的代理, 内网请求也会被代理

    <proxies>
      <proxy>
        <id>web-proxy</id>
        <!--http://user@host:port/-->
        <protocol>http</protocol>
        <username>rtx</username>
        <password>passwd</password>
        <host>localhost</host>
        <port>8080</port>
        <!-- 无需代理的域名,主机等-->
        <nonProxyHosts>*.oa.com | localhost</nonProxyHosts>
        <!-- 代理是否生效,默认是false的 -->
        <active>true</active>
      </proxy>
    </proxies>
    

    2.5. servers

    配置服务器登录命令,包括仓库和部署服务器

    <servers>
      <server>
        <!-- 服务器的id, 可能是仓库,可能是部署服务器-->
        <id>central</id>
        <username>user</username>
        <password>passwd</password>
        <!-- 如果服务器认证是采取密令认证,需要提供rsa私钥文件的全路径 -->
        <privateKey>~/user/.ssh/id_rsa</privateKey>
        <!-- 私钥文件一般加密存储,需要解密密码 -->
        <phrase>prikey_password</phrase>
        <!-- 部署服务时会创建文件,指定文件的权限 -->
        <filePermission>744</filePermission>
        <!-- 部署服务时会创建目录,指定文件的权限 -->
        <directoryPermission>744</directoryPermission>
        <!-- 传输层的一些控制,超时之类 -->
        <configuration></configuration>
      </server>
    </servers>
    

    3. 拓展

    其实所有的settings.xml中的配置项都可以在其xsd(XML shcema definition)文件中找到对应的解释。下图展示了该文件中的部分内容,如果大家想详细了解一个maven配置文件的格式,可以查看对应的xsd文件了解。
    在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章