MAVEN简介之——settings.xml
概述
Maven的settings.xml配置了Maven执行的方式,像pom.xml一样,但是它是一个通用的配置, 不能绑定到任何特殊的项目。它通常包括本地仓库地址,远程仓库服务,认证信息等。
settings.xml存在于两个位置:
- maven目录下的
/conf/settings.xml - 用户目录下的
/.m2/settings.xml
maven目录下的称为全局配置,用户目录下的称为用户配置。如果两个配置都存在,它们的内容将合并,有冲突的以用户配置优先。 通常情况下,用户目录下的/.m2/settings.xml是不存在的,如果你需要,可以从maven目录下的/conf/settings.xml复制过来。 maven的默认settings模板中,包含了所有的配置的例子,它们都被注释掉了,如果你需要,可以打开注释,配置你自己的信息。
下面是settings文件的顶层元素:
<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/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
settings文件中的内容可以使用插值替换,例如:
${user.home}或者其他的系统属性(3.0以上)${env.HOME}等环境变量
注意:profile中定义的properties不能使用插值
详细设置
简单值(simple value)
settings文件中,顶层元素中的一半以上都是简单值。接下来让我们看一看吧。
<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>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<usePluginRegistry>false</usePluginRegistry>
<offline>false</offline>
...
</settings>
- localRepository:本地仓库路径,默认值为:
${user.home}/.m2/repository。它允许所有的用户从这个公共的本地仓库构建系统。 - interactiveMode:默认为true,代表maven是否可以和用户通过输入进行交互。
- usePluginRegistry:默认为false,maven是否可以使用
${user.home}/.m2/plugin-registry.xml管理插件版本。从2.0以后,我们是不需要使用这个属性的,可以认为它废弃了。 - offline:默认false,构建系统是否可以使用离线模式。在不能连接远程仓库的情况下,这个属性是非常有用的。
插件组(Plugin Groups)
pluginGroups包含了一组pluginGroup元素,每一个都包含一个groupId。当你在命令行使用插件,没有提供groupId时,maven将搜索这个列表。 列表默认包含org.apache.maven.plugins和org.codehaus.mojo。
<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">
...
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
...
</settings>
例如:我们执行org.mortbay.jetty:jetty-maven-plugin:run时,可以使用短命令:mvn jetty:run。
服务(Servers)
下载和部署的仓库通常在pom.xml中的repositories和distributionManagement元素中定义,但是像username和password时不应该在 单独的pom文件中定义,这种配置信息应该在settings中定义。
<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">
...
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
...
</settings>
- id:server的id,它和maven连接的repository或mirror的id匹配。
- username, password:用户名和密码,这两个元素成对出现。
- privateKey, passphrase:私钥文件和私钥密码,也是成对出现。
- filePermissions, directoryPermissions:当通过maven部署到远程仓库的时候,文件和目录的权限通过这两个元素指定。
当使用私钥文件的时候,不要使用password,要使用passphrase。
镜像(Mirrors)
<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">
...
<mirrors>
<mirror>
<id>planetmirror.com</id>
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
...
</settings>
- id, name:mirror的唯一标识和用户设置的别名。当连接镜像需要用户名密码或私钥时,id要和
<servers>中配置的id一致。 - url:镜像的url。构建系统时将使用这个地址,而不是原始的仓库地址。
- mirrorOf:仓库镜像的id。例如:指向maven的中央仓库(https://repo.maven.apache.org/maven2/),设置为
center。也可以使用一些高级的语法:repo1,repo2或*,!inhouse。
代理(Proxies)
<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">
...
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
...
</settings>
- id:proxy的唯一标识。
- active:代理是否有效。多个代理的情况下,只能有一个代理有效。
- protocol, host, port:代理的
protocol://host:port,分隔成了多个元素。 - username, password:代理的用户名和密码,成对出现。
- nonProxyHosts:不使用代理的主机。使用逗号“,”分隔也可以。
镜像和代理的区别:镜像:改变原始的仓库地址;代理:有些公司是不能上网的,他们需要配置代理才能访问外网。
用户配置(Profiles)
settings.xml文件中的profile是pom.xml中的删减版。它由activation, repositories, pluginRepositories 和 properties组成。 而且只包含这4个元素,因为settings中的是全局配置,不是单个项目的配置。
如果settings中的profile是有效的,它将覆盖掉pom中的相同id的profile。
激活(Activation)
它是profile中的一个元素,会在满足activation的条件时,激活状态。
<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">
...
<profiles>
<profile>
<id>test</id>
<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>
...
</profile>
</profiles>
...
</settings>
当activation的条件满足时,该profile将激活。
- jdk:
activation有一个内嵌的,在jdk元素中已java为中心的检查。当jdk的版本与配置的版本前缀匹配时,这个profile将被激活。 上面的例子中,jdk的版本1.5.0_06将匹配。范围配置也是可以的,这里不做详细介绍了。 - os:os可以定义一些运行系统的特殊属性。由于比较少用,不做过多介绍,有兴趣的可以查阅官方文档。
- property:如果maven探测到一个属性(这个属性的值可以在pom.xml中配置),它的值与配置的值匹配,这个profile将被激活。上面的例子中, mavenVersion=2.0.3时,profile将激活。
- file:existence的文件存在,或者missing的文件不存在,条件将激活。
activation不是profile激活的唯一方式,settings.xml文件中的activeProfile元素包含了一个profile的id,可以同过命令行指定这个id来 激活profile。例如:-P test,将激活id为test的profile。
属性(Properties)
maven的属性是一个占位符,它可以在pom文件中,通过${X}进行访问,X是属性的名称。它们有5中不同的形式:
env.X:前缀是一个env,它将返回系统的环境变量。例如:${env.PATH}将返回系统的环境变量$path。project.x:访问pom嗯我那件,点(.)在pom中代表层级的分隔。例如:<project><version>1.0</version></project>可以通过${project.version}访问。settings.x:同上,只是访问的是settings文件。例如:<settings><offline>false</offline></settings>可以通过${settings.offline}访问。- Java System Properties:java系统属性,所有通过
java.lang.System.getProperties()可以访问到的属性,在pom文件中都可以访问。 例如:${java.home}。 x:<properties>元素里配置的属性。通过${someVal}访问。
<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">
...
<profiles>
<profile>
...
<properties>
<user.install>${user.home}/our-project</user.install>
</properties>
...
</profile>
</profiles>
...
</settings>
上面的例子中,如果profile被激活,在pom中可以访问${user.install}。
仓库(Repositories)
Repositories在这里不是本地仓库的意思,而是远程仓库的集合。它在本地仓库配置,maven通过它从远程下载插件或者依赖。 不同的仓库包含不同的项目,在激活的profile下,它们能被搜索到。
<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">
...
<profiles>
<profile>
...
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
</profiles>
...
</settings>
- releases, snapshots:稳定版本或快照版本对应的配置。
- enabled:true或者false。对应版本的仓库是否可用。
- updatePolicy:更新策略。它指定了多长时间更新一次,maven经常比较本地pom和远程pom的时间戳。它的选项有:
always、daily(默认)、interval:X(X是分钟)、never。 - checksumPolicy:当maven部署文件到仓库时,它还会部署相对应的checksum文件。选项有:
ignore,fail, 或warn,在checksum丢失或不正确的情况下执行。 - layout:在上面的配置中,它们都跟随一个公共的布局。这在大多数情况下是正确的。Maven 2有一个仓库的默认布局,但是maven 1.x有一个不同的布局。 使用这个元素可以选择使用哪个版本的布局,
default或legacy。
插件仓库(Plugin Repositories)
仓库有两种主要的类型。第一种是工件作为依赖,常说的jar包依赖。第二种是插件,maven的插件是一种特殊类型的工件,正因如此,maven把插件类型的仓库 单独提了出来。pluginRepositories的元素和repositories的元素非常的相似,它指定一个远程插件仓库的地址,可以在那里找到相应的maven插件。
激活profile(Active Profiles)
<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">
...
<activeProfiles>
<activeProfile>env-test</activeProfile>
</activeProfiles>
</settings>
activeProfiles元素包含了activeProfile元素的集合,activeProfile有一个profile的id值。在activeProfile里定义的id都将被激活。 如果没有找到匹配的profile,什么都不会生效。
好了,maven的settings.xml就为大家介绍的这里,有疑问可以随时评论、留言。接下来还会介绍maven的pom.xml。
我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1u17c7u12h747
MAVEN简介之——settings.xml的更多相关文章
- MAVEN简介之——pom.xml
maven构建的生命周期 maven是围绕着构建生命周期这个核心概念为基础的.maven里有3个内嵌的构建生命周期,default,clean和site. default是处理你项目部署的:clean ...
- Maven(四-1) Maven的配置文件settings.xml
转载于:http://www.cnblogs.com/yakov/archive/2011/11/26/maven2_settings.html 概览 当Maven运行过程中的各种配置,例如pom.x ...
- maven 完整的settings.xml
maven 完整的settings.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licen ...
- maven学习7 settings.xml解析
maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.home}/.m2/setting ...
- Maven全局配置文件settings.xml详解(转)
Maven全局配置文件settings.xml详解 目录 一.概要 1.settings.xml的作用2.settings.xml文件位置3.配置的优先级 二.settings.xml元素详解 1 ...
- maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. settings.xml文件是干什么的,为什么要配置它 ...
- [转]maven全局配置文件settings.xml详解
概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径. Paste_Image.png settings.xm ...
- [No000016B]清华maven库配置settings.xml
路径:"C:\Users\%USERNAME%\.m2\settings.xml" <settings xmlns="http://maven.apache.org ...
- 08.基于IDEA+Spring+Maven搭建测试项目--Maven的配置文件settings.xml
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
随机推荐
- 格式化代码引发的css编译失败
之前在做feather项目,处理IE8下的问题时,写 filter: progid: DXImageTransform.Microsoft.AlphaImageLoader(src='#', sizi ...
- Python垃圾回收机制--完美讲解!
转自: http://www.jianshu.com/p/1e375fb40506 先来个概述,第二部分的画述才是厉害的. Garbage collection(GC) 现在的高级语言如java,c# ...
- Java_深度剖析ConcurrentHashMap
本文基于Java 7的源码做剖析. ConcurrentHashMap的目的 多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用Hash ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- Windows 主机名映射地址
在开发中大数据集群中我们自己的电脑主机名映射不到集群的主机名下面我们就去修改自己电脑 主机名映射地址 c/Windows/System32/drivers/etc/host 文件将主机名和IP地址 ...
- 报文分析4、TCP协议的头结构
TCP协议的头结构 来源端口(2字节) 目的端口(2字节) 序号(4字节) 确认序号(4字节) 头长度(4位) 保留(6位) URG ACK PSH RST SYN PIN 窗口大小(2字节) 校验和 ...
- Python 学习笔记5 变量-列表
列表是python常用的一种变量. 是由一些列按照特定顺序排列的元素组成的.你可以创建包含字母表中的所有字母,数字.可以将任何东西都加入到列表中. 通常情况下,列表中都包含多个元素,所以建议变量的名称 ...
- Luogu4197 Peaks
题目链接:洛谷 看到“只经过困难值小于等于$x$的路径”. 感觉有点眼熟. ow,就是[NOI2018]归程. 和那道题一样,可以直接建出Kruskal重构树,每次倍增寻找祖先中最上面的不大于$x$的 ...
- IIS宿主WCF服务*.svc Mime类型映射
经常会遇到由于.net安装组件缺失,导致发布wcf服务后,访问wcf报.svc请求类型不支持 简单方法就是添加删除程序,修改.net组件安装选项,勾选http激活即可: 或者手工添加映射处理程序 1. ...
- vs code代码对齐快捷键
vscode缩进快捷键: 选中文本: Ctrl + [ 和 Ctrl + ] 实现文本的向左移动或者向右移动: vscode代码对齐快捷键: 选中文本: Shift + ...