• maven系列--settings.xml


    安装maven,会有一份settings.xml文件,我们可以理解这份文件就是用来管理本地maven的一系列东西的。这里我先贴出一份完整的原始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">
    	<!-- 该值表示构建系统本地仓库的路径。其默认值为 ${user.home}/.m2/repository 。 -->
    	<localRepository>D:
    epository</localRepository>
    	<!-- 该元素包含一个 pluginGroup 元素列表,每个子元素包含了一个 groupId 。当我们使用某个插件,并且没有在命令行为其提供 
    		groupId 的时候, Maven就会使用该列 表。默认情况下该列表包含了 org.apache.maven.plugins 。 -->
    	<pluginGroups></pluginGroups>
    	<!-- 代理 -->
    	<proxies></proxies>
    	<!-- POM中的 distributionManagement 元素定义了部署的仓库。然而,一些设置如安全证书不应该和 pom.xml 一起分发。这种类型的信息应该存在 
    		于构建服务器上的 settings.xml 文件中。 -->
    	<servers></servers>
    	<!-- 镜像 -->
    	<mirrors></mirrors>
    	<!-- settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。它包含了 activation , repositories , pluginRepositories 
    	和 properties 元素。这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml 文件的角色定位),而非单
    	 独的项目对象模型设置。如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。 -->
    	<profiles></profiles>
    	<!-- 简单值 -->
    	<!-- 如果Maven需要和用户交互以获得输入,则设置成 true ,反之则应为 false 。默认为 true 。 -->
    	<interactiveMode>true</interactiveMode>
    	<!-- 如果需要让Maven使用文件  ${user.home}/.m2/plugin-registry.xml 来管理插件版本,则设为 true 。默认为 false 。 -->
    	<usePluginRegistry>false</usePluginRegistry>
    	<!-- 如果构建系统需要在离线模式下运行,则为 true ,默认为 false 。当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,
    	该配置就十分有用。 -->
    	<offline>false</offline>
    
    </settings>
    

    当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置。这包含了本地仓库位置,远程仓库服务器以及认证信息等。

    • settings.xml存在于两个地方:
    1.安装的地方:$M2_HOME/conf/settings.xml
    2.用户的目录:${user.home}/.m2/settings.xml

    注意:前者又被叫做全局配置,后者被称为用户配置。如果两者都存在,它们的内容将被合并,并且用户范围的settings.xml优先。


    • 服务器
    用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。但是某些配置例如username和password就不应该随着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
                http://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:这是Server的ID(不是登录进来的user),与Maven想要连接上的repository/mirror中的id元素相匹配。
    username,password:这两个元素成对出现,表示连接这个server需要验证username和password。
    privateKey,passphrase:与前两个元素一样,这两个成对出现,分别指向了一个私钥(默认的是${user.home}/.ssh/id_dsa)和一个passphrase。passphrase和password元素可能在将来被客观化,但是现在必须以文本形式在settings.xml中设置。
    filePermissions,directoryPermissions:当一个仓库文件或者目录在部署阶段被创建的时候,就必须用到权限许可。他们合法的值是三个数字,就像*nix中的文件权限,例如:664,775.
    注意:如果你使用了一个私钥来登录server,那么password元素必须被省略,否则私钥将被忽视。

    密码加密:一个新特征:服务器password和passphrase加密已经被升到2.1.0+


    • 镜像

    <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">
        ...
        <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被用来区分mirror元素,并且当连接时候被用来获得相应的证书。
    url:镜像基本的URL,构建系统敬将使用这个URL来连接仓库,而不是原来的仓库URL。
    mirrorOf:镜像所包含的仓库的Id。例如,指向Maven central仓库的镜像(http://repo1.maven.org/maven2/),设置这个元素为central。更多的高级映射例如repo1,repo2 或者*,!inhouse都是可以的。没必要一定和mirror的id相匹配。


    • 代理
    <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">
        ...
        <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:proxy的唯一标识,用来区别proxy元素。
    active:当proxy被激活的时候为true。当申明的代理很多的时候,这个很有用,但是同一时间仅有一个被激活。
    protocol,host,port:代理地址protocol://host:port的分散形式。
    username,password:两个元素成对出现,提供连接proxy服务器时的认证。
    nonProxyHosts:这里列出了不需要使用代理的hosts。列表的分隔符是proxy服务器想要的类型。上面例子使用了pipe分隔符,逗号分隔符也比较通用。

    • 配置文件

    settings.xml中的profile是pom.xml中的profile的简洁形式。它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。


    • 激活(activation)

    activations是profile的关键,就像POM中的profiles,profile的能力在于它在特定情况下可以修改一些值。而这些情况是通过activation来指定的。

    <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">
        ...
        <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就被触发,而且不需要一次性全部达到。
    jdk:在jdk元素中,activation有一个内建的,java版本检测。如果检测到jdk版本与期待的一样,那么就激活。在上面的例子中,1.5.0_06是满足的。
    os:os元素可以定义一些上面所示的操作系统特定的属性。
    property:如果Maven检测到相应的名值对的属性,那么,这个profile将被激活。
    file:如果给定的文件存在,或者不存在那么将激活这个profile。
    activation并不是唯一激活profile的途径。settings.xml中的activeProfile包含了profile的id。他们也可以通过命令行来显式的激活,例如-P test。
    如果你想查看在一个构建过程中有哪些profile会被激活。就使用maven-help-plugin  mvn help:active-profiles


    • 属性(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}

    <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">
        ...
        <profiles>
            <profile>
                ...
                <properties>
                    <user.install>${user.home}/our-project</user.install>
                </properties>
                ...
            </profile>
        </profiles>
        ...
    </settings>

    如果这个profile被激活,那么属性${user.install}就可以被访问了。


    • 仓库(repositories)
    仓库是Maven用来构筑构建系统的本地仓库的远程项目集合。它来自于被Maven叫做插件和依赖的本地仓库。不同的远程仓库包含不同的项目,当profile被激活,他们就会需找匹配的release或者snapshot构件。

    <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">
        ...
        <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>
            ...
            </pluginRepositories>
            ...
            </profile>
        </profiles>
        ...
    </settings>
    releases,snapshots:这是各种构件的策略,release或者snapshot。因了这两个集合,POM可以在单个的仓库中不依赖于另外一个的策略而改变当前策略。例如:一个人可能只下载snapshot用来开发。

    enable:true或者false,来标记仓库是否为各自的类型激活(release 或者 snapshot)。
    updatePolicy:这个元素指明了更新的频率。Maven会比较本地POM与远程的时间戳。可选的项目为:always,daily,interval:X,nerver。
    checksumPolicy:当Maven向仓库部署文件的时候,它也部署了相应的校验和文件。可选的为:ignore,fail,warn,或者不正确的校验和。
    layout:在上面描述仓库的时候,我们提到他们有统一的布局。这完全正确。使用这个来表明它是default还是legacy。


    • 插件仓库(plugin repositories)

    仓库包含了两种重要类型的构件。第一种是用来做其他构件依赖的构件,这是在中央仓库中的大多数插件。另外一种类型的构件就是插件。Maven的插件本身就是一种特殊的构件。因此,插件仓库被从其他仓库中分离出来。无论怎么说,pluginRepositories元素模块的结构与repositories模块很相似。luginRepository元素指向一个可以找到新插件的远程地址。

    • 激活配置(Active Profiles)
    <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">
        ...
        <activeProfiles>
            <activeProfile>env-test</activeProfile>
        </activeProfiles>
    </settings>
    settings.xml最后一个谜题是activeProfiles元素。它包含一系列的activeProfile元素,每个都有一个profile id的值,任何profile id被定义到activeProfile的profile将被激活,不管其他的环境设置怎么样。如果没有匹配的profile被找到,那么就什么事情也不做。例如:如果env-test是一个activeProfile,一个在pom.xml或者profile.xml中的具有相应id的profile将被激活。如果没有这样的profile被找到,就什么事也不做,一切照常。


    最后settings.xml总结:

    一般情况下我们不需要修改settings.xml文件里面的内容,我们只是重新设置下这个文件的位置,然后修改里面的本地库标签(localRepository)。如果公司里面不是连接的远程公共仓库,有自己的仓库的话,就自己新建一个server就OK。

    <localRepository>@maven_localRepository@</localRepository>
        <server>
            <id>branchitech-maven</id>
            <username>@ivy_username@</username>
            <password>@ivy_passwd@</password>
        </server>
    


  • 相关阅读:
    selenium+phantomjs爬取bilibili
    使用 python 开发 Web Service
    OBIEE 立方刷新的问题
    解析OracleOLAP使用MView刷新Cube
    Codeforces Round #755 (Div. 2, based on Technocup 2022 Elimination Round 2)(CF1589)题解
    Codeforces Round #754 (Div. 2)(CF1605)题解
    完美解读Linux中文件系统的目录结构
    C#中获取程序当前路径的集中方法
    30个优秀.net在线学习资源站点
    如何删除windows service(转帖)
  • 原文地址:https://www.cnblogs.com/LinkinPark/p/5232990.html
Copyright © 2020-2023  润新知