官网

Settings Reference(设置参考)

setting.xml文件中的settings元素包含用于定义值values的元素,这些值以不同方式配置Maven的执行,比如pom .xml,但不应该捆绑到任何特定项目。这些值包括本地存储库位置、备用远程存储库服务器和身份验证信息等。
有两个位置可以放置settings.xml文件:

  • The Maven install(maven安装目录): ${maven.home}/conf/settings.xml
  • A user’s install(当前登录用户目录): ${user.home}/.m2/settings.xml

前一个settign.xml也称为全局设置,后一个setting.xml称为用户设置。如果这两个文件都存在,它们的内容将合并,以用户特定的setting.xml为主导。
提示:如果需要从头创建特定于用户的设置,最简单的方法是将全局设置从Maven的安装目录/conf/setting.xml复制到${user.home}/.m2目录。Maven的默认setting.xml是一个带有注释和示例的模板,因此您可以快速调整它以满足您的需求。

下面是设置中顶部元素的概述:

  <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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository/>
      <interactiveMode/>
      <offline/>
      <pluginGroups/>
      <servers/>
      <mirrors/>
      <proxies/>
      <profiles/>
      <activeProfiles/>
    </settings>

可以使用以下表达式对settings.xml的内容进行修改

  • ${user.home} 和全部系统属性 (since Maven 3.0)
  • ${env.HOME} 环境变量

Settings Details(设置详情)

简单的值

一半的顶级settings元素是简单的值,它们表示描述构建系统中活动的元素的值范围。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>${user.home}/.m2/repository</localRepository>
  <interactiveMode>true</interactiveMode>
  <offline>false</offline>
  ...
</settings>
  • localRepository:该值是此构建系统的本地存储库的路径。默认值为${user.home}/.m2/repository。
  • interactiveMode:如果Maven尝试与用户交互以进行输入,则为true,否则为false。 默认值为true。
  • Offline:如果构建系统以脱机模式运行,则为true,默认为false。 此元素对于由于网络设置或安全原因而不能连接到远程存储库的构建服务器非常有用。

Plugin Groups(插件组)

这个元素包含一个pluginGroup元素列表,每个元素包含一个groupId。 当要使用插件且命令行中没有提供groupId时,将搜索该列表。 这个列表自动包含org.apache.maven.plugins和org.codehaus.mojo。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <pluginGroups>
    <pluginGroup>org.eclipse.jetty</pluginGroup>
  </pluginGroups>
  ...
</settings>

例如,给定上述设置,Maven命令行可能会执行org.eclipse.jetty:jetty-maven-plugin:run使用截断命令运行:

mvn jetty:run

servers(服务器)

用于下载和部署的存储库由POM的repositories和distributionManagement元素定义。 但是,用户名和密码等某些身份设置不应该与pom.xml一起发布。 这类信息应该存在于构建服务器的settings.xml中。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <servers>
    <server>
      <id>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>
  ...
</settings>
  • id:这是服务器的ID(不是要作为用户登录的ID),用来匹配Maven试图连接到的存储库/镜像的id元素。
  • username, password:这些元素以一对的形式出现,表示向该服务器进行身份验证所需的登录名和密码。
  • privateKey, passphrase:与前面两个元素一样,这对元素指定私钥(默认为${user.home}/.ssh/id_dsa)的路径和一个passphrase(如果需要的话)。 passphrase和password元素将来可能会外部化,但目前它们必须在settings.xml文件中以明文方式设置。
  • filePermissions、directoryPermissions:文件、目录被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限(permission)。这两个元素合法的值是一个三位数字,其对应了unix文件系统的权限,如664,或者775。
    注意:如果使用私钥登录服务器,请确保忽略元素。 否则,该键将被忽略。

Mirrors(镜像)

