zoukankan      html  css  js  c++  java
  • Apache Maven(七):settings.xml

    settings.xml 文件中包含settings标签,这个标签可以配置如何去执行Maven。其中包括本地存储库位置,备用远程存储库服务器和身份验证信息等值。

    有如下两个位置可能存放这settings.xml 文件:

    • Maven 安装目录:${maven.home}/conf/settings.xml
    • 用户的目录:${user.home}/.m2/settings.xml

    前者的settings.xml 是一个全局的设置文件,后者的settings.xml  是一个用户设置文件,如果两者都存在的话,则将内容进行合并处理,并且用户的settings.xml 占主导地位。

    如果你想创建一个用户的设置文件,那么最好的办法就是复制全局的设置文件到${user.home}/.m2/目录下,全局的maven设置文件是一个包含了注释的示例模板。因此可以通过它调整你需要的内容。

    下面是设置文件的主要标签:

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3       xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                           https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5       <localRepository/>
     6       <interactiveMode/>
     7       <usePluginRegistry/>
     8       <offline/>
     9       <pluginGroups/>
    10       <servers/>
    11       <mirrors/>
    12       <proxies/>
    13       <profiles/>
    14       <activeProfiles/>
    15     </settings>

    可以使用$ {user.home}和一些其他的系统属性值插入settings.xml属性中;${env.HOME}等环境变量。

    简单设置

    settings.xml设置一些简单是值,如下示例:

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5   <localRepository>${user.home}/.m2/repository</localRepository>
     6   <interactiveMode>true</interactiveMode>
     7   <usePluginRegistry>false</usePluginRegistry>
     8   <offline>false</offline>
     9   ...
    10 </settings>
    • localRepository: 这个属性用来配置构建系统的本地存储库,默认值为:${user.home}/.m2/repository。
    • interactiveMode: Maven需要用户输入时,是否需要提示你。默认为true。
    • usePluginRegistry:如果你需要使用${user.home}/.m2/plugin-registry.xml文件来管理插件的版本,那么就设置为true。默认为false。
    • offline: 如果构建系统需要使用离线模式运行,则设置为true,默认为false。由于网络和安全问题,对于那些无法连接到远程存储库是很有用的。

    pluginGroups

    pluginGroups 标签包含pluginGroup标签列表,每一个pluginGroups标签包含一个groupId,当你使用插件并且在命令行中并没有提供groupId时,将搜索此列表。该列表自动包含org.apache.maven.pluginsorg.codehaus.mojo的groupId。

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5   ...
     6   <pluginGroups>
     7     <pluginGroup>org.mortbay.jetty</pluginGroup>
     8   </pluginGroups>
     9   ...
    10 </settings>

    例如给出如上示例时,当Maven执行org.mortbay.jetty:jetty-maven-plugin:run命令时,可以直接使用如下命令执行:

    mvn jetty:run

    servers

    下载和部署的存储库由POM的repositories 和 distributionManagement 元素定义。但某些值(如用户名或密码)不应该由POM设置,这种类型的信息应该存放在settings文件中。

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5   ...
     6   <servers>
     7     <server>
     8       <id>server001</id>
     9       <username>my_login</username>
    10       <password>my_password</password>
    11       <privateKey>${user.home}/.ssh/id_dsa</privateKey>
    12       <passphrase>some_passphrase</passphrase>
    13       <filePermissions>664</filePermissions>
    14       <directoryPermissions>775</directoryPermissions>
    15       <configuration></configuration>
    16     </server>
    17   </servers>
    18   ...
    19 </settings>
    • id: 这是与Maven试图连接的存储库/镜像的id元素相匹配的服务器的标识(不是用户登录的标识)。
    • username,password:这些元素显示为一对,表示对此服务器进行身份验证所需的登录名和密码。
    • privateKey,passphrase:与前面的两个元素一样,如果需要,该对将指定私钥的路径(默认为${user.home}/.ssh/id_dsa)和密码。该passphrase和password的元素可能在将来被外部化,但现在他们必须设置在纯文本的settings.xml文件。
    • filePermissions,directoryPermissions:在部署时创建存储库文件或目录时,这些是要使用的权限。每一个的合法值都是与*nix文件权限对应的三位数字,例如664或775。

    注:如果你使用私钥登录服务器,请确保省略了password元素,否则密钥不生效。

    在Maven 2.1.0+ 添加了一项新功能,对password 和 passphrase进行加密。具体加密信息可以查看官方介绍

    mirrors

    <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和用户名。这个ID可以区分不同的mirror元素。并在连接镜像时从<servers>部分选择相应的凭据。
    • url: 此镜像的基本网址。构建系统将使用此URL来连接到存储库,而不是原始存储库URL。
    • mirrorOf:  这是中央存储库的ID。例如,要指向Maven 中央存储库(https://repo.maven.apache.org/maven2/)的镜像,则将该元素值设置为central。还有更多的值如:repo1,repo2 或 *,!inhouse,这个值不能与镜像id一样。

    proxies

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5   ...
     6   <proxies>
     7     <proxy>
     8       <id>myproxy</id>
     9       <active>true</active>
    10       <protocol>http</protocol>
    11       <host>proxy.somewhere.com</host>
    12       <port>8080</port>
    13       <username>proxyuser</username>
    14       <password>somepassword</password>
    15       <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
    16     </proxy>
    17   </proxies>
    18   ...
    19 </settings>
    • id: 此代理的唯一标识ID,用来区分proxy元素。
    • active: 如果此代理处于活动状态,则设置为true。这对于声明一堆代理非常有用,但一次只能激活一个代理。
    • protocol,host,port: 协议,主机,端口。
    • username,password:这表示一对,用来验证此代理服务器所需要的用户名和密码。
    • nonProxyHosts: 这是不需要代理的主机列表。使用|进行分割,也可以使用逗号分隔。

    Profiles

    settings.xml 中的profile元素可以截断 pom.xml 的profile元素。它包含activation, repositories, pluginRepositories 和 properties 元素。该profile元素仅包含这四个元素,因为它们与构建系统作为一个整体,不是单个项目的设置。

    如果某个prefile在settings.xml中处于激活状态,则其值将会覆盖POM 或 profile.xml文件中相同ID的profile值。

     1 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
     4                       https://maven.apache.org/xsd/settings-1.0.0.xsd">
     5   ...
     6   <profiles>
     7     <profile>
     8       <id>test</id>
     9       <activation>
    10         <activeByDefault>false</activeByDefault>
    11         <jdk>1.5</jdk>
    12         <os>
    13           <name>Windows XP</name>
    14           <family>Windows</family>
    15           <arch>x86</arch>
    16           <version>5.1.2600</version>
    17         </os>
    18         <property>
    19           <name>mavenVersion</name>
    20           <value>2.0.3</value>
    21         </property>
    22         <file>
    23           <exists>${basedir}/file2.properties</exists>
    24           <missing>${basedir}/file1.properties</missing>
    25         </file>
    26       </activation>
    27       ...
    28     </profile>
    29   </profiles>
    30   ...
    31 </settings>
    • jdk:activation 在jdk元素中内置了一个以Java为中心的检查。如果测试在与给定前缀相匹配的jdk版本号下运行,这将激活。在上面的例子中,1.5.0_06将匹配。
    • os:os元素可以定义上面显示的某些操作系统特定的属性。
    • property: 如果Maven检测到相应的name = value对的属性(可以在POM中取消$ {name}的值),该配置文件将激活。
    • file:最终,一个给定的文件名可能通过文件的存在或缺失来激活配置文件。
  • 相关阅读:
    [导入]CodeSmith基础(六)
    [导入]CodeSmith基础(五)
    POJ 2229 Sumsets(经典2次幂和问题)
    UVa 10820 Send a Table(欧拉函数)
    UVa 571 Jugs(经典倒水问题)
    UVa 10717 Mint(LCM)
    UVa 10791 Minimum Sum LCM(素因子分解)
    汇编的艺术(01)sizeof operator
    UVa 11121 Base 2(负数进制)
    UVa 106 Fermat vs. Pythagoras(毕达哥拉斯定理)
  • 原文地址:https://www.cnblogs.com/peter1018/p/9214411.html
Copyright © 2011-2022 走看看