Maven讲解之 setting.xml

来源:互联网 发布:装修手机淘宝店铺首页 编辑:程序博客网 时间:2024/06/02 01:58

Maven讲解之 setting.xml

通常我们安装了Maven之后会对Maven setting.xml(Maven的配置文件做一些自定义),本章我们将详细地学习一下Maven setting.xml文件。

  • 文件位置
    一般存在与Maven安装的 Root File Path 的conf文件夹下:
    e.g.:
    这里写图片描述

  • setting.xml 概览

    <?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/>    <interactiveMode/>    <offline/>    <pluginGroups/>    <servers/>    <mirrors/>    <proxies/>    <profiles/>    <activeProfiles/></settings>

localRepository(Maven仓库)

Maven repository 用于存放项目中应用的jar包,通常我们有一下两种做法设置Maven的仓库。

  • 出厂默认设置
    即不配置Maven Repository的任何信息,默认使用 ${user.home}/.m2/repository/setting.xml

  • 自定义仓库

    <localRepository>E:\WorkSpace\IDEs\Maven\MavenRepository</localRepository>

interactiveMode

interactiveMode 用于决定maven是否在需要输出的时候提示你,默认true。如果是false,它将使用合理的默认值,或者基于一些设置。


offline

决定maven是否在构建的时候进行网络传输。 默认false,表示联网状态,true为取消联网。 在某些情况下设置为true是很有用的,比如jar无法从网上下载等。


pluginGroups
Maven组件,默认使用的是 org.apache.maven.plugins所有组件。在Maven 管理项目的生命周期时会用到plugin。一下为Maven常用的plugins
这里写图片描述

  • Default:默认使用Maven的插件。建议
  • Customized:自定义使用插件。不建议
    • <pluginGroup>com.your.plugins</pluginGroup>

proxies

此项用于设置http代理 有时候由于安全问题,需要配置http代理,通过代理服务才能正常访问外部仓库下载资源可以ping repo1.maven.org来访问中央仓库 telnet 218.14.227.197 3128 来查看代理地址以及端口是否畅通

<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>
  • id:proxy的唯一标识,用来区别proxy元素。
  • active:表示是否激活代理,如果配置多个,默认是第一个生效
  • username,password:提供连接代理服务器时的认证。
  • host,port:主机地址,端口号
  • nonProxyHosts:用来表示哪些主机名不需要代理,可以用|来分 割多个,此外也支持通配符,
    如:*.goole.com表示所有以goole.com结尾的都不需要通过代理

  • Default:

  • Customized:使用的不同的protocol 获取资源,可以配置多个proxy,顺序加载读取。

servers

这是一个认证配置的列表,根据系统中使用的server-id控制。认证配置在maven连接到远程服务时使用。

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

可以配置多个Server 顺序读取。


mirrors

指定镜像仓库位置用于从远程仓库下载资源

  <mirrors>  <mirror>        <id>nexus-aliyun</id>        <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>        <name>Nexus aliyun</name>        <url>http://maven.aliyun.com/nexus/content/groups/public</url>    </mirror>   <mirrors>  </mirrors>

这里已Ali的镜像为例,可以配置多个镜像,顺序读取。


profiles

  • settings.xml中时意味着该profile是全局的,所以只能配置范围宽泛一点配置信息,比如远程仓库等。而一些比较细致一点的需要定义在项目的pom.xml中。

  • profile可以让我们定义一系列的配置信息,然后指定其激活条件。 根据每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置。

  • 例子:通过profile定义jdk1.5以上使用一套配置,jdk1.5以下使用另外一套配置;或者通过操作系统来使用不同的配置信息。

  • settings.xml中的信息有repositories、pluginRepositories和properties。定义在properties的值可以在pom.xml中使用。

<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>
  • jdk:检测到对应jdk版本就激活
  • os:针对不同操作系统
  • property:当maven检测到property(pom中如${name}这样的)profile将被激活
  • file:如果存在文件,激活,不存在文件激活

通过以下命令查看哪些profile将生效
- mvn help:active-profiles


activeProfiles

每个activeProfile元素对应一个profile id的值,任何profile id被定义到activeProfile的profile将被激活。

<activeProfiles>    <activeProfile>alwaysActiveProfile</activeProfile>    <activeProfile>anotherAlwaysActiveProfile</activeProfile></activeProfiles>