用于定义一系列的远程仓库的镜像(mirros)。我们可以在pom.xml中定义一个下载工件的时候所使用的远程仓库。但是有时候这个远程仓库会比较忙,所以给它创建镜像mirros以缓解远程仓库的压力,把对远程仓库的请求转换到对其镜像地址的请求。每个远程仓库都会有一个id,可以在自己创建的mirror中通过mirrorOf指向此id来关联到该仓库,那么以后需要从远程仓库下载工件的时候Maven就可以从我们定义好的mirror站点来下载,这可以很好的缓解我们远程仓库的压力。每个远程仓库都只能有一个mirror与它关联,也就是说你不能同时配置多个mirror的mirrorOf指向同一个repositoryId。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <mirrors>
    <mirror>
      <id>planetmirror.com</id>
      <name>PlanetMirror Australia</name>
      <url>http://downloads.planetmirror.com/pub/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>
  • id, name:镜像的唯一标识和对用户友好的名称。id用于区分镜像元素,并在连接到镜像时从节中挑选相应的凭据。
  • url:这个镜像的基本url。 构建系统将使用此URL连接到存储库,而不是默认的存储库URL。
  • mirrorOf:作为镜像的存储库id。 例如,要指向Maven中央存储库(https://repo.maven.apache.org/maven2/)的一个镜像,请将此元素设置为central。这必须和中央仓库的id完全一致。

proxies(代理)

有时候网络不能直接访问外网,比如要访问国外的中央仓库, 需要设置代理。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <proxies>
    <proxy>
      <id>myproxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.somewhere.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    </proxy>
  </proxies>
  ...
</settings>
  • id:此代理的唯一标识符。 这用于区分不同的代理。
  • active:如果此代理是活动的,则为true。 这对于声明一组代理很有用,但一次只能有一个代理是活动的。
  • protocol, host, port:代理的url(protocol://host:port),分离成离散的元素。
  • username,password:这些元素以一对的形式出现,表示向代理服务器进行身份验证所需的登录名和密码。
  • nonProxyHosts:这是一个不应该被代理的主机列表。 列表的分隔符是所期望的代理服务器类型; 上面的例子是管道分隔-逗号分隔也是常见的。

Profiles(配置文件)

作用:根据环境参数来调整maven管理的所有项目的配置。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了id、activation、repositories、pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。
如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同idprofile。当所有的约束条件都满足的时候就会激活这个profile。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <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>
  ...
</settings>

激活发生在所有指定的条件都满足时,尽管不是一次需要所有条件。

  • Activation:自动触发profile的条件逻辑。这是profile中最重要的元素。跟pom.xml中的profile一样,settings.xml中的profile也可以在特定环境下改变一些值,而这些环境是通过activation元素来指定的。activation元素并不是激活profile的唯一方式。settings.xml文件中的元素可以包含profile的id。profile也可以通过在命令行,使用-P选项和id(可以是逗号分隔的列表)来显式的激活(如,-P test)。
  • activeByDefault:当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该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。
    当存在属性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

  • file:表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。如下面例子表示当文件D:/test.txt不存在的时候激活该profile。
<profile>
    <activation>
        <file>
            <missing>D:/test.txt</missing>
        </file>
    </activation>
</profile>

(一)Activation(profiles之Activation)

激活Activation是配置文件的关键。 像POM的配置文件(pom.xml)一样,配置文件的强大之处在于它只有在特定的情况下才能修改某些值; 这些情况是通过激活元素指定的。

(二)Properties(profiles之Properties)

Maven properties是值占位符,就像Ant中的属性一样。 它们的值可以通过使用符号${X}在POM中的任何位置访问,其中X是属性。 它们有五种不同的风格,都可以从settings.xml文件中访问:

  • env.X: 在一个变量前加上"env."的前缀,返回一个shell环境变量。例如,"env.PATH"指代了$path环境变量(在Windows上是%PATH%)。
  • project.x:指代了POM中对应的元素值。例如: 1.0通过${project.version}获得version的值。
  • settings.x: 指代了settings.xml中对应元素的值。例如:false通过 ${settings.offline}获得offline的值。
  • Java System Properties: 所有可通过java.lang.System.getProperties()访问的属性都能在POM中使用该形式访问,例如 ${java.home}。
  • x: 在元素中,或者外部文件中设置,以${someVar}的形式使用。
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <profiles>
    <profile>
      ...
      <properties>
        <user.install>${user.home}/our-project</user.install>
      </properties>
      ...
    </profile>
  </profiles>
  ...
</settings>

注:如果该profile被激活,则可以在pom.xml中使用${user.install} 。

(三)Repositories(profiles之Repositories)

当该profile是激活状态的时候,这里面定义的远程仓库将作为当前pom的远程仓库。它是maven用来填充构建系统本地仓库所使用的一组远程仓库。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <profiles>
    <profile>
      ...
      <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>
      <pluginRepositories>
        <pluginRepository>
          <id>myPluginRepo</id>
          <name>My Plugins repo</name>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <url>https://maven-central-eu....com/maven2/</url>
        </pluginRepository>
      </pluginRepositories>
      ...
    </profile>
  </profiles>
  ...
</settings>
  • releases、snapshots:这发布或快照两种类型工件的策略。 POM可以在单个存储库中独立地更改每种类型。 例如,可能出于开发目的,决定只支持快照下载。
  • enabled:表示这个仓库是否允许这种类型(releases、snapshots)的工件/这个存储库是否为相应的类型(releases、snapshots)启用
  • updatePolicy:此元素指定应尝试进行更新的频率, Maven将比较本地POM和远程POM的时间戳(存储在仓库的maven-metadate 文件中)。这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。
  • checksumPolicy:当Maven在部署项目到仓库的时候会连同校验文件一起提交,checksumPolicy表示当这个校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。
  • layout: 用于定位和排序构件的仓库布局类型-可以是default(默认)或者legacy(遗留)。Maven 2为其仓库提供了一个默认的布局;然而,Maven 1.x有一种不同的布局。我们可以使用该元素指定布局是default(默认)还是legacy(遗留)。

(四)Plugin Repositories(profiles之Plugin Repositories)

在Maven中有两种类型的仓库,一种是存储工件的仓库,另一种就是存储plugin插件的仓库。pluginRepositories的定义和repositories的定义类似,它表示Maven在哪些地方可以找到所需要的插件。和repository类似,只是repository是管理jar包依赖的仓库,pluginRepositories则是管理插件的仓库。maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。

<pluginRepositories>
  <!-- 包含需要连接到远程插件仓库的信息.参见profiles/profile/repositories/repository元素的说明 -->
  <pluginRepository>
    <releases>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </releases>
    <snapshots>
      <enabled />
      <updatePolicy />
      <checksumPolicy />
    </snapshots>
    <id />
    <name />
    <url />
    <layout />
  </pluginRepository>
</pluginRepositories>

Active Profiles(激活配置文件)

手动激活profiles的列表,按照profile被应用的顺序定义activeProfile。
该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile的id。任何在activeProfile中定义的profile的id,不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。
例如,env-test是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。

<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <activeProfiles>
    <activeProfile>env-test</activeProfile>
  </activeProfiles>
</settings>
posted on 2021-07-16 17:48  哑吧  阅读(2092)  评论(0编辑  收藏  举报