编程分享Maven框架settings详解
郭顺发1. 概述
1.1. 作用
用来设置Maven参数的配置文件,在Maven中提供了一个settings.xml文件来定义Maven的全局配置信息。我们通过这个文件来定义本地仓库、远程仓库和联网使用的代理信息等配置。
1.2. 文件位置
一般存在于两个位置:
全局配置:Maven的安装目录的conf子目录下面(${M2_HOME}/conf/settings.xml)
用户目录的的.m2子目录下面({user.home}/.m2/settings.xml)
。当前用户的独享配置。
当我们使用一些工具时(IDEA),可以直接指定settings.xml文件的位置。
1.3. 配置文件优先级
局部配置高于全局配置 配置优先级从高到低:pom.xml> user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。
1.4. Maven依赖搜索顺序
当我们执行Maven命令时,maven开始按照以下顺序查找依赖库:
- 步骤 1:在本地仓库搜索,如果找不到,执行步骤 2,找到了则执行其他操作
- 步骤 2:在中央仓库搜索,如果找不到,并且有一个或多个远程仓库已经设置,则执行步骤 4,如果找到了则下载到本地仓库中引用。
- 步骤 3:如果远程仓库没有被设置, 将简单的停滞处理并抛出错误(无法找到依赖的文件)。
- 步骤 4:在一个或多个远程仓库中搜索依赖的文件, 如果找到则下载到本地仓库已被将来引用, 否则将停止处理并抛出错误(无法找到依赖的文件)。
2. 元素详解
2.1. 顶级元素
2.1.1. LocalRepository
1
| <localRepository>${user.home}/.m2/repository</localRepository>
|
2.1.2. InteractiveMode
1
| <interactiveMode>true</interactiveMode>
|
2.1.3. offline
1
| <offline>false</offline>
|
2.1.4. pluginGroups
1 2 3 4
| <pluginGroups> <pluginGroup>com.your1.plugins</pluginGroup> <pluginGroup>com.your2.plugins</pluginGroup> </pluginGroups>
|
2.1.5. proxies
1 2 3 4 5 6 7 8 9 10 11 12
| <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>
|
2.1.6. servers
1 2 3 4 5 6 7 8 9 10 11 12
| <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>
|
2.1.7. mirrors
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <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> <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>
|
mirrorOf配置语法:
- :匹配所有远程仓库。相当于一个拦截器,它会拦截远程仓库的相关请求,把请求里的远程仓库地址,重定向到mirror里配置的地址。
- external:* : 匹配除 localhost、使用 file:// 协议外的所有远程仓库
- repo1,repo2:匹配仓库 repo1 和 repo2
- ,!repo1: 匹配所有远程仓库, repo1 除外
2.1.7. profiles
作用:构建方法的配置清单, maven 将根据不同环境参数来使用这些构建配置。 注意:settings.xml 中的 profile元素是 pom.xml中 profile元素的裁剪版本。
- settings.xml负责的是整体的构建过程, pom.xml负责单独的项目对象构建过程。
- settings.xml 只包含了id, activation, repositories, pluginRepositories 和 properties 元素。
- 如果 settings中的 profile 被激活, 它的值会覆盖任何其它定义在 pom.xml中或 profile.xml中的相同 id 的 profile。
查看当前激活的 profile
1
| mvn help:active-profiles
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <profiles> <profile> <id>jdk-1.4</id> <activation> <activeByDefault>false</activeByDefault> <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> <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>
|
2.1.8. activeProfiles
1 2 3 4
| <activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> </activeProfiles>
|
参考资料