“Maven:settings.xml详解”的版本间差异

来自Wikioe
跳到导航 跳到搜索
 
(未显示同一用户的22个中间版本)
第7行: 第7行:


settings.xml 文件一般存在于两个位置:
settings.xml 文件一般存在于两个位置:
# 全局配置:${M2_HOME}/conf/settings.xml
# 全局配置:<span style="color: blue">'''${M2_HOME}/conf/settings.xml'''</span>
# 用户配置:${user.home}/.m2/settings.xml
# 用户配置:<span style="color: blue">'''${user.home}/.m2/settings.xml</span>
 
'''
  全局配置一旦更改,所有的用户都会受到影响,而且如果 maven 进行升级,所有的配置都会被清除,所以要提前复制和备份 ${M2_HOME}/conf/settings.xml 文件,一般情况下不推荐配置全局的 settings.xml。
  全局配置一旦更改,所有的用户都会受到影响,而且如果 maven 进行升级,所有的配置都会被清除,所以要提前复制和备份 ${M2_HOME}/conf/settings.xml 文件,一般情况下不推荐配置全局的 settings.xml。


第17行: 第17行:
* 如果这些文件同时存在,在应用配置时,会合并它们的内容;如果有重复的配置,优先级高的配置会覆盖优先级低的。
* 如果这些文件同时存在,在应用配置时,会合并它们的内容;如果有重复的配置,优先级高的配置会覆盖优先级低的。


== settings.xml概述 ==
== 概述 ==
settings.xml:
settings.xml:<span class="darken mw-customtoggle-settings.xml"><font color="blue">'''(展开/隐藏)'''</font></span>
<syntaxhighlight lang="xml" highlight="">
<syntaxhighlight lang="xml" highlight="" class="darken mw-collapsible mw-collapsed" id="mw-customcollapsible-settings.xml">
<?xml version="1.0" encoding="UTF-8"?>   
<?xml version="1.0" encoding="UTF-8"?>   
      
      
第323行: 第323行:
</syntaxhighlight>
</syntaxhighlight>


== settings.xml详述 ==
== 详述 ==
下面列举了 settings.xml 中的顶级元素:
下面列举了 settings.xml 中的顶级元素:
: <syntaxhighlight lang="xml" highlight="">
: <syntaxhighlight lang="xml" highlight="5,11-13">
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
第342行: 第342行:
</settings>
</settings>
</syntaxhighlight>
</syntaxhighlight>
*其中最常用的是:
** <LocalRepository>
** <servers>
** <mirrors>
** <profiles>


=== <LocalRepository> ===
=== <LocalRepository> ===
第655行: 第661行:
==== <repositories> ====
==== <repositories> ====
----
----
如果配置了多个 repository 会用哪个呢?
答案是按顺序使用:如果第一个可用,就用第一个,如果不可用,就依次往下找。
'''作用''':远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。
'''作用''':远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。


第737行: 第747行:
</activeProfiles>
</activeProfiles>
</syntaxhighlight>
</syntaxhighlight>
== 示例 ==
<big>'''见:[[Maven:settings.xml示例]]'''</big>


== FAQ ==
== FAQ ==
=== 关于:profile ===
=== 关于:profile ===
----
 
==== “profile 的位置” ====
==== “profile 的位置” ====
  profile 可以在 settings.xml 中配置,也可以在 pom.xml 文件中配置。
  profile 可以在 settings.xml 中配置,也可以在 pom.xml 文件中配置。
第793行: 第806行:
=== Mirror 与 Repository ===
=== Mirror 与 Repository ===
----
----
Repository(仓库):repository 里存放的都是各种 jar 包和 maven 插件。
'''Repository(仓库)''':repository 里存放的都是各种 jar 包和 maven 插件。
* Maven 仓库主要有 2 种:
* Maven 仓库主要有 2 种:
*# local repository:本地仓库,存放在本地磁盘的一个文件夹。(例如,windows 上默认是:C:\Users\{用户名}\.m2\repository 目录)
*# local repository:本地仓库,存放在本地磁盘的一个文件夹。(例如,windows 上默认是:C:\Users\{用户名}\.m2\repository 目录)
第806行: 第819行:




Mirror(镜像):mirror 相当于一个拦截器,它会拦截 maven 对 remote repository 的相关请求,把请求里的 remote repository 地址,重定向到 mirror 里配置的地址。
'''Mirror(镜像)''':mirror 相当于一个拦截器,它会拦截 maven 对 remote repository 的相关请求,把请求里的 remote repository 地址,重定向到 mirror 里配置的地址。
* 其实,mirror 表示的是'''两个 Repository 之间的关系'''。
* 其实,mirror 表示的是'''两个 Repository 之间的关系'''。
* 如果某个 Repository 配置了多个 Mirror,(当前面的 Mirror 不可用时)会按顺序尝试。
* 如果某个 Repository 配置了多个 Mirror,(当前面的 Mirror 不可用时)会按顺序尝试。
第815行: 第828行:
   
   
  所以,网上很多资料,都只添加一个 Mirror,用于重定向默认仓库 central 的地址。
  所以,网上很多资料,都只添加一个 Mirror,用于重定向默认仓库 central 的地址。
=== '''settings.xml 示例''' ===
----
阿里云Maven仓库地址:[https://developer.aliyun.com/mvn/guide https://developer.aliyun.com/mvn/guide]
settings.xml 中最常用的是:
# <LocalRepository>
# <servers>
# <mirrors>
# <profiles>
示例:
: <syntaxhighlight lang="xml" highlight="">
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\Documents\Repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<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>
<servers>
<!-- server
<server>
  <id>deploymentRepo</id>
  <username>repouser</username>
  <password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
  <id>siteServer</id>
  <privateKey>/path/to/private/key</privateKey>
  <passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
<mirrors>
<!-- <mirror>
<id>alimaven</id>
<name>aliyun-maven</name>
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/central</url>
</mirror> -->
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>aliyun</id>
<name>Aliyun Repository</name>
<!-- <url>https://maven.aliyun.com/nexus/content/groups/public</url> -->
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<name>Aliyun Repository</name>
<!-- <url>https://maven.aliyun.com/nexus/content/groups/public</url> -->
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2/</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
</settings>
</syntaxhighlight>
如上,配置了:
# 本地仓库路径为:“D:\Documents\Repository”;
# 一个 profile(dev),包含两个 repository(aliyun、central)、两个 pluginRepository(aliyun、central);
#* aliyun 仓库不支持索引;【“目前还未支持生成和下载 maven indexer 文件”,参见:https://developer.aliyun.com/article/621196 】
# 两个个 mirror:(repo1、repo2),用作 repository(central)的镜像;
#* repo1、repo2 镜像可以更新索引,但不稳定且速度慢;
# 在 activeProfiles 中启用了 profile(dev);
体现在 IDE 中:
: [[File:Eclipse中的Maven仓库.png|left|thumb|600px|Eclipse中的Maven仓库]] [[File:Idea中的Maven仓库.png|none|thumb|600px|Idea中的Maven仓库]]
=== Idea 无法更新仓库索引 ===
----
配置了 setting.xml 之后,更新仓库索引失败,有两种情况:
# 提示信息为:“'''Cannot connect to the Maven process. Try again later. If the problem presists, check the Maven Importing JDK settings and restart Intelli IDEA'''”。
#: 原因:IDEA 连接到该仓库的'''网络问题'''。
#: 解决:
## 为该仓库配置国内可用的镜像;(如上一节的 repo1、repo2,不稳定但能用)
## 为 Idea 中 Maven->importing(VM options for importer)设置代理:“-DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=10800”;
##* 如果使用 shadowsocks:相关地址不在 PAC 名单中,所以可能需要使用全局模式。
##: [[File:Idea:为 Maven-importing 设置代理.png|none|thumb|600px|Idea:为 Maven-importing 设置代理]]
# 提示信息为:“'''Repository is non-nexus repo, or is not indexed'''”。
#: 原因:'''该仓库不支持仓库索引''';(例如:阿里云等Maven仓库“目前还未支持生成和下载 maven indexer 文件”,参见:https://developer.aliyun.com/article/621196 )
#: 解决:使用支持索引的 Maven 仓库;
网上看到了另一种思路:
# 使用 nexus 建立 Maven 私服;
#: 参考:
#* https://blog.csdn.net/qq_40573891/article/details/80711107?spm=1001.2101.3001.6650.10&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-10.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-10.no_search_link
#* https://blog.csdn.net/u010165638/article/details/89948308
P.S.:
#  阿里云 Maven 仓库:“'''阿里不再支持 http 下载,只支持 https'''”。
#: 需要对 Maven->importing(VM options for importer)设置“'''忽略ssl证书的验证'''”:“-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true”;
#* 阿里云Maven仓库地址:https://developer.aliyun.com/mvn/guide

2022年5月8日 (日) 21:32的最新版本


关于

Settings.xml 是用来设置 maven 参数的配置文件,包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

参考:http://maven.apache.org/settings.html

settings.xml 文件一般存在于两个位置:

  1. 全局配置:${M2_HOME}/conf/settings.xml
  2. 用户配置:${user.home}/.m2/settings.xml

全局配置一旦更改,所有的用户都会受到影响,而且如果 maven 进行升级,所有的配置都会被清除,所以要提前复制和备份 ${M2_HOME}/conf/settings.xml 文件,一般情况下不推荐配置全局的 settings.xml。

Maven 相关的配置

Maven 有三个相关的配置文件,优先级从高到低:

pom.xml(局部配置) > user settings(用户配置) > global settings(全局配置)
  • 如果这些文件同时存在,在应用配置时,会合并它们的内容;如果有重复的配置,优先级高的配置会覆盖优先级低的。

概述

settings.xml:(展开/隐藏)

详述

下面列举了 settings.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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
	
	<localRepository/>
	<interactiveMode/>
	<usePluginRegistry/>
	<offline/>
	<pluginGroups/>
	<proxies/>
	<servers/>
	<mirrors/>
	<profiles/>
	<activeProfiles/>
</settings>
  • 其中最常用的是:
    • <LocalRepository>
    • <servers>
    • <mirrors>
    • <profiles>


<LocalRepository>


作用:表示构建系统本地仓库的路径。

  • 默认值:~/.m2/repository。
	<localRepository>${user.home}/.m2/repository</localRepository>

<interactiveMode>


作用:表示 maven 是否需要和用户交互以获得输入。

即,当 Maven 在执行中需要输入一些操作指令或者一些参数时,是让用户输入,还是使用默认值。
  • 默认值:ture。
  • 如果设置为 false,Maven 会在执行的时候采取默认值,这些默认值可能是已经有的,可能是链式默认值。
	<interactiveMode>true</interactiveMode>

<usePluginRegistry>


作用:表示 maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。

  • 默认值:false。
	<usePluginRegistry>false</usePluginRegistry>

<offline>


作用:表示 maven 是否需要在离线模式下运行。

  • 默认值:false。
  • 当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。
	<offline>false</offline>

<pluginGroups>


作用:当插件的“组织Id”(groupId)没有显式提供时,供搜寻插件“组织Id”(groupId)的列表。

当我们使用某个插件,并且没有在命令行为其提供“组织Id”(groupId)的时候,Maven就会使用该列表。
  • 每个子元素包含了一个“组织Id”。
  • 默认情况下该列表包含了:“org.apache.maven.plugins”和“org.codehaus.mojo”。
	<pluginGroups>
        <pluginGroup>org.codehaus.mojo</pluginGroup>
    </pluginGroups>

<proxies>


作用:用来配置不同的代理。

	<proxies>
        <proxy>
            <id>myproxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>proxy.somewhere.com</host>
            <port>1080</port>
            <username>proxyuser</username>
            <password>proxypass</password>
            <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
    </proxies>
  1. id:proxy 的唯一标识符;
  2. active:是否使用该代理;
    • Maven 会使用列表组中第一个激活的服务器作为代理服务器;
  3. protocol:代理的协议;
  4. host:代理的主机名;
  5. port:代理的端口;
  6. username:代理的用户名;
  7. password:代理的密码;
  8. nonProxyHosts:不该被代理的主机名列表。
    • 该列表的分隔符由代理服务器指定。(例子中使用了竖线分隔符,使用逗号分隔也很常见)

<servers>


一般,仓库的下载和部署是在 pom.xml 文件中的 repositoriesdistributionManagement 元素中定义的。

然而,一般类似用户名、密码、安全证书等信息(有些仓库访问是需要安全认证的)不应该在 pom.xml 文件中配置,这些信息应该配置在 settings.xml 中。

作用:根据不同的私服 server(根据 id 区分)来配置每个私服的账号和密码(或者私钥指纹)

  • 当 Maven 使用了私服仓库,或,项目使用自有仓库时,该项十分有用。
	<servers>
        <server>
            <id>server001</id>
            <username>my_login</username>
            <password>my_password</password>
            <privateKey>${usr.home}/.ssh/id_dsa</privateKey>
            <passphrase>some_passphrase</passphrase>
            <filePermissions>664</filePermissions>
            <directoryPermissions>775</directoryPermissions>
            <configuration></configuration>
        </server>
    </servers>
  1. id:表示 server 的 id(注意不是用户登陆的id);
    • 与 pom.xml 文件中 <distributionManagement> 下 <repository> 元素的 <id> 相匹配。
  2. username:服务器认证的用户名;
  3. password:服务器认证的密码;
    • 以上两个元素用于“用户名/密码”认证。
  4. privateKey:服务器认证所使用的私钥位置;(默认是:${user.home}/.ssh/id_dsa)
  5. passphrase:服务器认证所使用的私钥密码;
    • 以上两个元素用于“私钥”认证。
  6. filePermissions:文件被创建时的权限;
  7. directoryPermissions:目录被创建时的权限;
    • 如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限(filePermissions、directoryPermissions)。
    • 这两个元素合法的值是一个三位数字,其对应了 unix 文件系统的权限(如:664、775)。
  8. configuration:传输层额外的配置项;

示例

项目的 pom.xml 中使用了两个仓库:

  1. 一个是 snapshot 的仓库(id为:cat-snapshot),
  2. 一个是 releases 仓库(id为:cat-release)。

那么相关的配置如下:

  1. pom.xml:
    	<distributionManagement>
    		<repository>
    			<id>cat-release</id>
    			<name>RELEASES</name>
    			<url>http://172.10.0.100:8081/repository/cat-releases/</url>
    		</repository>
    
    		<snapshotRepository>
    			<id>cat-snapshot</id>
    			<name>SNAPSHOT</name>
    			<url>http://172.10.0.100:8081/repository/cat-snapshot/</url>
    		</snapshotRepository>
    	</distributionManagement>
    
  2. settings.xml:
    	<servers>
    		<server>
    			<id>cat-release</id>
    			<username>laomao</username>
    			<password>123456</password>
    		</server>
    		<server>
    			<id>cat-snapshot</id>
    			<username>laomao</username>
    			<password>123456</password>
    		</server>
    		...
    	</servers>
    

如上:

  • pom.xml 与 settings.xml 中的 id 项需对应;
  • 私服中 laomao 账号具有访问操作两个仓的权限;
没有权限的话,在打包发布到私仓的时候,会提示“code 401”,权限不足,或者 Access Denied,拒绝访问。

出现这个问题的时候,排查方法如下:
1、首先要确定id是否对应;
2、然后检查账号密码是否错误;
3、最后去检查私服中是否给对应的账号配置了对应私仓的权限。

<mirrors>


镜像:是指“目标仓库”的“镜像”、“备用库”、“copy”。

配置了镜像之后,Maven 会从“备用仓库”里面获取依赖,而不是目标仓库。

作用:为仓库列表配置的下载镜像列表。

	<mirrors>
		<mirror>
			<id>alimaven</id>
			<name>aliyun-maven</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<mirrorOf>central</mirrorOf>
		</mirror>
		...
	</mirrors>
  1. id:镜像的唯一标识符;
  2. name:镜像的名称;
  3. url:镜像的 URL;
    • 构建系统会优先考虑使用该 URL,而非使用默认的服务器 URL。
  4. mirrorOf:被镜像的服务器的 id,是指该镜像对哪个库生效;
    • 必须和配置的仓库 id 对应。
    • Maven还支持更高级的镜像配置:
      1. “<mirrorOf>*</mirrorOf>”:匹配所有远程仓库。
      2. “<mirrorOf>external:*</mirrorOf>”:匹配所有远程仓库,使用 localhost 的除外,使用 file:// 协议的除外。(也就是说,匹配所有不在本机上的远程仓库)
      3. “<mirrorOf>repo1,repo2</mirrorOf>”:匹配仓库 repo1 和 repo2。(使用逗号分隔多个远程仓库)
      4. “<mirrorOf>*,!repo1</miiroOf>”:匹配所有远程仓库,repo1 除外。(使用感叹号将仓库从匹配中排除)
    • 由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven 仍将无法访问被镜像仓库,因而将无法下载构件。

<profiles>


如 pom.xml 中的 profile 一样,profile 的作用在于它能够在某些特定的环境中自动使用某些特定的值。

profile 可以让 maven 能够自动适应外部的环境变化。比如同一个项目,在 linux 下编译 linux 的版本,在 win 下编译 win 的版本等。

作用:根据环境参数来调整构建配置的列表。


profiles 包含了:

	<profiles>
		<profile>
			<!-- profile 的唯一标识 -->
			<id>test</id>
			<!-- 自动触发 profile 的条件逻辑 -->
			<activation />
			<!-- 扩展属性列表 -->
			<properties />
			<!-- 远程仓库列表 -->
			<repositories />
			<!-- 插件仓库列表 -->
			<pluginRepositories />
		</profile>
	</profiles>
  • 只包含这五个子元素是因为:是因为他们涉及到整个的构建系统(这正是 settings.xml 文件的角色定位),而不是个别的项目级别的 POM 配置。
  • 一个项目可以设置多个 profile,也可以在同一时间设置多个 profile 被激活(active)的。自动激活的 profile 的条件可以是各种各样的设定条件,组合放置在 activation 节点中,也可以通过命令行直接指定。

<Activation>


如果一个 settings.xml 中的 profile 被激活,它的值会覆盖任何其它定义在 pom.xml 中带有相同 id 的 profile。

如果想要某个 profile 默认处于激活状态,可以在 settings.xml 的 <activeProfiles> 中将该 profile 的 id 放进去。这样,不论环境设置如何,其对应的 profile 都会被激活。

作用:自动触发 profile 的条件逻辑。

即,设置该 profile 在什么条件下会被激活;
			<activation>
				<activeByDefault>false</activeByDefault>
				<jdk>1.5</jdk>
				<os>
					<name>Windows XP</name>
					<family>Windows</family>
					<arch>x86</arch>
					<version>5.1.2600</version>
				</os>
				<property>
					<name>mavenVersion</name>
					<value>2.0.3</value>
				</property>
				<file>
					<exists>${basedir}/file2.properties</exists>
					<missing>${basedir}/file1.properties</missing>
				</file>
			</activation>

常见的激活条件有如下几个:

  1. activeByDefault:在没有指定激活其他 profile 的时候,是否默认激活。
    • 如果有其他 profile 被指定激活之后,则这个 profile 将不会被激活。
  2. os:判断操作系统相关的参数。它包含如下可以自由组合的子节点元素:
    1. name:匹配操作系统的名字;
    2. family:匹配操作系统家族;
      • 常见的取值为:dos,mac,netware,os/2,unix,windows,win9x,os/400 等。
    3. arch:匹配 cpu 结构;
      • 常见为 x86。
    4. version:匹配的操作系统版本号;
    5. display:检测到操作系统之后显示的信息;
    6. message:规则失败之后显示的消息;
  3. jdk:检查 jdk 版本。
    • 可以用区间表示。
    例如,“1.4”匹配 JDK1.4、JDK 1.4.0_2 等;而“!1.4”匹配所有版本不是以 1.4 开头的 JDK。
  4. property:检查属性值。
    1. name:属性名称;
    2. value:属性值;
    • 如果 Maven 检测到某一个属性(其值可以在 POM 中通过“${name}”引用)拥有对应的“name = value”,Profile就会被激活。
    • 如果 value 字段是空的,那么存在 name 字段就会激活 profile,否则按区分大小写方式匹配属性值字段。
  5. file:检查文件相关内容。
    1. missing:如果文件不存在,则激活 profile。
    2. exists:如果文件存在,则激活 profile。

<properties>


maven 属性和 ant 中的属性一样,可以用来存放一些值。

这些值可以在 pom.xml 中的任何地方使用标记 ${Xxx} 来使用(Xxx 是指属性的名称)。

作用:profile 的扩展属性列表。


properties 有五种不同的形式,并且都能在 settings.xml 文件中访问:

  1. env.Xxx:在一个变量前加上“env.”的前缀,会返回一个 shell 环境变量。
    例如,“env.PATH”指代了“$path”环境变量(在 Windows 上是“%PATH%”)。
  2. project.Xxx:指代了 POM 中对应的元素值。
    例如:“<project><version>1.0</version></project>”通过“${project.version}”获得 version 的值。
  3. settings.Xxx:指代了 settings.xml 中对应元素的值。
    例如:“<settings><offline>false</offline></settings>”通过“${settings.offline}”获得 offline 的值。
  4. Java System Properties:所有可通过“java.lang.System.getProperties()”访问的属性,都能在 POM 中使用该形式访问。
    例如:“${java.home}”。
  5. Xxx:在 <properties/> 元素中,或者外部文件中设置,以“${someVar}”的形式使用。


示例:

如下 profile 被激活,则可以在 pom.xml 中使用“${user.install}”。
			<properties>
				<user.install>${user.home}/our-project</user.install>
			</properties>

<repositories>


如果配置了多个 repository 会用哪个呢?

答案是按顺序使用:如果第一个可用,就用第一个,如果不可用,就依次往下找。

作用:远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。

			<repositories>
				<repository>
					<id>codehausSnapshots</id>
					<name>Codehaus Snapshots</name>
					<releases>
						<enabled>false</enabled>
						<updatePolicy>always</updatePolicy>
						<checksumPolicy>warn</checksumPolicy>
					</releases>
					<snapshots>
						<enabled />
						<updatePolicy />
						<checksumPolicy />
					</snapshots>
					<url>http://snapshots.maven.codehaus.org/maven2</url>
					<layout>default</layout>
				</repository>
			</repositories>
  1. id:远程仓库唯一标识;
  2. name:远程仓库名称;
  3. releases:如何处理远程仓库里“发布版本”的下载;
    1. enabled:该仓库是否为下载“发布版”构件开启;(snapshots 中则为“快照版”)
      • 默认 true。
    2. updatePolicy:指定更新发生的频率;
      • 可选值:always(一直)、daily(每日,默认)、interval:X(X 是以分钟为单位的时间间隔)、never(从不)。
    3. checksumPolicy:当 Maven 验证构件校验文件失败时,如何处理
      • 可选值:ignore(忽略)、fail(失败)、warn(警告)。
  4. snapshots:如何处理远程仓库里“快照版本”的下载;
    • (类似 releases)
    • 有了 releases 和 snapshots 这两组配置,POM 就可以在每个单独的仓库中,为每种类型的构件采取不同的策略。
  5. url:远程仓库 URL;
    • 按“protocol://hostname/path”形式配置。
  6. layout:用于定位和排序构件的仓库布局类型;
    • 可选值:default(默认)、legacy(遗留)。
    • Maven 2 为其仓库提供了一个默认的布局;然而,Maven 1.x 有一种不同的布局。

<pluginRepositories>


maven 插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。

pluginRepositories(管理插件的仓库)和 repository(管理 jar 包依赖的仓库)类似,每个 pluginRepository 元素指定一个Maven可以用来寻找新插件的远程地址。

作用:插件的远程仓库列表。

			<pluginRepositories>
				<pluginRepository>
					<id />
					<name />
					
					<releases>
						<enabled />
						<updatePolicy />
						<checksumPolicy />
					</releases>
					<snapshots>
						<enabled />
						<updatePolicy />
						<checksumPolicy />
					</snapshots>
					
					<url />
					<layout />
				</pluginRepository>
			</pluginRepositories>

<activeProfiles>


作用:手动激活 profiles 的列表,按照 profile 被应用的顺序定义 activeProfile。

  • 任何在 activeProfile 中定义的 profile id,不论环境设置如何,其对应的 profile 都会被激活。
  • 如果没有匹配的 profile,则什么都不会发生。
			<activeProfiles>
				<activeProfile>dev</activeProfile>
			</activeProfiles>

示例

见:Maven:settings.xml示例

FAQ

关于:profile

“profile 的位置”

profile 可以在 settings.xml 中配置,也可以在 pom.xml 文件中配置。

关于 profile 配置的位置:

  • 特定项目的配置就在项目的 pom.xml 文件中配置;
  • 特定用户的配置则放在用户目录下的 .m2 文件夹下;
  • 全局的配置则放在 Maven 安装目录下:${maven.home}、${m2.home};


“pom.xml 中的 profile”

settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。

pom.xml 中的 profile 配置:

	<project>
		<profiles>
			<profile>
				<id>xxxx</id>
				<build>
					<defaultGoal>...</defaultGoal>
					<finalName>...</finalName>
					<resources>...</resources>
					<testResources>...</testResources>
					<plugins>...</plugins>
				</build>
				<reporting>...</reporting>
				<modules>...</modules>
				<dependencies>...</dependencies>
				<dependencyManagement>...</dependencyManagement>
				<distributionManagement>...</distributionManagement>
				<repositories>...</repositories>
				<pluginRepositories>...</pluginRepositories>
				<properties>...</properties>
			</profile>
		</profiles>
	</project>


通过命令行管理 profile 激活状态

在运行 Maven 时:

  1. 激活 profile:运行时需要激活某个 profile,在 mvn 命令中,使用参数 -P 来指定要激活的 profile:
    mvn -P <profileID>
    
  2. 取消 profile:当某个 profile 在 activeProfile、activeByDefault 中被激活了,在 mvn 命令中,使用参数 -P!<profileID> 来取消激活的 profile:
    mvn -P !<profileID>
    
  • 在 maven 工程的 pom.xml 所在目录下执行 mvn help:active-profiles 命令可以查看中央仓储的 profile 是否在工程中生效。

Mirror 与 Repository


Repository(仓库):repository 里存放的都是各种 jar 包和 maven 插件。

  • Maven 仓库主要有 2 种:
    1. local repository:本地仓库,存放在本地磁盘的一个文件夹。(例如,windows 上默认是:C:\Users\{用户名}\.m2\repository 目录)
    2. remote repository:远程仓库,相当于公共的仓库,大家都能访问到,一般可以用 URL 的形式访问。
      1. 中央仓库:http://repo1.maven.org/maven2/。
      2. 私服:内网自建的 maven repository,其 URL 是一个内部网址。
      3. 其他公共仓库:其他可以互联网公共访问 maven repository(例如 jboss repository 等)。
当向仓库请求插件或依赖的时候,会先检查 local repository,如果 local repository 有则直接返回,否则会向 remote repository 请求,并缓存到 local repository。

也可以把做的东西放到本地仓库,仅供本地使用;或上传到远程仓库,供大家使用。 


Mirror(镜像):mirror 相当于一个拦截器,它会拦截 maven 对 remote repository 的相关请求,把请求里的 remote repository 地址,重定向到 mirror 里配置的地址。

  • 其实,mirror 表示的是两个 Repository 之间的关系
  • 如果某个 Repository 配置了多个 Mirror,(当前面的 Mirror 不可用时)会按顺序尝试。
  • 由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven 仍将无法访问被镜像仓库,因而将无法下载构件。


如果不配置 Repository,则 Maven 仍然有一个默认仓库“central”(存在于源码,即使添加了其他仓库,它也仍然存在)。

所以,网上很多资料,都只添加一个 Mirror,用于重定向默认仓库 central 的地址。