• Maven解析1配置文件setting.xml


    前言
     
    本机使用的Maven 版本:apache-maven-3.6.0
    JDK1.8
    镜像仓库:如果仓库 X 可以提供仓库 Y 存储的所有的内容,那么仓库 X 就可以说是 仓库 Y 的镜像(也就是说能从仓库 Y 获取的内容,从仓库 X 也能获取)
    maven的setting.xml文件一般会存在两个地方:
    1. 在maven安装目录(全局):apache-maven-3.6.0/conf/setting.xml 
    2. 在用户安装目录下(当前用户下):${user.home}\.m2\settings.xml 。
    配置文件这里对于本地IDEA开发会遇到一个坑,留给大家吧
     
    属性解析
     
    setting
    <setting>setting 文件的根元素</setting>
     
    localRepository
    <localRepository>本地仓库</localRepository>
    <!-- 该属性一般需要自己设置
         不设置的话,本地仓库默认地址为-->
    <localRepository>usr/.m2/repository</localRepository>
    interactiveMode
    <interactiveMode>Maven 是否与用户交互</interactiveMode>
    <!-- 该属性一般按照默认配置(设置为 true )
         设置为 false 时,Maven 会基于一些其他设置参数,配置一个默认值-->
    <interactiveMode>true</interactiveMode>
     
    offline
    <offline>离线模式</offline>
    <!-- 该属性一般按照默认设置(设置为 false)
         该属性值 Maven 构建时是否连接网络,会产生 jar 包下载、部署及其他错误影响-->
    <offline>false</offline>
     
    pluginGroups
    <pluginGroups>插件组</pluginGroups>
    proxies
    <!-- 
        | 代理主机信息
        | 当公司网络与公网隔离,需要代理主机才能访问公网时,需要设置该属性 
    -->
    <proxies>
        <proxy>
          <!-- (自定义id名称即可) -->
          <id>optional</id>
          <!-- (是否激活该代理) -->
          <active>true</active>
          <!-- (使用的代理协议) -->
          <protocol>http</protocol>
          <!-- (用户名) -->  
          <username>proxyuser</username>
          <!-- (密码) -->
          <password>proxypass</password>
          <!-- (代理服务器地址) -->
          <host>proxy.host.net</host>
          <!-- (代理服务器端口) -->
          <port>80</port>
          <!-- (指定那些主机地址不需要代理)
              | 支持 “ | ” 分隔多个主机地址 
            | 支持 “ * ” 适配符 例如 *.baidu.com 表示所有以 baidu.com 结尾的主机地址,都不会走代理
          -->
          <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
    </proxies>
    <!-- 
         | 支持配置多个 proxy ,默认是第一个 proxy 配置生效
    -->
    servers
    <!-- 
        | 仓库/镜像仓库的认证信息
    -->
    <servers>
         <server>   
          <!-- (自定义id名称) 
              | id 属性,需要与下面讲述的 mirror 、profile 属性中的 id 保持一致
          -->
          <id>deploymentRepo</id>
          <!-- (用户名) -->
          <username>repouser</username>
          <!-- (密码) -->
          <password>repopwd</password>
        </server>
    </servers>
    mirrors
    <!--
        | 镜像仓库
    -->
    <mirrors>
        <mirror>
          <!-- (自定义id名称) -->
          <id>mirrorId</id>
          <!-- (镜像仓库匹配范围) 
            | “ * ”表示匹配所有远程仓库,任何对远程仓库的请求,都会转到该镜像地址下
            | “ external:* ”表示匹配所有远程仓库(localhost、file://协议、这两种除外)即匹配不在本机上的所有远程仓库
            | “ repo1,repo2 ”表示匹配 repo1、repo2 两个远程仓库,可以使用逗号分隔多个远程仓库
            | “ *,!repo1”表示匹配所有远程仓库,但是 repo1 远程仓库除外          
          -->
          <mirrorOf>repositoryId</mirrorOf>
          <!-- (镜像名称) -->
          <name>Human Readable Name for this Mirror.</name>
          <!-- (镜像地址) -->
          <url>http://my.repository.com/repo/path</url>
        </mirror>
    </mirrors>
    <!--    
        | 镜像仓库的常用方式:结合私服使用
            | 说明:因为私服,可以代理任何外部公共仓库,那么,可以使用一个私服地址,代理所有需要的外部公共仓库,简化 Maven 的配置。
                   这种情况下,私服可以说所有需要的外部公共仓库的镜像;    
        | 如果镜像需要认证信息,同样是在 servers 中增加 server 信息
        | 镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或停止服务的时候,Maven 将无法方位被镜像仓库,因为无法下载内容
    -->
    profiles
    <!-- 
        | 配置文件列表
        | 不同环境的构建大概率是不同的,比如数据库配置、使用特殊版本的依赖、配置插件使用本地文件,为了让构建能在不同环境移植,Maven 引入 Profile 
    -->
    <profiles>
        <profile>
            <!-- (自定义id名称) -->
            <id>nexus</id> 
            <repositories> 
              <repository> 
                <id>nexus</id>
                <!-- (自定义资源名称) -->
                <name>Nexus</name>
                <!-- 仓库地址 -->
                <url>http://100.4.252.5:18080/nexus/content/groups/public</url>
                <releases>
                    <!-- 是否允许该仓库为构件提供 发布版 / 快照版 下载功能 -->
                    <enabled>true</enabled>
                    <!-- 每次执行构建命令时, Maven 会比较本地 POM 和远程 POM 的时间戳, 该元素指定比较的频率 
                        | always(每次构建都检查)
                        | daily(默认, 距上次构建检查时间超过一天)
                        | interval: x(距上次构建检查超过 x 分钟)
                        | never(从不)
                    -->
                    <updatePolicy>daily</updatePolicy>
                    <!-- 当 Maven 验证构件的校验文件失败时该怎么做: ignore(忽略), fail(失败), 或者warn(警告)-->
                    <checksumPolicy>warn</checksumPolicy>  
                </releases> 
                <snapshots>
                    <!-- 参照 releases -->
                    <enabled>true</enabled>  
                    <updatePolicy>daily</updatePolicy>
                    <checksumPolicy>warn</checksumPolicy>  
                </snapshots>
              </repository>
            </repositories> 
            <pluginRepositories> 
              <pluginRepository> 
                <id>nexus</id> 
                <name>Nexus</name>
                <!-- 仓库地址 -->
                <url>http://100.4.252.5:18080/nexus/content/groups/public</url>
                <!-- (发布版本的插件) -->
                <releases><enabled>true</enabled></releases> 
                <!--(快照版本的插件) -->
                <snapshots><enabled>true</enabled></snapshots> 
              </pluginRepository> 
            </pluginRepositories> 
        </profile>
    </profiles>
    <!-- 
         | profile 激活
            | 通过命令行激活:用户使用 mvn 命令行参数 -P 加上 profile 的id 来激活 profile,多个通过逗号分隔
            | setting.xml 配置文件显式激活:使用 activeProfiles 属性,表示 setting.xml 中的 profile 在所有项目中激活(下面会讲)
            | 系统属性激活
    -->
    profile 种类
    1、pom.xml: pom 文件的 profile 只对当前项目生效
    2、用户setting.xml: 用户目录下/.m2/setting.xml 文件中的 profile 只对本机该用户所有的 Maven 项目生效
    3、全局setting.xml: Maven 安装目录下的 setting.xml 文件中的 profile 对本机所有的 Maven 项目生效
    4、profiles.xml (Maven 2) 还可以在项目根目录下使用一个额外的 profiles.xml 声明,该特性在 Maven 3 中移除,建议将 profiles 添加到 setting.xml 中
     
    profile激活
    1、通过命令行激活:用户使用 mvn 命令行参数 -P 加上 profile 的id 来激活 profile,多个通过逗号分隔
    mvn clean -Pdev-x,dev-y
    2、setting.xml 配置文件显式激活:使用 activeProfiles 属性,表示 setting.xml 中的 profile 在所有项目中激活
    <activeProfiles> 
         <activeProfile>nexus</activeProfile>  
     </activeProfiles>
    3、系统属性激活:用户可以配置系统属性 test 存在时,自动激活 profile ,如下面第一个示例;用户可以配置系统属性存在,且系统属性值时,自动激活profile,如下面第二个示例
    <!-- 第一个示例 -->
    <profiles>
        <profile>
            <activeprofile>
                <property>
                    <name>test</name>
                </property>
            </activeprofile>
        </profile>
    </profiles>
    
    <!-- 第二个示例 -->
    <profiles>
        <profile>
            <activeprofile>
                <property>
                    <name>test</name>
                    <value>x</value>
                </property>
            </activeprofile>
        </profile>
    </profiles>
    
    <!-- 一定要记得,也是可以通过命令行声明系统属性激活 -->
    mvn clean -Ptest=x
    4、操作系统环境激活:profile 可以根据不同操作系统环境自动激活,如果构建在不同操作系统环境且有差异,可以把这些差异写进 profile
    <profiles>
        <profile>
            <activeprofile>
                <os>
                    <name>Window XP</name>
                    <family>Window</value>
                    <arch>X86</arch>
                </os>
            </activeprofile>
        </profile>
    </profiles>
    5、文件是否存在激活:Maven 能够根据项目中某个文件是否存在来决定是否激活 profile
    <profiles>
        <profile>
            <activeprofile>
                <file>
                    <missing>y.properties</name>
                    <exists>x.properties</value>
                </file>
            </activeprofile>
        </profile>
    </profiles>
    activeprofile
    <!-- 使用 activeProfiles 属性,表示 setting.xml 中的 profile 在所有项目中激活 -->
    <activeProfiles>
        <activeProfile>central(id 需要与 profile 中的 id 保持一致)</activeProfile>
    </activeProfiles>

    补充国内常用的几个镜像地址-供本地开发学习使用:

        <mirrors>
            <mirror>
                <id>alimaven</id>
                <name>aliyun maven</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                <mirrorOf>central</mirrorOf>
            </mirror>
            <mirror>
                <id>alisnapshots</id>
                <name>Ali OSS snapshots</name>
                <mirrorOf>central</mirrorOf>
                <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            </mirror>
            <mirror>
                <id>alirelase</id>
                <name>Ali OSS release</name>
                <mirrorOf>central</mirrorOf>
                <url>https://oss.sonatype.org/content/repositories/releases</url>
            </mirror>
            <mirror>
                <id>central</id>
                <name>Maven Repository Switchboard</name>
                <url>http://repo1.maven.org/maven2/</url>
                <mirrorOf>central</mirrorOf>
            </mirror>
            <mirror>
                <id>repo2</id>
                <mirrorOf>central</mirrorOf>
                <name>Human Readable Name for this Mirror.</name>
                <url>http://repo2.maven.org/maven2/</url>
            </mirror>
        </mirrors>
  • 相关阅读:
    DesignPattern系列__10单例模式
    DesignPattern系列__09设计模式概述
    DesignPattern系列__08UML相关知识
    DesignPattern系列__07合成复用原则
    DesignPattern系列__06迪米特原则
    爬取猫眼电影top100电影
    安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理
    python 通过使用pandas的实现的Excel的批量转换CSV文件的处理
    输入一个字符串,判断字符串中最大对称字串的长度
    面向对象六大设计原则(转载)
  • 原文地址:https://www.cnblogs.com/chch213/p/16304029.html
Copyright © 2020-2023  润新知