简单明了的maven 配置文件详解和示例-[settings.xml]
直接上我的配置,有哪些说明不对的地方希望各位童学指正
示例配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
settings.xml是maven的全局配置文件。而pom.xml文件是所在项目的局部配置。
Settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。
在线搜索添加Jar组件"坐标"
阿里云jar组件"坐标"搜索: http://maven.aliyun.com/nexus/
将找到组件"坐标" 复制到pom.xml文件中, 保存pom.xml文件时候自动下载jar组件.下载jar组件自动保存在本地库
编译:右击项目》点击Debug 》点击 Maven install进行编译,编译成功后入图(要出现success字样编译才算成功
打包:右击项目》点击Debug as》点击 Maven build ,在弹出的对话框中在Goals中输入clean compile package,然后点击debug等到完成
上传配置的远程仓库:在项目的根目录下在命令行下执行命令:mvn clean deploy,或通过IDE或eclipse执行命令 clean deploy
【依赖的匹配下载顺序:
1.pom.xml 的profiles配置
2.settings.xml 的profiles配置
3.settings.xml 的mirrors配置
4.默认的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 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 构建系统本地仓库的路径。 -->
<localRepository>D:\Maven\repository</localRepository>
<!-- interactiveMode作用:表示maven是否需要和用户交互以获得输入。 默认为true。-->
<interactiveMode>true</interactiveMode>
<!-- offline表示maven是否需要在离线模式下运行。 默认为false。构建服务器不能连接远程仓库的时,可用-->
<offline>false</offline>
<!-- pluginGroups作用:当插件(依赖包)的组织id(groupId)没有显式提供时,供搜寻插件(依赖包)组织Id(groupId)的列表。-->
<pluginGroups>
<!--plugin的组织Id(groupId)
<pluginGroup>com.your.plugins</pluginGroup> -->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<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
作用:一般,仓库的下载和部署是在pom.xml文件中的repositories和distributionManagement元素中定义的。
一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在pom.xml文件中配置,这些信息可以配置在settings.xml中。
-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!--server服务器元素包含配置服务器时需要的信息
id :这是server的id(注意不是用户登陆的id),该id与distributionManagement中repository元素的id相匹配。
username:鉴权用户名。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。
password:鉴权密码 。鉴权用户名和鉴权密码表示服务器认证所需要的登录名和密码。密码加密功能已被添加到2.1.0 +。详情请访问密码加密页面
privateKey:鉴权时使用的私钥位置。和前两个元素类似,私钥位置和私钥密码指定了一个私钥的路径(默认是${user.home}/.ssh/id_dsa)以及如果需要的话
passphrase:鉴权时使用的私钥密码。
filePermissions:文件被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限,如:664
directoryPermissions:目录被创建时的权限。如:775
-->
<!-- 例子:
<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>
</server>
-->
<!-- xxx私服配置-->
<server>
<id>xxx-distribute-releases</id>
<username>xxxuser</username>
<password>xxxpwd</password>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
<server>
<id>xxx-distribute-snapshots</id>
<username>xxxuser</username>
<password>xxxpwd</password>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>
<!-- mirrors 作用:为仓库列表配置的下载镜像列表。
mirrorOf标签里面放置的是要被镜像的Repository ID。为了满足一些复杂的需求,Maven还支持更高级的镜像配置:
<mirrorOf>*</mirrorOf>
匹配所有仓库请求,即将所有的仓库请求都转到该镜像上
<mirrorOf>repo1,repo2</mirrorOf>
将仓库repo1和repo2的请求转到该镜像上,使用逗号分隔多个远程仓库。
<mirrorOf>*,!repo1</miiroOf>
匹配所有仓库请求,repo1除外,使用感叹号将仓库从匹配中排除。
为指定id的远程仓库做镜像。以后向这个ID的仓库发的请求都会发到这个镜像地址-->
<mirrors>
<!-- mirror指定仓库的下载镜像,不配置mirror时使用maven的中央库。
id:该镜像的唯一标识符。id用来区分不同的mirror元素
name:镜像名称
url:该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。
mirrorOf:该镜像的服务器的id。例如,如果我们要设置了一个Maven中央仓库(http://repo.maven.apache.org/maven2/)的镜像,
就需要将该元素设置成central。这必须和中央仓库的id central完全一致。
【
* :匹配所有远程仓库。
external:* 匹配所有远程仓库,使用localhost的除外,使用file://协议的除外。也就是说,匹配所有不在本机上的远程仓库。
repo1,repo2: 匹配仓库repo1和repo2,使用逗号分隔多个远程仓库。
*,!repo1: 匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。 】
-->
<!-- 仓库镜像按配置顺序匹配
maven默认仓库的搜索顺序如下:
本地仓库> settings_profile_repo > pom_profile_repo > pom_repositories > settings_mirror > central
-->
<!--mirrorOf说明
mirrorOf=* //mirror一切,配置的所有repository(仓库)不起作用了
mirrorOf=仓库id //如mirrorOf=xxx-releases,配置的xxx-releases仓库(repository)不起作用了
mirrorOf=*,!xxx-releases //!表示非运算,排除配置的xxx-releases仓库(repository),其他仓库都被镜像了。
就是请求下载xxx-releases的仓库的jar不使用mirror的url下载,其他都是用mirror配置的url下载
mirrorOf=external:* //如果本地库存在就用本地库的,如果本地没有所有下载就用mirror配置的url下载
-->
<!-- 使用xxx仓库镜像,
本地库存在就用本地库的,排除xxx-releases,xxx-snapshot,xxx-3rd-party
其他都使用http://repo.xxx***.com/repository/maven-public/下载
-->
<mirror>
<id>xxx_mirror</id>
<url>http://repo.xxx***.com/repository/maven-public/</url>
<mirrorOf>external:*,!xxx-releases,!xxx-snapshot,!xxx-3rd-party</mirrorOf>
</mirror>
<mirror>
<id>xxx_release</id>
<url>http://repo.xxx***.com/repository/xxx-releases/</url>
<mirrorOf>xxx-releases</mirrorOf>
</mirror>
<mirror>
<id>xxx_snapshot</id>
<url>http://repo.xxx***.com/repository/xxx-snapshots/</url>
<mirrorOf>xxx-snapshot</mirrorOf>
</mirror>
<mirror>
<id>xxx_3rd_party</id>
<url>http://repo.xxx***.com/repository/xxx-3rd-party/</url>
<mirrorOf>xxx-3rd-party</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>external:*,!central,!spring,!apache-snapshots,!google,!grails-core,!spring-plugin,!gradle-plugin,!xxx-releases,!xxx-snapshot,!xxx-3rd-party</mirrorOf>
</mirror>
<!-- xxx_mirror不存在时使用,本地库存在就用本地库的,排除定义的阿里仓库,和私服xxx-releases,xxx-snapshot,xxx-3rd-party仓库
其他都使用阿里聚合库https://maven.aliyun.com/repository/public下载
-->
</mirrors>
<!-- profiles 作用:【构建配置】根据环境参数来调整构建配置的列表。【就是用来调整不同的构建配置,比如调整远程仓库列表,插件仓库列表,扩展属性列表等】
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories和 properties元素。
这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),
而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。
profile的生效与否不仅可以在这里配置,同时可以在命令行通过 -P profileid使其生效
-->
<profiles>
<!-- profile
id:profile的唯一标识
activation:自动触发profile的条件逻辑, 多个条件的关系是AND的关系
properties:扩展属性列表
repositories:远程仓库列表
pluginRepositories:插件仓库列表
-->
<!-- 例:
<profile>
<id>test</id>
<activation />
<properties />
<repositories />
<pluginRepositories />
</profile>
-->
<!--profile的activation例子
<activation>
#profile默认是否激活的标识
<activeByDefault>false</activeByDefault>
#当匹配的jdk被检测到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4开头的JDK
<jdk>1.5</jdk>
#当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。
<os>
#激活profile的操作系统的名字
<name>Windows XP</name>
#激活profile的操作系统所属家族(如 'windows')
<family>Windows</family>
#激活profile的操作系统体系结构
<arch>x86</arch>
#激活profile的操作系统版本
<version>5.1.2600</version>
</os>
#如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),其拥有对应的name = 值,Profile就会被激活。如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段
<property>
#激活profile的属性的名称
<name>mavenVersion</name>
#激活profile的属性的值
<value>2.0.3</value>
</property>
#提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。另一方面,exists则会检查文件是否存在,如果存在则激活profile。
<file>
#如果指定的文件存在,则激活profile。 ${basedir}指的是pom文件所在的目录
<exists>${basedir}/file2.properties</exists>
#如果指定的文件不存在,则激活profile。
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
【maven profile的激活方式:
1.使用mvn ... -Pxxx指令,指定激活id为xxx的profile
2.在profile里面使用activation标签,当满足条件时激活该profile
3.使用mvn ... -Dxxx=value指定参数激活
】
-->
<!--profile的properties例子 , 作用:对应profile的扩展属性列表。
maven属性和ant中的属性一样,可以用来存放一些值。这些值可以在pom.xml中的任何地方使用标记${X}来使用,
这里X是指属性的名称。属性有五种不同的形式,并且都能在settings.xml文件中访问。
1. env.X: 在一个变量前加上"env."的前缀,会返回一个shell环境变量。例如,"env.PATH"指代了$path环境变量(在Windows上是%PATH%)。
2. project.x:指代了POM中对应的元素值。例如: <project><version>1.0</version></project>通过${project.version}获得version的值。
3. settings.x: 指代了settings.xml中对应元素的值。例如:<settings><offline>false</offline></settings>通过 ${settings.offline}获得offline的值。
4. Java System Properties: 所有可通过java.lang.System.getProperties()访问的属性都能在POM中使用该形式访问,例如 ${java.home}。
5. x: 在<properties/>元素中,或者外部文件中设置,以${someVar}的形式使用。
如:
<properties>
<user.install>${user.home}/our-project</user.install>
</properties>
-->
<!--Repositories 作用:远程仓库列表,它是maven用来填充构建系统本地仓库所使用的一组远程仓库。
id:远程仓库唯一标识
name:远程仓库名称
releases|snapshots:如何处理远程仓库里发布版本的下载
enabled:true或者false表示该仓库是否为下载某种类型构件(发布版(releases),快照版(snapshots))开启
updatePolicy:该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。
checksumPolicy:当Maven验证构件校验文件失败时该怎么做-ignore(忽略),fail(失败),或者warn(警告)。
url:远程仓库URL
layout:用于定位和排序构件的仓库布局类型-可以是default(默认)或者legacy(遗留)
-->
<!-- 例:
<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>
-->
<!-- pluginRepositories 作用:发现(查找)插件的远程仓库列表。
和repository类似,只是repository是管理jar包依赖的仓库,pluginRepositories则是管理插件的仓库。
maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。
每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。
-->
<!-- 例:
<pluginRepositories>
<pluginRepository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
</releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
</snapshots>
<id />
<name />
<url />
<layout />
</pluginRepository>
</pluginRepositories>
-->
<!-- 全局JDK1.8配置 -->
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>xxx-dev</id>
<activation>
<!--多个条件需要同时满足-->
<!--name:
激活profile的属性的名称,系统属性存在时激活 profile ,如可使用命令指定属性:mvn clean package -Dxxx=YES
value:
激活profile的属性的值
-->
<!--
<property>
<name>xxx</name>
<value>YES</value>
</property>
-->
<file>
<!--如果指定的文件存在,则激活profile。 -->
<exists>${basedir}/xxx.active</exists>
<!--如果指定的文件不存在,则激活profile。
<missing>${basedir}/xxx.properties</missing>
-->
</file>
</activation>
<repositories>
<repository>
<id>xxx-releases</id>
<name>xxx-releases</name>
<releases>
<enabled>true</enabled>
<!--元素updatePolicy用来配置Maven从远处仓库检查更新的频率,默认值是daily,表示Maven每天检查一次-->
<!--其他可用的值包括:never-从不检查更新;always-每次构建都检查更新;interval:X-每隔X分钟检查一次更新(X为任意整数-->
<updatePolicy>always</updatePolicy>
<!--元素checksumPolicy用来配置Maven检查校验和文件的策略。当构建被部署到Maven仓库中时,会同时部署对应的检验和文件。-->
<!--在下载构件的时候,Maven会验证校验和文件,如果校验和验证失败,当checksumPolicy的值为默认的warn时,Maven会在执行构建时输出警告信息-->
<!--其他可用的值包括:fail-Maven遇到校验和错误就让构建失败;ignore-使Maven完全忽略校验和错误-->
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://repo.xxx***.com/repository/xxx-releases/</url>
<layout>default</layout>
</repository>
<repository>
<id>xxx-snapshot</id>
<name>xxx-snapshot</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://repo.xxx***.com/repository/xxx-snapshots/</url>
<layout>default</layout>
</repository>
<repository>
<id>xxx-3rd-party</id>
<name>xxx-3rd-party</name>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://repo.xxx***.com/repository/xxx-3rd-party/</url>
<layout>default</layout>
</repository>
</repositories>
</profile>
<!-- 阿里云配置: 提高国内的jar包下载速度 -->
<profile>
<id>ali</id>
<activation>
<!--多个条件需要同时满足-->
<!--name:
激活profile的属性的名称,系统属性存在时激活 profile ,如可使用命令指定属性:mvn clean package -Dali=YES
value:
激活profile的属性的值
-->
<!--
<property>
<name>ali</name>
<value>YES</value>
</property>
-->
<file>
<!--如果指定的文件存在,则激活profile。 -->
<exists>${basedir}/ali.active</exists>
<!--如果指定的文件不存在,则激活profile。
<missing>${basedir}/xxx.properties</missing>
-->
</file>
</activation>
<repositories>
<repository>
<id>central</id>
<name>aliyun central</name>
<url>https://maven.aliyun.com/repository/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring</id>
<name>aliyun spring</name>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>apache-snapshots</id>
<name>aliyun apache-snapshots</name>
<url>https://maven.aliyun.com/repository/apache-snapshots</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>google</id>
<name>aliyun google</name>
<url>https://maven.aliyun.com/repository/google</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>grails-core</id>
<name>aliyun grails-core</name>
<url>https://maven.aliyun.com/repository/grails-core</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-plugin</id>
<name>aliyun spring-plugin</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>gradle-plugin</id>
<name>aliyun gradle-plugin</name>
<url>https://maven.aliyun.com/repository/gradle-plugin</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- activeProfiles 作用:【手动激活profiles的列表】,按照profile被应用的顺序定义activeProfile。
该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile id。任何在activeProfile中定义的profile id,
不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。
例如,env-test是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。
如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。
-->
<!--例:
<activeProfiles>
<activeProfile>xinpa-test</activeProfile>
</activeProfiles>
-->
<activeProfiles>
<activeProfile>jdk1.8</activeProfile>
<!--如要激活xxx-dev和ali,建议在项目目录下创建ali.active和xxx.active-->
<activeProfile>ali</activeProfile>
<activeProfile>xxx-dev</activeProfile>
</activeProfiles>
</settings>