Maven Setting 配置

818 阅读3分钟

简介

Maven 翻译为"专家"、"内行",是 Apache 下的一个纯 Java 开发的开源项目。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。

Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理。

Maven 也可被用于构建和管理各种项目,例如 C#,Ruby,Scala 和其他语言编写的项目。Maven 曾是 Jakarta 项目的子项目,现为由 Apache 软件基金会主持的独立 Apache 项目。

基本介绍可见 Maven教程 | 菜鸟教程

setting文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
 | 官方文档: https://maven.apache.org/settings.html
 |
 | Maven 提供以下两种 level 的配置:
 |
 |  1. User Level.      当前用户独享的配置, 通常在 ${user.home}/.m2/settings.xml 目录下。 
 |                      可在 CLI 命令行中通过以下参数设置:  -s /path/to/user/settings.xml
 |
 |  2. Global Level.    同一台计算机上的所有 Maven 用户共享的全局配置。 通常在${maven.home}/conf/settings.xml目录下。
 |                      可在 CLI 命令行中通过以下参数设置:  -gs /path/to/global/settings.xml
 |
 |  备注:  User Level 优先级 > Global Level
 |-->

<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">
    <!--
     | Maven 依赖搜索顺序, 当我们执行 Maven 命令时, Maven 开始按照以下顺序查找依赖的库: 
     |
     | 步骤 1 - 在本地仓库中搜索, 如果找不到, 执行步骤 2, 如果找到了则执行其他操作。
     | 步骤 2 - 在中央仓库中搜索, 如果找不到, 并且有一个或多个远程仓库已经设置, 则执行步骤 4, 如果找到了则下载到本地仓库中已被将来引用。
     | 步骤 3 - 如果远程仓库没有被设置, Maven 将简单的停滞处理并抛出错误(无法找到依赖的文件)。
     | 步骤 4 - 在一个或多个远程仓库中搜索依赖的文件, 如果找到则下载到本地仓库已被将来引用, 否则 Maven 将停止处理并抛出错误(无法找到依赖的文件)。
     |-->

    <!-- 地仓库路径, 默认值: ${user.home}/.m2/repository -->
    <localRepository>${user.home}/workspace/env/maven/repository</localRepository>

    <!-- 当 maven 需要输入值的时候, 是否交由用户输入, 默认为true;false 情况下 maven 将根据使用配置信息进行填充 -->
    <interactiveMode>true</interactiveMode>

    <!-- 是否支持联网进行 artifact 下载、 部署等操作, 默认: false -->
    <offline>false</offline>

    <!-- 
     | 搜索插件时, 如果 groupId 没有显式提供时, 则以此处配置的 groupId 为默认值, 可以简单理解为默认导入这些 groupId 下的所有 artifact(需要时才下载)
     | 默认情况下该列表包含了 org.apache.maven.plugins和org.codehaus.mojo
     |
     | 查看插件信息: 
     |    mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:3.5.1 -Ddetail
     |-->
    <pluginGroups>

        <!-- plugin 的 groupId -->
        <!--
        <pluginGroup>com.your.plugins</pluginGroup>
        -->

    </pluginGroups>

    <!-- 进行远程服务器访问时所需的授权配置信息。通过系统唯一的 server-id 进行唯一关联 -->
    <servers>
        <server>
            <!-- 这是 server 的 id, 该 id 与 distributionManagement 中 repository 元素的id 相匹配 -->
            <id>server_id</id>

            <!-- 鉴权用户名 -->
            <username>auth_username</username>

            <!-- 鉴权密码 -->
            <password>auth_pwd</password>

            <!-- 鉴权时使用的私钥位置。和前两个元素类似, 私钥位置和私钥密码指定了一个私钥的路径(默认是/home/hudson/.ssh/id_dsa)以及如果需要的话, 一个密钥 -->
            <privateKey>path/to/private_key</privateKey>

            <!-- 鉴权时使用的私钥密码, 非必要, 非必要时留空 -->
            <passphrase>some_passphrase</passphrase>

            <!-- 
             | 文件被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录, 这时候就可以使用权限(permission)
             | 这两个元素合法的值是一个三位数字, 其对应了unix文件系统的权限, 如664, 或者775 
             |-->
            <filePermissions>664</filePermissions>

            <!-- 目录被创建时的权限 -->
            <directoryPermissions>775</directoryPermissions>

            <!-- 传输层额外的配置项 -->
            <configuration></configuration>

        </server>
    </servers>

    <!-- 
   | 从远程仓库才下载 artifacts 时, 用于替代指定远程仓库的镜像服务器配置;
   | 
   | 例如当您无法连接上国外的仓库是, 可以指定连接到国内的镜像服务器;
   |
   | pom.xml 和 setting.xml 中配置的仓库和镜像优先级关系(mirror 优先级高于 repository): 
   |
   |    repository(setting.xml) < repository(pom.xml) < mirror(setting.xml)
   |
   |    例如, 如果配置了 mirrorOf = *,  则 不管项目的 pom.xml 配置了什么仓库, 最终都会被镜像到 镜像仓库
   |
   |  私服的配置推荐用profile配置而不是mirror
   |-->
    <mirrors>

        <!-- 
         | 【mirro 匹配顺序】: 
         | 多个 mirror 优先级 按照 id字母顺序进行排列(即与编写的顺序无关)
         | 在第一个 mirror 找不到 artifact, 不会继续超找下一个镜像。
         | 只有当 mirror 无法链接的时候, 才会尝试链接下一个镜像, 类似容灾备份。
         |-->

        <mirror>

            <!-- 该镜像的唯一标识符, id用来区分不同的 mirror 元素, 同时会套用使用 server 中 id 相同授权配置链接到镜像 -->
            <id>aliyun</id>

            <!-- 镜像名称, 无特殊作用, 可视为简述 -->
            <name>sjtug maven proxy</name>

            <!-- 镜像地址 -->
            <url>https://mirrors.sjtug.sjtu.edu.cn/maven-central/</url>
            <!-- 被镜像的服务器的id, 必须与 repository 节点设置的 ID 一致。但是 This must not match the mirror id
             | mirrorOf 的配置语法: 
             | *           = 匹配所有远程仓库。 这样所有 pom 中定义的仓库都不生效
             | external:*  = 匹配除 localhost、使用 file:// 协议外的所有远程仓库
             | repo1,repo2 = 匹配仓库 repo1 和 repo2
             | *,!repo1    = 匹配所有远程仓库, repo1 除外
             |-->
            <mirrorOf>central</mirrorOf>
        </mirror>

    </mirrors>

    <!-- 用来配置不同的代理, 多代理 profiles 可以应对笔记本或移动设备的工作环境: 通过简单的设置 profile id 就可以很容易的更换整个代理配置 -->
    <proxies>

        <!-- 代理元素包含配置代理时需要的信息 -->
        <proxy>

            <!-- 代理的唯一定义符, 用来区分不同的代理元素 -->
            <id>example_proxy</id>

            <!-- 该代理是否是激活的那个。true则激活代理。当我们声明了一组代理, 而某个时候只需要激活一个代理的时候, 该元素就可以派上用处 -->
            <active>false</active>

            <!-- 代理的协议 -->
            <protocol>https</protocol>

            <!-- 代理的主机名 -->
            <host>proxy.molo.com</host>

            <!-- 代理的端口 -->
            <port>443</port>

            <!-- 代理服务器认证的登录名 -->
            <username>proxy_user</username>

            <!-- 代理服务器认证登录密码 -->
            <password>proxy_pwd</password>

            <!-- 不该被代理的主机名列表。该列表的分隔符由代理服务器指定;例子中使用了竖线分隔符, 使用逗号分隔也很常见 -->
            <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>

        </proxy>

    </proxies>

    <!--
     | 构建方法的配置清单, maven 将根据不同环境参数来使用这些构建配置。
     | settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。
     | settings.xml 负责的是整体的构建过程, pom.xml 负责单独的项目对象构建过程。
     | settings.xml 只包含了id, activation, repositories, pluginRepositories 和 properties 元素。
     | 
     | 如果 settings 中的 profile 被激活, 它的值会覆盖任何其它定义在 pom.xml 中或 profile.xml 中的相同 id 的 profile。
     |
     | 查看当前激活的 profile:
     |   mvn help:active-profiles
     |-->
    <profiles>

        <profile>

            <!-- 该配置的唯一标识符 -->
            <id>profile_id</id>

            <!--
             | profile 的激活条件配置;
             | 其他激活方式: 
             | 1. 通过 settings.xml 文件中的 activeProfile 元素进行指定激活。
             | 2. 在命令行, 使用-P标记和逗号分隔的列表来显式的激活, 如: mvn clean package -P myProfile)。 
             |-->
            <activation>

                <!-- 是否默认激活 -->
                <activeByDefault>false</activeByDefault>

                <!--  内建的 java 版本检测, 匹配规则: https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html -->
                <jdk>9.9</jdk>

                <!-- 内建操作系统属性检测, 配置规则: https://maven.apache.org/enforcer/enforcer-rules/requireOS.html -->
                <os>

                    <!-- 操作系统 -->
                    <name>Windows XP</name>

                    <!-- 操作系统家族 -->
                    <family>Windows</family>

                    <!-- 操作系统 -->
                    <arch>x86</arch>

                    <!-- 操作系统版本 -->
                    <version>5.1.2600</version>

                </os>

                <!--
                 | 如果Maven检测到某一个属性(其值可以在POM中通过${名称}引用), 并且其拥有对应的名称和值, Profile就会被激活。
                 | 如果值字段是空的, 那么存在属性名称字段就会激活profile, 否则按区分大小写方式匹配属性值字段
                 |-->
                <property>

                    <!-- 属性名 -->
                    <name>mavenVersion</name>

                    <!-- 属性值 -->
                    <value>2.0.3</value>

                </property>
                
                <!-- 根据文件存在/不存在激活profile -->
                <file>

                    <!-- 如果指定的文件存在, 则激活profile -->
                    <exists>/path/to/active_on_exists</exists>

                    <!-- 如果指定的文件不存在, 则激活profile -->
                    <missing>/path/to/active_on_missing</missing>

                </file>

            </activation>
            <!-- 扩展属性设置。扩展属性可以在 POM 中的任何地方通过 ${扩展属性名} 进行引用
             |
             | 属性引用方式(包括扩展属性, 共 5 种属性可以引用): 
             |
             | env.x                  : 引用 shell 环境变量, 例如, "env.PATH"指代了 $path 环境变量(在 Linux / Windows 上是 %PATH% ).
             | project.x              : 引用 pom.xml(根元素就是 project) 中 xml 元素内容.例如 ${project.artifactId} 可以获取 pom.xml 中设置的 <artifactId /> 元素的内容
             | settings.x             : 引用 setting.xml(根元素就是 setting) 中 xml 元素内容, 例如 ${settings.offline}
             | Java System Properties : 所有可通过 java.lang.System.getProperties() 访问的属性都能在通过 ${property_name} 访问, 例如 ${java.home}
             | x                      : 在 <properties/> 或者 外部文件 中设置的属性, 都可以 ${someVar} 的形式使用
             | 
             |-->
            <properties>

                <!-- 在当前 profile 被激活时,  ${profile.property} 就可以被访问到了 -->
                <profile.property>this.property.is.accessible.when.current.profile.actived</profile.property>

            </properties>

            <!-- 远程仓库列表 -->
            <repositories>
                <!-- 
                 | releases vs snapshots
                 | maven 针对 releases、snapshots 有不同的处理策略, POM 就可以在每个单独的仓库中, 为每种类型的 artifact 采取不同的策略
                 | 例如: 
                 |     开发环境 使用 snapshots 模式实时获取最新的快照版本进行构建
                 |     生成环境 使用 releases 模式获取稳定版本进行构建
                 | 参见repositories/repository/releases元素 
                 |-->

                <!--
                 | 依赖包不更新问题:                
                 | 1. Maven 在下载依赖失败后会生成一个.lastUpdated 为后缀的文件。如果这个文件存在, 那么即使换一个有资源的仓库后, Maven依然不会去下载新资源。
                 |    可以通过 -U 参数进行强制更新、手动删除 .lastUpdated 文件:
                 |      find . -type f -name "*.lastUpdated" -exec echo {}" found and deleted" \; -exec rm -f {} \;
                 |
                 | 2. updatePolicy 设置更新频率不对, 导致没有触发 maven 检查本地 artifact 与远程 artifact 是否一致
                 |-->
                <repository>

                    <!-- 远程仓库唯一标识 -->
                    <id>maven_repository_id</id>

                    <!-- 远程仓库名称 -->
                    <name>maven_repository_name</name>

                    <!-- 远程仓库URL, 按protocol://hostname/path形式 -->
                    <url>http://host/maven</url>

                    <!-- 
                    | 用于定位和排序 artifact 的仓库布局类型-可以是 default(默认)或者 legacy(遗留)
                    | Maven 2为其仓库提供了一个默认的布局;然而, Maven 1.x有一种不同的布局。我们可以使用该元素指定布局是default(默认)还是legacy(遗留)
                    | -->
                    <layout>default</layout>

                    <!-- 如何处理远程仓库里发布版本的下载 -->
                    <releases>

                        <!-- 是否允许该仓库为 artifact 提供 发布版 / 快照版 下载功能 -->
                        <enabled>false</enabled>

                        <!-- 
                         | 每次执行构建命令时, Maven 会比较本地 POM 和远程 POM 的时间戳, 该元素指定比较的频率。
                         | 有效选项是: 
                         |     always(每次构建都检查), daily(默认, 距上次构建检查时间超过一天), interval: x(距上次构建检查超过 x 分钟)、 never(从不)
                         |
                         | 重要: 
                         |     设置为 daily, 如果 artifact 一天更新了几次, 在一天之内进行构建, 也不会从仓库中重新获取最新版本
                         |-->
                        <updatePolicy>always</updatePolicy>

                        <!-- 当 Maven 验证 artifact 校验文件失败时该怎么做: ignore(忽略), fail(失败), 或者warn(警告) -->
                        <checksumPolicy>warn</checksumPolicy>

                    </releases>

                    <!-- 如何处理远程仓库里快照版本的下载 -->
                    <snapshots>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    </snapshots>

                </repository>

                <!-- 
                    国内可用的 maven 仓库地址(updated @ 2019-02-08):

                    https://maven.aliyun.com/repository/public
                    http://maven.wso2.org/nexus/content/groups/public/
                    http://jcenter.bintray.com/
                    http://maven.springframework.org/release/
                    http://repository.jboss.com/maven2/
                    http://uk.maven.org/maven2/
                    http://repo1.maven.org/maven2/
                    http://maven.springframework.org/milestone
                    http://maven.jeecg.org/nexus/content/repositories/
                    http://repo.maven.apache.org/maven2
                    http://repo.spring.io/release/
                    http://repo.spring.io/snapshot/
                    http://mavensync.zkoss.org/maven2/
                    https://repository.apache.org/content/groups/public/
                    https://repository.jboss.org/nexus/content/repositories/releases/   
                -->

            </repositories>

            <!-- 
             | maven 插件的远程仓库配置。maven 插件实际上是一种特殊类型的 artifact。
             | 插件仓库独立于 artifact 仓库。pluginRepositories 元素的结构和 repositories 元素的结构类似。
             |-->
            <!--
            <pluginRepositories>
                <pluginRepository>
                    <releases>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    </releases>
                    <snapshots>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    </snapshots>
                    <id />
                    <name />
                    <url />
                    <layout />
                </pluginRepository>
            </pluginRepositories>
            -->

        </profile>

    </profiles>

    <!--
     | 手动激活 profiles 的列表, 按照 profile 被应用的顺序定义 activeProfile
     | 任何 activeProfile, 不论环境设置如何, 其对应的 profile 都会被激活, maven 会忽略无效(找不到)的 profile
     |-->
    <!--
    <activeProfiles>
        <activeProfile>not-exits-profile</activeProfile>
    </activeProfiles>
    -->

