Maven全局配置文件settings.xml详解

Maven全局配置文件settings.xml详解

  • 一、概要
    • 1. settings.xml 的作用
    • 2. settings.xml 文件的位置
    • 3. 配置的优先级
  • 二、settings.xml元素详解
    • 1. 顶级元素概览
      • 1.1 LocalRepository
      • 1.2 InteractiveMode
      • 1.3 UsePluginRegistry
      • 1.4 Offline
      • 1.5 PluginGroups
      • 1.6 Servers
      • 1.7 Mirrors
      • 1.8 Proxies
      • 1.9 Profiles
        • 1.9.1 Activation
      • 1.10 ActiveProfiles
  • 三、参考链接

一、概要

1. settings.xml 的作用

它是用来设置 Maven 参数的配置文件。并且,settings.xml 是 Maven 的全局配置文件。settings.xml中包含类似本地仓库、远程仓库和联网使用的代理信息等配置。

2. settings.xml 文件的位置

settings.xml 文件一般存在于 Maven 的安装目录的 conf 子目录下面,或者是用户目录的 .m2 子目录下面。

3. 配置的优先级

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

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

用户目录下的 settings.xml 文件一般是不存在的,但是 Maven 允许我们在这里定义我们自己的 settings.xml,如果需要在这里定义我们自己的 settings.xml 的时候就可以把 Maven 安装目录下面的 settings.xml 文件拷贝到用户目录的 .m2 目录下,然后改成自己想要的样子。

二、settings.xml元素详解

1. 顶级元素概览

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

1.1 LocalRepository

  • 作用:该值表示构建系统本地仓库的路径。
  • 默认值:~/.m2/repository。
<localRepository>${user.home}/.m2/repositorylocalRepository>

1.2 InteractiveMode

  • 作用:表示 maven 是否需要和用户交互以获得输入。
  • 默认值:true。如果 maven 需要和用户交互以获得输入,则设置成 true,反之则为 false。
<interactiveMode>trueinteractiveMode>

1.3 UsePluginRegistry

  • 作用:表示 maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。
  • 默认值:false。如果需要让 maven 使用文件 ~/.m2/plugin-registry.xml 来管理插件版本,则设为 true,反之则为 false。
<usePluginRegistry>falseusePluginRegistry>

1.4 Offline

  • 作用:表示在 Maven 进行项目编译和部署等操作时是否允许 Maven 进行联网来下载所需要的信息。

  • 默认值:false。如果构建系统需要在离线模式下运行,则为 true,反之则为 false。

    当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。

<offline>falseoffline>

1.5 PluginGroups

  • 作用:在 pluginGroups 元素下面可以定义一系列的 pluginGroup 元素。表示当通过 plugin 的前缀来解析 plugin 的时候到哪里寻找。pluginGroup 元素指定的是 plugingroupId
  • 默认情况下,Maven 会自动把 org.apache.maven.pluginsorg.codehaus.mojo 添加到 **pluginGroups **下。
<pluginGroups>
    
    <pluginGroup>org.codehaus.mojopluginGroup>
pluginGroups>

1.6 Servers

  • 作用:一般,仓库的下载和部署是在 pom.xml 文件中的 repositoriesdistributionManagement 元素中定义的。然而,一般类似用户名、密码(有些仓库访问是需要安全认证的)等信息不应该在 pom.xml 文件中配置,这些信息可以配置在 settings.xml 中。

<servers>
    
    <server>
        
        <id>server001id>
        
        <username>my_loginusername>
        
        <password>my_passwordpassword>
        
        <privateKey>${usr.home}/.ssh/id_dsaprivateKey>
        
        <passphrase>some_passphrasepassphrase>
        
        <filePermissions>664filePermissions>
        
        <directoryPermissions>775directoryPermissions>
    server>
servers>

1.7 Mirrors

  • 作用:用于定义一系列的远程仓库的镜像。

    我们可以在 pom 中定义一个下载工件的时候所使用的远程仓库。但是有时候这个远程仓库会比较忙,所以这个时候人们就想着给它创建镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。

    每个远程仓库都会有一个 id,这样我们就可以创建自己的 mirror 来关联到该仓库,那么以后需要从远程仓库下载工件的时候 Maven 就可以从我们定义好的 mirror 站点来下载,这可以很好的缓解我们远程仓库的压力。

    在我们定义的 mirror 中每个远程仓库都只能有一个 mirror 与它关联,也就是说你不能同时配置多个 mirror 的 mirrorOf 指向同一个 repositoryId。

<mirrors>
    
    <mirror>
        
        <id>mirrorIdid>
        
        <name>PlanetMirror Australianame>
        
        <url>http://downloads.planetmirror.com/pub/maven2url>
        
        <mirrorOf>repositoryIdmirrorOf>
    mirror>
mirrors>

1.8 Proxies

  • 作用:用来配置不同的代理。
<proxies>
    
    <proxy>
        
        <id>myproxyid>
        
        <active>trueactive>
        
        <protocol>httpprotocol>
        
        <host>proxy.somewhere.comhost>
        
        <port>8080port>
        
        <username>proxyuserusername>
        
        <password>somepasswordpassword>
        
        <nonProxyHosts>*.google.com|ibiblio.orgnonProxyHosts>
    proxy>
proxies>

1.9 Profiles

  • 作用:根据环境参数来调整构建配置的列表。

    settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。它包含了 id、activation、repositories、pluginRepositories 和 properties元素。

    这里的 profile 元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml 文件的角色定位),而非单独的项目对象模型设置。

    如果一个 settings.xml 中的 profile 被激活,它的值会覆盖任何其它定义在 pom.xml 中带有相同 id 的 profile。当所有的约束条件都满足的时候就会激活这个 profile。

