在 Maven 中提供了一个 settings.xml 文件来定义 Maven 的全局环境信息. 这个文件会存在于 Maven 的安装目录的 conf 子目录下面, 或者是用户家目录的. m2 子目录下面. 我们可以通过这个文件来定义本地仓库, 远程仓库和联网使用的代理信息等.
其实相对于多用户的 PC 机而言, 在 Maven 安装目录的 conf 子目录下面的 settings.xml 才是真正的全局的配置. 而用户家目录的. m2 子目录下面的 settings.xml 的配置只是针对当前用户的. 当这两个文件同时存在的时候, 那么对于相同的配置信息用户家目录下面的 settings.xml 中定义的会覆盖 Maven 安装目录下面的 settings.xml 中的定义. 用户家目录下的 settings.xml 文件一般是不存在的, 但是 Maven 允许我们在这里定义我们自己的 settings.xml, 如果需要在这里定义我们自己的 settings.xml 的时候就可以把 Maven 安装目录下面的 settings.xml 文件拷贝到用户家目录的. m2 目录下, 然后改成自己想要的样子.
先来看一个基本的 settings.xml 的样子:
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <?
- 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">
- <localRepository>/path/to/local/repo</localRepository>
- <interactiveMode>true</interactiveMode>
- <offline>false</offline>
- <pluginGroups>
- </pluginGroups>
- <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>
- <servers>
- <server>
- <id>deploymentRepo</id>
- <username>repouser</username>
- <password>repopwd</password>
- </server>
- </server>
- <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>
- </mirrors>
- <profiles>
- <profile>
- <id>jdk-1.4</id>
- <activation>
- <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>
- </profiles>
- <activeProfiles>
- <activeProfile>alwaysActiveProfile</activeProfile>
- <activeProfile>anotherAlwaysActiveProfile</activeProfile>
- </activeProfiles>
- </settings>
settings.xml 中主要包括以下元素:
localRepository: 表示 Maven 用来在本地储存信息的本地仓库的目录. 默认是用户家目录下面的. m2/repository 目录.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <localRepository>/path/to/local/repo</localRepository>
interactiveMode: 表示是否使用交互模式, 默认是 true; 如果设为 false, 那么当 Maven 需要用户进行输入的时候, 它会使用一个默认值.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <interactiveMode>true</interactiveMode>
offline: 表示是否离线, 默认是 false. 这个属性表示在 Maven 进行项目编译和部署等操作时是否允许 Maven 进行联网来下载所需要的信息.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <offline>false</offline>
pluginGroups: 在 pluginGroups 元素下面可以定义一系列的 pluginGroup 元素. 表示当通过 plugin 的前缀来解析 plugin 的时候到哪里寻找. pluginGroup 元素指定的是 plugin 的 groupId. 默认情况下, Maven 会自动把 org.apache.maven.plugins 和 org.codehaus.mojo 添加到 pluginGroups 下.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <pluginGroups> </pluginGroups>
proxies: 其下面可以定义一系列的 proxy 子元素, 表示 Maven 在进行联网时需要使用到的代理. 当设置了多个代理的时候第一个标记 active 为 true 的代理将会被使用.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <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>
servers: 其下面可以定义一系列的 server 子元素, 表示当需要连接到一个远程服务器的时候需要使用到的验证方式. 这主要有 username/password 和 privateKey/passphrase 这两种方式.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <servers>
- <server>
- <id>deploymentRepo</id>
- <username>repouser</username>
- <password>repopwd</password>
- </server>
- </server>
mirrors: 用于定义一系列的远程仓库的镜像. 我们可以在 pom 中定义一个下载工件的时候所使用的远程仓库. 但是有时候这个远程仓库会比较忙, 所以这个时候人们就想着给它创建镜像以缓解远程仓库的压力, 也就是说会把对远程仓库的请求转换到对其镜像地址的请求. 每个远程仓库都会有一个 id, 这样我们就可以创建自己的 mirror 来关联到该仓库, 那么以后需要从远程仓库下载工件的时候 Maven 就可以从我们定义好的 mirror 站点来下载, 这可以很好的缓解我们远程仓库的压力. 在我们定义的 mirror 中每个远程仓库都只能有一个 mirror 与它关联, 也就是说你不能同时配置多个 mirror 的 mirrorOf 指向同一个 repositoryId.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <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>
- </mirrors>
l id: 是用来区别 mirror 的, 所有的 mirror 不能有相同的 id
l mirrorOf: 用来表示该 mirror 是关联的哪一个仓库, 其值为其关联仓库的 id. 当要同时关联多个仓库时, 这多个仓库之间可以用逗号隔开; 当要关联所有的仓库时, 可以使用 "*" 表示; 当要关联除某一个仓库以外的其他所有仓库时, 可以表示为 "*,!repositoryId"; 当要关联不是 localhost 或用 file 请求的仓库时, 可以表示为 "external:*".
l url: 表示该镜像的 url. 当 Maven 在建立系统的时候就会使用这个 url 来连接到我们的远程仓库.
profiles: 用于指定一系列的 profile.profile 元素由 activation,repositories,pluginRepositories 和 properties 四个元素组成. 当一个 profile 在 settings.xml 中是处于活动状态并且在 pom.xml 中定义了一个相同 id 的 profile 时, settings.xml 中的 profile 会覆盖 pom.xml 中的 profile.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <profiles>
- <profile>
- <id>jdk-1.4</id>
- <activation>
- <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>
- t;/profile>
- </profiles>
activation: 这是 profile 中最重要的元素. 跟 pom.xml 中的 profile 一样, settings.xml 中的 profile 也可以在特定环境下改变一些值, 而这些环境是通过 activation 元素来指定的.
在上面这段代码中, 当所有的约束条件都满足的时候就会激活这个 profile.
jdk: 表示当 jdk 的版本满足条件的时候激活, 在这里是 1.4. 这里的版本还可以用一个范围来表示, 如
<jdk>[1.4,1.7]</jdk > 表示 1.4,1.5,1.6 和 1.7 满足;
activeProfiles: 底包含一系列的 activeProfile 元素, 表示对于所有的 pom 都处于活跃状态的 profile.
- [html] view plain https://blog.csdn.net/xuexiaoxu1990/article/details/52882664# copy https://blog.csdn.net/xuexiaoxu1990/article/details/52882664#
- <activeProfiles>
- <activeProfile>alwaysActiveProfile</activeProfile>
- <activeProfile>anotherAlwaysActiveProfile</activeProfile>
- </activeProfiles>
来源: http://www.bubuko.com/infodetail-2611720.html