</settings>
<?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">
        <!-- 默认值是${user.home}/.m2/repository -->
    <localRepository>E:/project/localRepository</localRepository>
    <!-- 表示是否使用交互模式,默认是true
    如果为false,命令如下
    mvn archetype:generate -DgroupId=com.zworks -DartifactId=maven-setting -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    需要指定groupId、artifactId、archetypeArtifactId,如果不指定会报错,因为这些是无法推测出值的。
    如果为true,命令如下
    mvn archetype:generate
    后面会让你选择或输入archetype、groupId、artifactId、version、package、为false的时候之所以不用指定version和package是因为这两个都有默认值。
    -->
    <interactiveMode>true</interactiveMode>
    <!-- 如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false -->
    <usePluginRegistry>false</usePluginRegistry>
    <!-- 如果构建系统要在离线模式下工作,设置为true,默认为false。如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。 -->
    <offline>false</offline>
  
    <!--插件组
    在pluginGroups元素下面可以定义一系列的pluginGroup元素。表示当通过plugin的前缀来解析plugin的时候到哪里寻找。
    pluginGroup元素指定的是plugin的groupId。默认情况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。-->
    <pluginGroups>
      <pluginGroup>org.mortbay.jetty</pluginGroup>
    </pluginGroups>
    <!--例如,有了上面的配置,Maven命令行可以使用简单的命令执行org.morbay.jetty:jetty-maven-plugin:run,如下:
    mvn jetty run
    -->
    <!--服务器
    用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。
    但是某些配置例如username和password就不应该随着pom.xml来分配了。这种类型的信息应该保存在构建服务器中的settings.xml中。
    -->
    <servers>
        <server>
        <!-- 这是Server的ID(不是登录进来的user),与Maven想要连接上的repository/mirror中的id元素相匹配。 -->
            <id>nexus_server_id</id>
            <username>my_login</username>
            <password>my_password</password>
        <!-- 与前两个元素一样,这两个成对出现,分别指向了一个私钥(默认的${user.home}/.ssh/id_dsa)和一个passphrase。即分别表示私钥位置和私钥密码 -->
            <privateKey>${user.home}/.ssh/id_dsa</privateKey>
            <passphrase>some_passphrase</passphrase>
        <!-- 文件和目录被创建时的权限。后续需要用此权限来访问。
                这两个元素合法的值是一个三位数字,其对应了unix文件系统的权限,如664,或者775。 -->
            <filePermissions>664</filePermissions>
            <directoryPermissions>775</directoryPermissions>
        <!-- 传输层额外的配置项 -->
            <configuration></configuration>
        </server>
    </servers>
        <!--镜像-->
    <mirrors>
        <mirror>
            <!-- 镜像标识id -->
            <id>nexus-aliyun</id>
            <name>Nexus aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <!-- 指向此镜像的仓库Id,任何对于远程仓库的请求都会被转至此url。
                        多个逗号隔开,或者*号统配,或者!排除某个之外的所有仓库
            external:*匹配除使用 localhost 或基于文件的存储库之外的所有存储库。当您想要排除为集成测试定义的重定向存储库时使用。
            自 Maven 3.8.0 起,external:http:*匹配所有使用 HTTP 的存储库,但使用 localhost 的存储库除外。
            * = 一切
            external:* = 一切不在本地主机上,也不基于文件。
            repo,repo1 = repo或repo1
            *,!repo1 = 除了 repo1 之外的所有东西
                        注意不要在逗号分隔列表中的标识符或通配符周围包含额外的空格。
                        例如,<mirrorOf设置为 >的镜像!repo1, *不会镜像任何内容,而!repo1,*会镜像除repo1
                        注意多个mirrorOf内容相同并不会都生效,每个仓库只能有一个镜像,Maven 不会聚合镜像,而只是选择第一个匹配项
                        -->
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
    <!-- 代理设置,主要用于无法直接访问中心的库用户配置 -->
        <!--用来配置不同的代理,多代理profiles 可以应对笔记本或移动设备的工作环境:通过简单的设置profile id就可以很容易的更换整个代理配置。 --> 
        <proxies>
          <proxy>
             <id>myproxy</id>
             <active>true</active>
             <protocol>http</protocol>
             <host>proxy.somewhere.com</host>
             <port>8080</port>
             <!-- 两个元素成对出现,提供连接proxy服务器时的认证 -->
             <username>proxyuser</username>
             <password>somepassword</password>
             <!--不该被代理的主机名列表。该列表的分隔符由代理服务器指定;例子中使用了竖线分隔符,使用逗号分隔也很常见。-->
             <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
         </proxy>
        </proxies>
    <!--配置文件
    settings.xml中的profile是pom.xml中的profile的简洁形式。
    它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。
    profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。
    如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。
    -->
    <profiles>
        <profile>
            <id>dev</id>
            <!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。
                        如POM中的profile一样,profile的力量来自于它能够在某些特定的环境中自动使用某些特定的值;
                        这些环境通过activation元素指定。activation元素并不是激活profile的唯一方式。
                        settings.xml文件中的activeProfile元素可以设置需要激活profile的id。
                        profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。-->
                    <activation>
              <!-- 默认激活的标识 -->
              <activeByDefault>false</activeByDefault>
              <!--当匹配的jdk被检测到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4开头的JDK。-->
              <jdk>1.5</jdk>
              <!--当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。-->
              <os>
                <!--激活profile的操作系统的名字 -->
                <name>Windows XP</name>
                <!--激活profile的操作系统所属家族(如 'windows')  -->
                <family>Windows</family>
                <!--激活profile的操作系统体系结构  -->
                <arch>x86</arch>
                <!--激活profile的操作系统版本-->
                <version>5.1.2600</version>
              </os>
              <!--如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),其满足对应的name = 值,Profile就会被激活。
                            如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段-->
              <property>
                <name>mavenVersion</name>
                <value>2.0.3</value>
              </property>
              <!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。
                            另一方面,exists则会检查文件是否存在,如果存在则激活profile。-->
              <file>
                <!--如果指定的文件存在,则激活profile。 -->
                <exists>${basedir}/file2.properties</exists>
                <!--如果指定的文件不存在,则激活profile。-->
                <missing>${basedir}/file1.properties</missing>
              </file>
                </activation>
            <!--如果以上所有指定的条件都达到了,那么,activation就被触发,而且不需要一次性全部达到。-->
            <!--仓库(repositories)
            仓库是Maven用来构筑构建系统的本地仓库的远程项目集合。它来自于被Maven叫做插件和依赖的本地仓库。
                        不同的远程仓库包含不同的项目,当profile被激活,他们就会需找匹配的release或者snapshot构件。 -->
            <repositories>
                <repository>
                    <id>ccl-nexus</id>
                    <url>http://172.16.10.99:8081/nexus/content/groups/public</url>
                    <!--如何处理远程仓库里发布版本的下载-->
                    <releases>
                        <!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。  -->
                        <enabled>true</enabled>
                        <!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。
                                                选项是:always(一直), 
                                                            daily(默认,每日),
                                                            interval:X(这里X是以分钟为单位的时间间隔),
                                                            never(从不)。 -->
                                            <updatePolicy>always</updatePolicy>
                        <!--当Maven验证构件校验文件失败时该怎么做-ignore(忽略),fail(失败),或者warn(警告)。-->
                                        <checksumPolicy>warn</checksumPolicy>
                    </releases>
                    <!--如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置,
                                        POM就可以在每个单独的仓库中,为每种类型的构件采取不同的策略。
                                        例如,可能有人会决定只为开发目的开启对快照版本下载的支持。参见repositories/repository/releases元素-->
                    <snapshots>
                        <enabled>true</enabled>
                                            <updatePolicy>never</updatePolicy>
                                        <checksumPolicy>fail</checksumPolicy>
                    </snapshots>
                </repository>
            </repositories>
            <!--插件仓库(plugin repositories)
            仓库包含了两种重要类型的构件:第一种是用来做其他构件依赖的构件,这是在中央仓库中的大多数插件。另外一种类型的构件就是插件。
                        Maven的插件本身就是一种特殊的构件。因此,插件仓库被从其他仓库中分离出来。
                        pluginRepositories元素模块的结构与repositories模块很相似。pluginRepository元素指向一个可以找到新插件的远程地址。
            -->
            <pluginRepositories>
                <pluginRepository>
                    <id>ccl-nexus</id>
                    <url>http://172.16.10.99:8081/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <!--属性(properites)
            Maven的属性是值占位符,就像Ant中的属性。如果X是一个属性的话,那么它的值在POM中可以使用${X}来进行任意地方的访问。他们来自于五种不同的风格,所有都可以从settings.xml文件中访问到。
            1.env.X:使用“env.”前缀将会返回当前的环境变量。例如${env.PATH}就是使用了$path环境变量。
            2.project.X:一个点“.”分割的路径,在POM中就是相关的元素的值。例如:<project><version>1.0</version></project>就可以通过${project.version}来访问。
            3.settings.X:一个点“.”分割的路径,在settings.xml中就是相对应的元素的值,例如:<settings><offline>false</offline></settings>就可以通过${settings.offline}来访问。
            4.Java系统属性:所有通过java.lang.System.getProperties()来访问的属性都可以像POM中的属性一样访问,例如:${java.home}
            5.X:被<properties/>或者外部文件定义的属性,值可以这样访问${someVar}
            -->
            <properties>
              <sonar.jdbc.url>
                jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8
              </sonar.jdbc.url>
              <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
              <sonar.jdbc.username>root</sonar.jdbc.username>
              <sonar.jdbc.password></sonar.jdbc.password>
              <sonar.host.url>http://172.16.11.43:80</sonar.host.url>
            </properties>
                        <!--如果这个profile被激活,那么属性${sonar.jdbc.url}就可以被访问了-->
        </profile>     
    </profiles>
        <!--激活配置(Active Profiles)-->
    <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>
   
