Maven配置之settings.xml

在Maven安装目录下有一个settings.xml文件( M2HOME/conf/settings.xmlMaven /.m2settings.xml {user.home}/.m2/settings.xml),该文件的配置只对当前用户有效(如果~/.m2目录下没有settings文件,可以将安装目录下的配置文件复制到该目录下)。
settings.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>D:\Maven\repository</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>

常用到的几个配置项,简单说明下:
localRepository:表示本地库的保存位置,也就是maven主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
interactiveMode:如果Maven需要和用户交互以获得输入,则设置成true,反之则应为false。默认为true。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
usePluginRegistry:如果需要让Maven使用文件plugin-registry.xml来管理插件版本,则设为true。默认为false。
profiles: 项目构建的配置信息,这里会有单独说明。
activeProfiles:激活的profile列表,按顺序生效。
pluginGroups: 如果插件groupId未指明,按该列表下的id去查找。
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>

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

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

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>
  • id:是用来区别mirror的,所有的mirror不能有相同的id
  • mirrorOf:用来表示该mirror是关联的哪一个仓库,其值为其关联仓库的id。当要同时关联多个仓库时,这多个仓库之间可以用逗号隔开;当要关联所有的仓库时,可以使用“”表示;当要关联除某一个仓库以外的其他所有仓库时,可以表示为“,!repositoryId”;当要关联不是localhost或用file请求的仓库时,可以表示为“external:*”。
  • url:表示该镜像的url。当Maven在建立系统的时候就会使用这个url来连接到我们的远程仓库。

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。这里的版本还可以用一个范围来表示,如
    [1.4,1.7)表示1.4、1.5和1.6满足;
    [1.4,1.7]表示1.4、1.5、1.6和1.7满足;
  • os:表示当操作系统满足条件的时候激活。
  • property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。

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

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

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

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

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

  • file:表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。如下面的例子表示当文件hello/world不存在的时候激活该profile。
<profile>
       <activation>
              <file>
                     <missing>hello/world</missing>
              </file>
       </activation>
</profile>
  • activeByDefault:当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该profile将自动被激活。
    1.2properties:用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性都可以在pom.xml中使用。
    1.3 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。

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

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

  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaxiaoli_2013

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值