Maven中settings.xml的配置项说明

一.Maven的setting配置文件 和 在Eclipse中对Maven的正确配置。

1.Maven的配置文件(Maven的安装目录/conf/settings.xml ) 和 Maven仓库下(默认的Maven仓库的是用户家目录下的.m2文件,能够另行制定)的settings.xml文件

在Maven中提供了一个settings.xml文件来定义Maven的全局环境信息。这个文件会存在于Maven的安装目录的conf子目录下面,或者是用户家目录的.m2子目录下面。咱们能够经过这个文件来定义本地仓库、远程仓库和联网使用的代理信息等。apache

其实相对于多用户的PC机而言,在Maven安装目录的conf子目录下面的settings.xml才是真正的全局的配置。而用户家目录的.m2子目录下面的settings.xml的配置只是针对当前用户的服务器

当这两个文件同时存在的时候,那么对于相同的配置信息用户家目录下面的settings.xml中定义的会覆盖Maven安装目录下面的settings.xml中的定义。maven

用户家目录下的settings.xml文件通常是不存在的,可是Maven容许咱们在这里定义咱们本身的settings.xml,若是须要在这里定义咱们本身的settings.xml的时候就能够把Maven安装目录下面的settings.xml文件拷贝到用户家目录的.m2目录下,而后改为本身想要的样子。编辑器

2.在Eclipse中正确的配置Maven(此处个人Maven仓库由默认的用户家目录下的.m2文件改为了F:\Development\m2\repository)

博客园编辑器对图片的显示有压缩,看高清大图请点击:http://images2017.cnblogs.com/blog/610238/201710/610238-20171014114125668-433293838.pngthis

 

二.settings.xml文件中对应的配置项

 先来看看一个基本的settings.xml文件中的内容:(若是使用默认的settings.xml文件不少配置多都注释掉了,只有一个<localRepository>,此处该settings.xml文件进行了一些其余配置)url

<?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>D:\\develop\\mavenRepository</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>
  </servers>
 
  <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.5</id>
      <activation>
        <jdk>1.5</jdk>
      </activation>
      <repositories>
        <repository>
          <id>jdk15</id>
          <name>jdk1.5</name>
          <url>http://www.myhost.com/maven/jdk15</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>jdk-1.5</activeProfile>
  </activeProfiles>
</settings>

settings.xml中主要包括如下元素:

1.localRepository

表示Maven用来在本地储存信息的本地仓库的目录。默认是用户家目录下面的.m2/repository目录。spa

2.interactiveMode

表示是否使用交互模式,默认是true;若是设为false,那么当Maven须要用户进行输入的时候,它会使用一个默认值。操作系统

3.offline

表示是否离线,默认是false。这个属性表示在Maven进行项目编译和部署等操做时是否容许Maven进行联网来下载所须要的信息。.net

4.pluginGroups

在pluginGroups元素下面能够定义一系列的pluginGroup元素。表示当经过plugin的前缀来解析plugin的时候到哪里寻找。pluginGroup元素指定的是plugin的groupId。默认状况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。插件

5.proxies

其下面能够定义一系列的proxy子元素,表示Maven在进行联网时须要使用到的代理。当设置了多个代理的时候第一个标记active为true的代理将会被使用。下面是一个使用代理的例子:

<proxies>
  <proxy>
      <id>xxx</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>用户名</username>
      <password>密码</password>
      <host>代理服务器地址</host>
      <port>代理服务器的端口</port>
      <nonProxyHosts>不使用代理的主机</nonProxyHosts>
  </proxy>
</proxies>

6.servers

其下面能够定义一系列的server子元素,表示当须要链接到一个远程服务器的时候须要使用到的验证方式。这主要有username/password和privateKey/passphrase这两种方式。如下是一个使用servers的示例:

  <servers>
    <server>
      <id>id</id>
      <username>用户名</username>
      <password>密码</password>
    </server>
  </servers>

7.mirrors