</settings>

镜像仓库(mirror)

  • 为什么需要配置镜像仓库 1、在不配置镜像的情况下,maven默认会使用中央库.

2、maven中央库在国外,有时候访问会很慢,尤其是下载较大的依赖的时候,有时候速度会很慢,甚至会出现无法下载的情况.

3、为了解决依赖下载速度的问题,需要配置maven国内镜像

  • 配置多个镜像仓库 配置多个镜像仓库,他不是我们想的一个找不到就在下一个镜像中寻找,只有在第一个镜像仓库链接不上的情况下,才会去下一个镜像仓库中寻找。而我们想要的效果是:当a.jar在第一个mirror中不存在的时候,maven会去第二个mirror中查询下载,但是maven不会这样做! 多个 mirror 优先级 按照 id字母顺序进行排列(即与编写的顺序无关)

  • 常见镜像仓库

<!-- 阿里中央仓库 -->
<repository>
    <id>aliyun</id>
    <name>aliyun maven Repository</name>
    <url>https://maven.aliyun.com/repository/public</url>
</repository>

<!-- camunda.com 中央仓库 -->
<repository>
    <id>activiti-repos2</id>
    <name>Activiti Repository 2</name>
    <url>https://app.camunda.com/nexus/content/groups/public</url>
</repository>

