概述
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://www.cnblogs.com/boboooo/p/9485770.html