用于定义一系列的远程仓库的镜像。咱们能够在pom中定义一个下载工件的时候所使用的远程仓库。可是有时候这个远程仓库会比较忙,因此这个时候人们就想着给它建立镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。每一个远程仓库都会有一个id,这样咱们就能够建立本身的mirror来关联到该仓库,那么之后须要从远程仓库下载工件的时候Maven就能够从咱们定义好的mirror站点来下载,这能够很好的缓解咱们远程仓库的压力。在咱们定义的mirror中每一个远程仓库都只能有一个mirror与它关联,也就是说你不能同时配置多个mirror的mirrorOf指向同一个repositoryId。

看如下是一个使用mirrors的例子:

<mirrors>
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>定义一个容易看懂的名称 </name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
</mirrors>

  1> id:

  是用来区别mirror的,全部的mirror不能有相同的id

  2> mirrorOf:

   用来表示该mirror是关联的哪个仓库,其值为其关联仓库的id。当要同时关联多个仓库时,这多个仓库之间能够用逗号隔开;当要关联全部的仓库时,可使用“*”表示;当要关联除某一个仓库之外的其余全部仓库时,能够表示为“*,!repositoryId”;当要关联不是localhost或用file请求的仓库时,能够表示为“external:*”。

  3> url:

  表示该镜像的url。当Maven在创建系统的时候就会使用这个url来链接到咱们的远程仓库。

8.profiles

用于指定一系列的profile。profile元素由activation、repositories、pluginRepositories和properties四个元素组成。当一个profile在settings.xml中是处于活动状态而且在pom.xml中定义了一个相同id的profile时,settings.xml中的profile会覆盖pom.xml中的profile。

  1> activation:

  这是profile中最重要的元素。跟pom.xml中的profile同样,settings.xml中的profile也能够在特定环境下改变一些值,而这些环境是经过activation元素来指定的。

       看下面一个例子:

  <profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.6</jdk>
        <os>
          <name>Windows 7</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>

在上面这段代码中,当全部的约束条件都知足的时候就会激活这个profile。

jdk:表示当jdk的版本知足条件的时候激活,在这里是1.6。这里的版本还能够用一个范围来表示,如

<jdk>[1.4,1.7)</jdk>表示1.四、1.5和1.6知足;

<jdk>[1.4,1.7]</jdk>表示1.四、1.五、1.6和1.7知足;

os:表示当操做系统知足条件的时候激活。

property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。

①下面的示例表示当存在属性hello的时候激活该profile。

<property>
       <name>hello</name>
</property>

②下面的示例表示当属性hello的值为world的时候激活该profile。

<property>
       <name>hello</name>
       <value>world</value>
</property>

这个时候若是要激活该profile的话,能够在调用Maven指令的时候加上参数hello并指定其值为world,如:mvn compile –Dhello=world

  2>file:

  表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。以下面的例子表示当文件hello/world不存在的时候激活该profile。

<profile>
       <activation>
              <file>
                     <missing>hello/world</missing>
              </file>
       </activation>
</profile>

  3>activeByDefault:

  当其值为true的时候表示若是没有其余的profile处于激活状态的时候,该profile将自动被激活。

  4>properties:

  用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性均可以在pom.xml中使用。

  5>repositories:

  用于定义远程仓库的,当该profile是激活状态的时候,这里面定义的远程仓库将做为当前pom的远程仓库。

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

  releases、snapshots:这是对于工件的类型的限制。

  enabled:表示这个仓库是否容许这种类型的工件

  updatePolicy:表示多久尝试更新一次。可选值有always、daily、interval:minutes(表示每多久更新一次)和never。

  checksumPolicy:当Maven在部署项目到仓库的时候会连同校验文件一块儿提交,checksumPolicy表示当这个校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。

       6>pluginRepositories:

  在Maven中有两种类型的仓库,一种是存储工件的仓库,另外一种就是存储plugin插件的仓库。pluginRepositories的定义和repositories的定义相似,它表示Maven在哪些地方能够找到所须要的插件。

9.activeProfiles

底包含一系列的activeProfile元素,表示对于全部的pom都处于活跃状态的profile。如:

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

 本文部分引用自:http://haohaoxuexi.iteye.com/blog/1827778

相关文章
相关标签/搜索