<!-- alfresco.com 中央仓库 -->
<repository>
    <id>activiti-repos</id>
    <name>Activiti Repository</name>
    <url>https://maven.alfresco.com/nexus/content/groups/public</url>
</repository>

<!-- spring.io 中央仓库 -->
<repository>
    <id>springsource-repos</id>
    <name>SpringSource Repository</name>
    <url>http://repo.spring.io/release/</url>
</repository>

<!-- maven.apache.org 中央仓库 -->
<repository>
    <id>central-repos</id>
    <name>Central Repository</name>
    <url>http://repo.maven.apache.org/maven2</url>
</repository>

<!-- maven.org 中央仓库 -->
<repository>
    <id>central-repos1</id>
    <name>Central Repository 2</name>
    <url>http://repo1.maven.org/maven2/</url>
</repository>

<!-- oschina 中央仓库(需要翻墙) -->
<repository>
    <id>oschina-repos</id>
    <name>Oschina Releases</name>
    <url>http://maven.oschina.net/content/groups/public</url>
</repository>

<!-- oschina thinkgem 中央仓库(需要翻墙) -->
<repository> 
    <id>thinkgem-repos</id> 
    <name>ThinkGem Repository</name>
    <url>http://git.oschina.net/thinkgem/repos/raw/master</url>
