请确保自己的maven环境安装成功,具体安装流程详见Maven安装

在安装完maven环境后,最重要的就是去配置settings.xml文件,其作为统一的依赖配置管理入口,了解其相关配置有助于我们对maven的理解

settings.xml主配置文件

笔者此处优先查看其注释内容,信息如下

<!--
| This is the configuration file for Maven. It can be specified at two levels:
| #用户级别,面向单个用户配置,即每个用户都可以自定义settings.xml供己方使用
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| #其中的CLI选项指的是mvn操作命令的相关参数比如:mvn -v
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
| #全局级别,即面向所有用户同一配置
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.home}/conf/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->

由上可得

  1. settings.xml具有两个级别:用户级别和全局级别,一般我们只需要去配置用户级别即可
  2. settings.xml配置的是所有POM工程项目的全局配置

主要参数配置

笔者罗列出主要的参数配置

	## 此处的构件我们简单的理解为依赖,针对JAVA就是JAR包
localRepository 本地仓库配置路径
interactiveMode 是否交互式输入提醒,默认true
offline 是否每次编译部署时候需要联网,默认false
pluginGroups 插件组合,供编译时选用
proxies 代理配置,连接本地无法远程的网络
servers 服务配置,上传构件/连接仓库所用
mirrors 替代有网络问题的repository的访问站点
profiles 应用于不同环境的配置
activeProfiles 激活指定的配置,与profile搭配使用

对上述的参数分别作下简单的描述,更多的解释可详见官网Maven-Settings Reference

localRepository

本地仓库路径,主要用于存放从远程仓库下载的构件依赖。采用绝对路径,默认存放在${user.home}/.m2/repository路径下

<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository-->

interactiveMode

交互式节点配置,默认为true。设置为false则会在mvn执行中使用默认的参数设置交互性问题

<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true-->

offline

是否每次的编译部署都需要联网,默认为false。如果构件不需要更新下载则可设置为true,以提高build效率和成功率。

<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false-->

pluginGroups

插件组,默认包含maven插件、mojo插件,用于插件在使用时没有指定groupId时,这个列表就会被搜索。

!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->

举个例子:

<pluginGroups>
<pluginGroup>
org.mortbay.jetty
</pluginGroup>
</pluginGroups>

上述配置后,可通过mvn jetty run替代长命令org.morbay.jetty:jetty-maven-plugin:run

proxies

网络代理配置

<!-- 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

上传或者下载构件时所需要验证的服务器配置。通过id来区分,主要与mirror/repository配置搭配使用

有两种方式的校验,此配置有助于用户可以搭建自己的maven私库。


密码方式校验(推荐)

 <!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<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>
-->

privateKey方式校验,更为安全的认证方式

  <servers>
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>

mirrors

构件镜像,替代有网络问题的仓库的访问站点。即当maven构件出现下载问题时便通过此配置替换原有的下载地址去下载

<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository. //central代表maven官网的中央仓库
|-->

举个例子

	<mirror>
