【Maven】配置文件解析:setting.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>${user.home}/.m2/repository</localRepository>
  <interactiveMode>true</interactiveMode>
  <offline>false</offline>
  ...
</settings>
  • localRepository:本地仓库的路径。默认值为${user.home}/.m2/repository。
  • interactiveMode:是否需要和用户交互以进行输入?
    • true:需要
    • false:不需要
  • offline:这个构建系统是否应该在离线模式下运行?由于网络设置或安全原因,此元素对于无法连接到远程存储库的构建服务器很有用
    • true:是
    • false:不是

2 pluginGroups

使用插件且命令行中未提供 groupId 时会搜索该列表

<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">
  ...
  <pluginGroups>
    <pluginGroup>org.eclipse.jetty</pluginGroup>
  </pluginGroups>
  ...
</settings>
  • pluginGroups
    • 包含<pluginGroup>列表,每一个元素包含一个groupid
    • 使用插件且命令行中未提供 groupId 时会搜索<pluginGroup>列表
    • <pluginGroup>列表自动包含 org.apache.maven.pluginsorg.codehaus.mojo。、

3 servers

当需要连接到一个私有服务器的时候需要的认证信息

用于下载和部署的存储库由 POM的repositoriesdistributionManagement元素定义。但是,某些设置(例如usernamepassword )不应与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 https://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:想要连接的存储库/镜像服务器id*(不是登录用户的 ID) 。*即与distributionManagementrepository元素的id相匹配
  • username , password:通过用户名密码连接。
  • privateKey , passphrase:通过密钥连接
    • privateKey:私钥位置,默认为 ${user.home}/.ssh/id_dsa
    • passphrase:私钥密码
  • filePermissions , directoryPermissions:当存储库文件目录在部署被创建时使用的权限。每个的合法值是对应于 *nix 文件权限的三位数字,例如 664 或 775。

注意:如果您使用私钥登录服务器,请确保省略<password>元素。否则,密钥将被忽略。

2.1.0+ 中添加了一项新功能 - 服务器密码和密码短语加密

4 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用于区分mirror元素,并在连接到镜像时从<servers>部分中 选择相应的凭据 。
  • url:此镜像的基本 URL。
  • mirrorOf:这是被镜像的存储库的id。更高级的映射如repo1,repo2or*,!inhouse 也是可能的。

5 proxies

设置通过代理访问远程仓库,有些私服仓库是不予许直接访问的

<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">
  ...
  <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元素。
  • active:此代理是否处于活动状态。一次只能有一个处于活动状态
  • protocol , host , portprotocol://host:port的代理,被分成离散的元素。
  • username , password:代理服务器的登录名和密码。
  • nonProxyHosts:这是不应被代理的主机列表。常见的分隔符有:“|”和“,”

6 profiles

用于指定一系列的profile。会覆盖pom.xml或profiles.xml中的profile

  • 激活方式一:满足<Activation>条件时
  • 激活方式二:使用mvn … -P xxx指令,指定激活id为xxx的profile
  • 激活方式三:被<activeProfile>指定
  • 要查看哪个配置文件将在某个构建中激活,请使用 maven-help-plugin.

6.1 Activation

<profile>的激活条件

<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">
  ...
  <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>

注意:当所有指定的条件都满足时,激活就会发生。

  • activeByDefault:没有profile处于激活状态的时候,该profile是否自动被激活。
    • jdk:需满足条件的jdk版本。还可以用一个范围来表示,例如[1.4,1.8),[1.4,1.8]。
  • os:表示当操作系统满足条件的时候激活(一般项目不做限制)。
  • property:表示当Maven检测到了这样一个键值对的时候激活该profile (例如上面代码中的可以在执行命令中:mvn compile –DmavenVersion=2.0.3)
  • file:表示当文件存在或不存在的时候激活

6.2 Properties

Maven 属性是值占位符。它们的值可以通过使用表示法在 POM 中的任何位置访问 ${X}X是属性的时候。它们有五种不同的样式,都可以从settings.xml文件中访问:

  1. env.X: 用 env 为变量添加前缀。将返回 shell 的环境变量。例如,${env.PATH}包含 $path 环境变量(%PATH%在 Windows 中)。

  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()访问的所有属性,例如${java.home}.

  5. x: 在 元素或外部文件中设置,该值可以用作${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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
      ...
      <profiles>
        <profile>
          ...
          <properties>
            <user.install>${user.home}/our-project</user.install>
          </properties>
          ...
        </profile>
      </profiles>
      ...
    </settings>
    

    如果此配置文件处于活动状态,则可以从 POM 访问该属性${user.install}

6.3 Repositories

定义远程仓库的地址,用来填充本地存储库。

<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">
  ...
  <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>
      ...
    </profile>
  </profiles>
  ...
</settings>
  • id:指定远程仓库的唯一标识符
  • name:远程仓库名称
  • url:远程仓库地址
  • releases、snapshots:这是对于工件的类型的限制。一般来说snapshots版本代表正在开发中的版本,release代表比较稳定的发布版本,
    • enabled:是否为相应的类型启用此存储库。
    • updatePolicy:指定尝试更新的频率,Maven 会将本地 POM 的时间戳(存储在存储库的 maven-metadata 文件中)与远程进行比较。可选值有always、daily(默认)、interval:minutes 和 never 。
    • checksumPolicy:表示校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。当文件在部署到仓库时检验。
  • layoutdefault还是legacy。在上面对存储库的描述中,它们都遵循一个共同的布局,这大多是正确的。Maven 2 的存储库有一个默认布局;然而,Maven 1.x 有不同的布局。

6.4 Plugin Repositories

插件存储库,与<Repositories>类似

<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">
  ...
  <profiles>
    <profile>
      ...
      <pluginRepositories>
        <pluginRepository>
          <id>myPluginRepo</id>
          <name>My Plugins repo</name>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <url>https://maven-central-eu....com/maven2/</url>
        </pluginRepository>
      </pluginRepositories>
      ...
    </profile>
  </profiles>
  ...
</settings>

7 activeProfiles

对于所有的pom都处于活跃状态的profile

<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-test</activeProfile>
  </activeProfiles>
</settings>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愿你满腹经纶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值