<profiles>
    <profile>
        
        <id>testid>     
        
        <activation>
            <activeByDefault>falseactiveByDefault>
            <jdk>1.6jdk>
            <os>
                <name>Windows 7name>
                <family>Windowsfamily>
                <arch>x86arch>
                <version>5.1.2600version>
            os>
            <property>
                <name>mavenVersionname>
                <value>2.0.3value>
            property>
            <file>
                <exists>${basedir}/file2.propertiesexists>
                <missing>${basedir}/file1.propertiesmissing>
            file>
        activation>
        
        <properties />
        
        <repositories />
        
        <pluginRepositories />
        ...
    profile>
profiles>

1.9.1 Activation

  • 作用:自动触发 profile 的条件逻辑。这是 profile中 最重要的元素。

    跟 pom.xml 中的 profile 一样,settings.xml 中的 profile 也可以在特定环境下改变一些值,而这些环境是通过 activation 元素来指定的。activation 元素并不是激活 profile 的唯一方式。

    settings.xml 文件中的 activeProfile 元素可以包含 profile 的 id。profile 也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。

  1. jdk:表示当 jdk 的版本满足条件的时候激活,在这里是 1.6。这里的版本还可以用一个范围来表示,如

    <jdk>[1.4,1.7)jdk> 
    <jdk>[1.4,1.7]jdk> 
    
  2. os:表示当操作系统满足条件的时候激活。

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

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

    <property>
        <name>helloname>
    property>
    

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

    <property>
        <name>helloname>
        <value>worldvalue>
    property>
    

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

    mvn compile –Dhello=world
    
  4. file:表示当文件存在或不存在的时候激活,exists 表示存在,missing 表示不存在。如下面例子表示当文件 hello/world 不存在的时候激活该 profile。

    <profile>
        <activation>
            <file>
                <missing>hello/worldmissing>
            file>
        activation>
    profile>
    
  5. activeByDefault:当其值为 true 的时候表示如果没有其他的 profile 处于激活状态的时候,该 profile 将自动被激活。

  6. properties:用于定义属性键值对的。当该 profile 是激活状态的时候,properties 下面指定的属性都可以在 pom.xml 中使用。对应 profile 的扩展属性列表。
    maven 属性和 ant 中的属性一样,可以用来存放一些值。这些值可以在 pom.xml 中的任何地方使用标记 ${X} 来使用,这里X是指属性的名称。属性有五种不同的形式,并且都能在 settings.xml 文件中访问。

    
    <properties>
        <user.install>${user.home}/our-projectuser.install>
    properties>
    

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

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

    <repositories>
        
        <repository>
            
            <id>codehausSnapshotsid>
            
            <name>Codehaus Snapshotsname>
            
            <releases>
                
                <enabled>falseenabled>
                
                <updatePolicy>alwaysupdatePolicy>
                
                <checksumPolicy>warnchecksumPolicy>
            releases>
            
            <snapshots>
                <enabled />
                <updatePolicy />
                <checksumPolicy />
            snapshots>
            
            <url>http://snapshots.maven.codehaus.org/maven2url>
            
            <layout>defaultlayout>
        repository>
    repositories>
    
    • releases、snapshots:这是对于工件的类型的限制。
    • enabled:表示这个仓库是否允许这种类型的工件
    • updatePolicy:表示多久尝试更新一次。可选值有 always、daily、interval:minutes(表示每多久更新一次)和 never。
    • checksumPolicy:当 Maven 在部署项目到仓库的时候会连同校验文件一起提交,checksumPolicy 表示当这个校验文件缺失或不正确的时候该如何处理,可选项有 ignore、fail 和 warn。
  8. pluginRepositories:在 Maven 中有两种类型的仓库,一种是存储工件的仓库,另一种就是存储 plugin 插件的仓库。

    pluginRepositories 的定义和 repositories 的定义类似,它表示 Maven 在哪些地方可以找到所需要的插件,只是 repository 是管理 jar 包依赖的仓库, pluginRepositories 则是管理插件的仓库。

    pluginRepositories 元素的结构和 repositories 元素的结构类似。每个 pluginRepository 元素指定一个 Maven 可以用来寻找新插件的远程地址。

    maven 插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。

    <pluginRepositories>
        
        <pluginRepository>
            <releases>
                <enabled />
                <updatePolicy />
                <checksumPolicy />
            releases>
            <snapshots>
                <enabled />
                <updatePolicy />
                <checksumPolicy />
            snapshots>
            <id />
            <name />
            <url />
            <layout />
        pluginRepository>
    pluginRepositories>
    

    示例:

    <activation>
        
        <activeByDefault>falseactiveByDefault>
        
        <jdk>1.5jdk>
        
        <os>
            
            <name>Windows XPname>
            
            <family>Windowsfamily>
            
            <arch>x86arch>
            
            <version>5.1.2600version>
        os>
        
        <property>
            
            <name>mavenVersionname>
            
            <value>2.0.3value>
        property>
        
        <file>
            
            <exists>${basedir}/file2.propertiesexists>
            
            <missing>${basedir}/file1.propertiesmissing>
        file>
    activation>
    

1.10 ActiveProfiles

  • 作用:手动激活 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-testactiveProfile>
    activeProfiles>
    ...
settings>

三、参考链接

  1. 原文链接:https://www.cnblogs.com/hongmoshui/p/10762272.html

你可能感兴趣的:(笔记,maven,xml)