</repository>

<!-- java.net 中央仓库(需要翻墙) -->
<repository>
    <id>java-repos</id>
    <name>Java Repository</name>
    <url>http://download.java.net/maven/2/</url>
</repository>

<!-- github.com 中央仓库(需要翻墙) -->
<repository> 
    <id>thinkgem-repos</id> 
    <name>ThinkGem Repository 2</name>
    <url>https://raw.github.com/thinkgem/repository/master</url>
</repository>

<!-- ibiblio 镜像地址(这个也比较快) -->
<mirror>
    <id>ibiblio</id>
    <name>Mirror from Maven ibiblio</name>
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<!-- repo1.maven.apache.org 镜像地址  -->
<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.apache.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<!-- repo2 镜像地址  -->
<mirror>
    <id>repo2</id>
    <name>Mirror from Maven Repo2</name>
    <url>http://repo2.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<!-- JBoos 镜像地址 -->
<mirror>
    <id>jboss-public-repository-group</id>
    <name>JBoss Public Repository Group</name>
    <url>http://repository.jboss.org/nexus/content/groups/public</url>
    <mirrorOf>central</mirrorOf>
</mirror>

<!-- Google 镜像地址 -->
<mirror>
    <id>google</id>
    <name>google maven</name>
    <url>https://maven.google.com/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