<!--与server参数配置的id属性一致,登录校验-->
<id>${server_id}</id>
<!--拦截指定的仓库,支持多仓库id,以逗号分隔(*代表拦截所有的仓库,central代表拦截maven官方中央仓库)-->
<mirrorOf>${repository_id}/*</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<!--服务器地址-->
<url>http://my.repository.com/repo/path</url>#镜像url
</mirror>

profiles

多条件集合配置,用于不同条件下可应用不同的配置,其中也可配置自定义的repository/pluginRepository。灵活性很高

	| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<!--在此配置的任何一个节点条件满足则触发此配置-->
<activation>
<jdk>1.4</jdk>
</activation>
<!--仓库配置-->
<repositories>
<repository>
<id>jdk14</id>#唯一ID,可与server搭配使用
<name>Repository for JDK 1.4 builds</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://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>

像上述的例子,用户只需要jdk版本大于1.4,上述的profile便会生效。

activeProfiles

profile参数搭配使用,用于激活相应的profile配置,可激活多个配置

  <activeProfiles>
<activeProfile>${profile_id1}</activeProfile>
<activeProfile>${profile_id2}</activeProfile>
</activeProfiles>

总结

  1. server节点是仓库服务器的配置,有密码校验方式和私钥校验方式。其下的id配置应用于mirror/repository节点用于登录校验

  2. mirror节点配置会根据其内部的mirrorOf配置拦截指定的repository。

    如果指定为*则会拦截所有的仓库;

    指定为central则会拦截https://repo.maven.apache.org/maven2/

  3. profile节点则可由用户灵活配置,可用于不同条件下的环境配置

Maven settings.xml配置解读的更多相关文章

  1. Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    转: 详解Maven settings.xml配置(指定本地仓库.阿里云镜像设置) 更新时间:2018年12月18日 11:14:45   作者:AmaniZ   我要评论   一.settings. ...

  2. Maven——settings.xml配置

    settings.xml配置 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed ...

  3. 学习笔记——Maven settings.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  4. Maven settings.xml配置详解

    首先:Maven中央仓库的搜索全部公共jar包的地址是,http://search.maven.org/ ===Maven基础-默认中央仓库============================== ...

  5. maven settings.xml配置优化

    <?xml version="1.0" encoding="UTF-8"?> <settings> <localRepositor ...

  6. Maven项目使用Nexus作为远程仓库的settings.xml配置

    Maven项目使用Nexus作为远程仓库的settings.xml配置(转) 在自己电脑C:\Users\hanmm\.m2\下的setting.xml. 1.服务器配置 <server> ...

  7. [java][JEECG] Maven settings.xml JEECG项目初始化 RouYi settings.xml配置

    好吧一下是经验之谈,原本这些坑不应该躺的,从头看手册完全可以避免这些. 懒得整理了,看懂了就看,看不懂自己琢磨JEECG的帮助文档去,不过嘛我喜欢用Intelij IDEA,他里面都是别的IDE,不喜 ...

  8. Maven学习存档(2)——settings.xml配置

    二.settings.xml配置 2.1 原文 <?xml version="1.0" encoding="UTF-8"?> <!-- Lic ...

  9. Maven的settings.xml配置详解

    子节点详细介绍转载:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension 全局配置 ...

随机推荐

  1. Ubuntu 14.04下搭建Node.js的开发环境

    最近想找一个轻量级且支持快速开发的服务开发平台,选来选去选择了Node.js,当时有几种选择: Python + Django(用过Django,虽然开发快速,但是感觉性能并不太好). Ruby + ...

  2. Git学习之路(4)- 撤销操作、删除文件和恢复文件

    ▓▓▓▓▓▓ 大致介绍 经过前面的学习(小白学Git)已经建立了版本库,并上传了文件,这次来学习对这些文件进行基本的操作,即: ◆ 撤销操作 ◆ 删除文件 ◆ 恢复文件 我在此之前,已经将三个文件提交 ...

  3. linux文件相关的命令

    1.linux下的文件分为字符设备文件和块设备文件 2.文件的属性有读写权限.执行权限.访问时间.修改时间.状态改变时间等. 状态改变时间指修改了文件的读写权限或者所有者等操作. 3.ls -l 执行 ...

  4. java socket tcp(服务器循环检测)

    刚才看了下以前写了的代码,tcp通信,发现太简单了,直接又摘抄了一个,运行 博客:http://blog.csdn.net/zhy_cheng/article/details/7819659 优点是服 ...

  5. flex控件总结

    Flex基本控件总结 一.flex控件的分类:文本控件(text controls).数据源控件(data provider controls).菜单控件       (menu  controls) ...

  6. Java Collection 接口、Set 接口、List 接口基本操作 练习

    //////// One package Chp11; import java.util.ArrayList; import java.util.List; public class TestList ...

  7. [solr] - solr5.2.1环境搭建 - 使用tomcat做为容器

    这里忽略solr其他依赖环境的搭建,这里搭建solr5.2.1.使用Java1.7.0_17,tomcat使用6.0.36版本的. 1.下载solr压缩文件 Solr是Apache基金组织在lucen ...

  8. .net是最牛逼的开发平台没有之一

    .net是最牛逼的开发平台没有之一 .net是最牛逼的开发平台没有之一 .net是最牛逼的开发平台没有之一 .net是最牛逼的开发平台没有之一 .net是最牛逼的开发平台没有之一 .net是最牛逼的开 ...

  9. thinkjs——一个字段一种数字代表两种状态

    问题来源: 现有一张company数据表,其中有一个字段state(-2:待审核:-1:禁用:0:正常:1:会员过期:),一般而言,在前期设计数据表的时候,会将每种状态下都用一种特定的数字代表,但是这 ...

  10. web开发之Servlet 一

    因为最近在研究公司一套新的框架,发现这套框架的底层是对Struts2,Spring 封装后的WEB应用框架,而我发现如果仅仅是利用这个框架开发,确实很容易快速上手,做业务来说是没有问题的,但我觉得如果 ...