maven - settings.xml文件详解

Settings.xml配置文件详解

maven默认的settings.xml是一个包含注释和例子的模板,可以快速的修改settings.xml文件

maven安装后不会在用户目录下自动生成settings.xml,一般是将/maven/conf下的settings.xml文件拷贝过去进行相应的修

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

  <!-- 本地仓库的路径,默认值是~/.m2/repository -->  <localRepository>/path/to/local/repo</localRepository>
<!-- maven是否需要和用户交互,默认值是true --> <interactiveMode>true</interactiveMode>

 <!-- 是否使用plugin-registry.xml文件来管理插件版本 -->
 <usePluginRegistry>false</usePluginRegistry>
 
<!--
    maven是否需要在离线模式下运行
    true:一般用于构建服务器不能连接远程仓库的时候,基于网络或者安全考虑
    默认为false -->
<offline>false</offline>
<!--
    当插件的组织ID(groupId)没有提供的时候,供搜寻插件组织的列表
    包含<pluginGroup>元素列表,每一个子元素包含一个组织ID(groupId)
    默认包含org.apache.maven.plugins -->  
<pluginGroups> <!-- plugin的组织ID(groupId) --> <pluginGroup>com.your.plugins</pluginGroup> </pluginGroups> <!-- 配置多种代理,包含<proxy>元素 --> <proxies> <!-- 配置代理时需要的信息 --> <proxy>
    <!-- 代理唯一id,用于区分不同的代理 --> <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> <!-- 配置服务端的设置,包含server元素--> <servers> <!-- 包含配置服务器是需要的信息 --> <server>
    <!-- server的id,与distributionManagement中repository的id相匹配 -->  <id>deploymentRepo</id>
    <!-- 鉴权用户名,表示服务器认证需要的用户名/密码 -->  <username>repouser</username>
    <!-- 鉴权密码 --> <password>repopwd</password>
    <!-- 鉴权时使用的私钥位置,私钥位置和私钥密码指定一个私钥的路径(默认是/home/hudson/.ssh/id_dsa) -->  <privateKey>/path/to/private/key</privateKey>
    <!-- 鉴权时使用的私钥密码 -->  <passphrase>optional; leave empty if not used.</passphrase>
    <!--
      文件被创建时的权限,如果在部署的时候会创建一个仓库文件或者目录,这时需要使用权限
      该元素的合法值是一个三位数字,对应unix文件系统的权限 -->
    <filePermissions>664</filePermissions>
    <!-- 目录创建时的权限 --> 
    <directoryPermissions>775</directoryPermissions>
    <!-- 传输层的额外配置项 -->

    <configuration></configuration> </server>
</servers> <!-- 下载的镜像列表 --> <mirrors> <!-- 镜像信息 --> <mirror>
    <!-- 镜像唯一标识 -->  <id>mirrorId</id>
    <!-- -->  <mirrorOf>repositoryId</mirrorOf>
    <!-- 镜像名称 -->  <name>Human Readable Name for this Mirror.</name>
    <!-- 镜像URL -->  <url>http://my.repository.com/repo/path</url> </mirror>
</mirrors> <!--
    根据环境参数来调整构建配置的列表
    settings.xml中的profiles是pom.xml中profiles的裁剪版
    包含<id>,<activation>,<repository>,<pluginRepository>,<properties>元素
   settings.xml中的profiles只包含这几个元素是因为settings.xml文件只关心构建系统的整体而非单独的项目对象模型设置
    如果一个profile被激活,它的值会覆盖任何其定义在pom.xml或profile.xml中相同id的profile  -->
<profiles> <!-- 根据环境参数调整构建配置 --> <profile>
    <!-- 该配置的唯一标识 --> <id>jdk-1.4</id>
    <!--
       自动触发profile的逻辑条件
       和pom.xml中的profile一样,在特定的环境中自动使用某些特定的值
       这些特定的值通过activation指定 --> 
    <activation>
     <!-- profile默认是否激活,默认false -->
     <activeByDefault>false</activeByDefault>
     <!-- activation有一个java版本检测,如果检测的版本与当前配置一致则profile被激活 --> <jdk>1.4</jdk>
     <!-- 当匹配的操作系统属性被检测到时,profile被激活 -->
     <os>
       <!-- 操作系统名称 -->
       <name>Windows XP</name>
       <!-- 操作系统所属的家族 -->
       <family>Windows</family>
       <!-- 操作系统体系结构 -->
       <arch>x86</arch>
       <!-- 操作系统版本 -->
       <version>5.1.2600<version>
     </os>
     <!-- 属性检测,如果拥有对应的名称和值,profile将会被激活 -->
     <property>
       <!-- 激活profile的属性名称 -->
       <name>mavenVersion</name>
       <!-- 激活profile的值 -->
       <value>2.0.3</value>  
     </property>
     <!-- 通过检测文件是否存在激活profile -->  
     <file>
       <!-- 文件存在则激活profile -->  
       <exists>/usr/local/min/workspace</exists>
       <!-- 文件不存在则激活profile -->  
       <missing>/usr/local/min/workspace</missing>   
     </file>         
  </activation>     
    <!-- 远程仓库列表,是maven用来填充构建系统本地仓库所使用的一组远程项目 --> <repositories>
     <!-- 远程仓库 --> <repository>
      <!-- 远程仓库唯一标识 --> <id>jdk14</id>
      <!-- 远程仓库名称 --> <name>Repository for JDK 1.4 builds</name>
      <!-- 远程仓库url --> <url>http://www.myhost.com/maven/jdk14</url>
      <!-- maven 2为仓库提供的默认布局,maven 1.x有另一个不同的布局,通过default/legacy指定 --> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories>
    
    <!-- 插件远程仓库 -->
    <pluginRepositories>
      <pluginRepository>

      <pluginRepository>
    </pluginRepositories>
    <!--
       对应profile的扩展属性列表,和ant中的属性一样,可以用来存放值,这些值可以在pom的任何地方使用${x}访问,x指代属性名称
       <env.x>:返回shell环境变量 
       <project.x>:指代pom对应的元素值
       <settings.x>:指代settings.xml中对应的元素值
       <x>:在<properties/>中或外部文件中使用${x}形式使用      
    -->
    <properties>
      <var>value</var>
    </properties>
   </profile>
 
  </profiles>
  <!-- 手动激活的profile列表 -->
  <activeProfiles>
    <!-- 如果有匹配的profile则pom.xml或者profile.xml中对应id的profile被激活 -->
    <activeProfile>value</activeProfile>
  </activeProfiles>  
</
settings>

 实际应用中,经常使用到的是<localRepository>,<servers>,<mirrors>,<profiles>几个节点

<localRepository>:配置本地仓库地址

<servers>:服务端配置,多用于鉴权操作

<mirrors>:镜像配置,如使用阿里云镜像

<profiles>:系统构建配置,涉及整个系统的构建,可以让maven自动适应外部环境的变化

主要元素有:激活<activation>,仓库<repositories>,插件仓库<pluginRepositories>,属性<properties>

一个项目可以设置多个profile,也可以在同一时间设置多个profile被激活

自动激活条件主要有:操作系统相关参数的判断,JDK版本的检查,属性值的检查,文件检查

如果settings.xml中的profile被激活,则其值会覆盖pom.xml或profile.xml中同等id的profile

猜你喜欢

转载自www.cnblogs.com/min-code/p/10934735.html