注意:<mirror>标签中<mirrorof>*</mirrorof>*:表示匹配所有远程仓库,这样pom中定义的仓库和<profile>中定义的仓库将失效

  • jar包搜索顺序

1、本地仓库;
2、maven settings profile中的repository;
3、pom.xml中profile中定义的repository;
4、pom.xml中的repositorys(定义多个repository,按定义顺序找);
5、mirror。

  • setting 示例
<?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">

    <!-- 默认的值是${user.home}/.m2/repository -->
    <localRepository>E:\apache\repository</localRepository>

    <!-- 如果Maven要试图与用户交互来得到输入就设置为true,否则就设置为false,默认为true。 -->
    <interactiveMode>true</interactiveMode>

    <!-- 如果Maven使用${user.home}/.m2/plugin-registry.xml来管理plugin的版本,就设置为true,默认为false。 -->
    <usePluginRegistry>false</usePluginRegistry>

    <!-- 如果构建系统要在离线模式下工作,设置为true,默认为false。 如果构建服务器因为网络故障或者安全问题不能与远程仓库相连,那么这个设置是非常有用的。 -->
    <offline>false</offline>

    <mirrors>
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf><!--*代表所有的jar包都到阿里云下载-->
            <!--<mirrorOf>central</mirrorOf>--><!--central代表只有中央仓库的jar包才到阿里云下载-->
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>

        <!--老版阿里云公共仓库-->
        <!--
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </mirror>
        -->
        
        <!-- 中央仓库在中国的镜像 -->
        <mirror>
            <id>maven.net.cn</id>
            <name>Mirror from Maven in china</name>
            <url>http://maven.net.cn/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>




    <!-- settings.xml中的profile是pom.xml中的profile的简洁形式。 它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。 
        profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。 如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。 -->
    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <!-- 远程仓库-->
            <repositories>
                <!-- 阿里云远程仓库-->
                <repository>
                    <id>aliyun</id>
                    <name>aliyun maven Repository</name>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>spring-milestone</id>
                    <name>Spring Milestone Repository</name>
                    <url>http://repo.spring.io/milestone</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                    <layout>default</layout>
                </repository>
                <repository>
                    <id>spring-snapshot</id>
                    <name>Spring Snapshot Repository</name>
                    <url>http://repo.spring.io/snapshot</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <layout>default</layout>
                </repository>
            </repositories>
            <!-- 镜像,添加了会先从阿里云仓库下载-->
            <pluginRepositories>
                <pluginRepository>
                    <id>aliyun-plugin</id>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <!-- activations是profile的关键,就像POM中的profiles,profile的能力在于它在特定情况下可以修改一些值。 
        而这些情况是通过activation来指定的。 -->
    <!-- <activeProfiles/> -->

</settings>