Maven学习之 Settings

时间:2022-07-12 17:35:35

虽然天天在用,但是没有系统的学习过,总觉得别扭。

只能用于Java项目。

约定:

repository  翻译成 仓库

build 翻译成 构建

build system 翻译成 构建系统

build server 翻译成 构建服务器

central (repository) 翻译成 *仓库--指maven官方仓库

配置:

MAVEN_OPTS 环境变量:该变量包含了用于启动执行Maven的JVM,可用于为全局的Maven提供额外的配置。例如,JVM 内存设置:-Xms256m -Xmx512m

settings.xml 文件:位于 USER_HOME/.m2 下面,包含Maven使用的设置。

.mvn 文件夹:位于项目的*文件夹中,其 maven.config 和 extensions.xml 文件包含了运行Maven的特定配置。

最佳实践 - 使用一个 Repository Manager

其实就是搭一个私服,略。

Settings Reference


介绍

快速浏览

settings.xml中的 <settings> 元素,是用于定义那些用来配置Maven执行的值 -- 以多种方式,例如 pom.xml,但不会绑定到某个特定的项目,或分发给特定的听众。

包含的值有:本地仓库位置、替代的远程仓库服务器、认证信息等。

settings.xml 可能存在于两个位置(其实IDE不受此限制):

  • Maven安装路径:${maven.home}/conf/settings.xml
  • 用户文件夹中:${user.home}/.m2/settings.xml

前者的settings.xml 也被叫做全局设置,后者被叫做用户设置。如果都存在,它们的内容会融合,特定用户的settings.xml 占优 (就是局部覆盖全局)。

提示:如果你需要创建特定用户的设置,最好是将全局的设置复制到你的${user.home}/.m2文件夹中。 Maven的默认 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">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>

settings.xml 的内容可以插入下列表达式:

  • ${user.home} 和其他系统属性 (自Maven 3.0起)
  • ${env.HOME} 等环境变量

注意:在settings.xml的profiles中定义的properties不能用来作为插入表达式。

Settings Details

Simple Values

<settings>元素的一半都是简单的值,用于描述构建系统的元素 -- 始终活动状态(就是全局有效的意思吧)。

<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>
<usePluginRegistry>false</usePluginRegistry>
<offline>false</offline>
...
</settings>
  • localRepository:这个值是构建系统的本地仓库的路径。 默认值是 ${user.home}/.m2/repository 。该元素对a main build server来说特别有用,它允许所有登陆的用户从一个通用的本地仓库中构建。
  • interactiveMode:true代表Maven应该试图与用户交互--即需要用户输入,默认 false。
  • usePluginRegistry:true代表Maven应该使用 ${user.home}/.m2/plugin-registry.xml 文件来管理插件版本,默认 false。
  • offline:true代表该build system 应该以离线模式操作,默认false。当build servers 不能连接到远程仓库时有用。

Plugin Groups

该元素包含了多个<pluginGroup> 元素(即列表),每个都有一个groupId。 当使用一个插件,而没有在命令行中提供其groupId时(???),就会搜索该列表。该列表自动包含org.apache.maven.pluginsorg.codehaus.mojo

<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.mortbay.jetty</pluginGroup>
</pluginGroups>
...
</settings>

例如,根据上面的设置,Maven 命令行可能以简写命令来执行org.mortbay.jetty:jetty-maven-plugin:run

  1. mvn jetty:run

Servers

用来下载和部署的仓库是定义在POM的 repositoriesdistributionManagement 元素中。然而,特定的设置,如 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
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 ,需要匹配Maven试图连接的 repository/mirror 的 id。

username, password:该元素成对出现,用于指定该服务器认证需要的登录和密码。

privateKey, passphrase:如同前两个元素,这一对指定了private key的路径(默认是 ${user.home}/.ssh/id_dsa)和一个passphrase -- 如果需要的话。嗯嗯,以后可能支持外部化,但现在只能是纯文本了。

filePermissions, directoryPermissions:在部署时需要在仓库中创建文件或文件夹,这就涉及到权限。其合法的值是 *nix 文件权限,例如 664、775、777之类的。

注意:如果你使用一个private key来登陆服务器,务必去掉<password> 元素。否则会忽略掉该key。

Password Encryption

2.1.0+  新加的一个新特性 -- 服务器的password 和 passphrase encryption。略。

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>部分捡拾相应的credentials
  • url:该镜像的基本URL。构架系统会使用该URL来连接到一个仓库,而非原本的仓库URL。
  • mirrorOf:当前镜像所镜像的仓库的id。

想深入了解镜像部分,请参阅 Guide to Mirror Settings

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的唯一标识符。用来区分proxy。
  • active:true代表该proxy是活动的。可以声明一组代理,而只使用其中的一个。
  • protocol, host, port:该代理的protocol://host:port,拆分在不同的元素中。
  • username, password:代理服务器的认证信息。
  • nonProxyHosts:不需要被代理访问的主机。使用”|”或逗号来连接多个值。

Profiles

settings.xml 中的 profile元素,是pom.xml profile元素的缩减版本。它由activation, repositories, pluginRepositories 以及 properties元素组成。profile元素只包含这四个元素,是因为他们是对整个build system负责,而不是某个单独的项目的设置。

如果settings中一个profile是active的,那它的值会覆盖任何POM 或 profiles.xml 中相同ID的profiles。

Activation

activation是profile的一个key。如同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
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>

注意:当且仅当所有指定的条件都满足时,才会出现activation。当然,你可以不必全部指定(例如仅仅指定jdk,或者干脆都不指定)。

  • jdk:只有jdk的版本不低于指定的值时,才会满足该条件。自Maven 2.1起,也支持指定版本的范围,详见maven-enforcer-plugin
  • os:指定操作系统。更多详见maven-enforcer-plugin
  • property:Maven能探测的一个property(可以在POM中通过 ${name} 方式获取其值)。
  • file:Finally, a given filename may activate the profile by the existence of a file, or if it is missing.

注意:activation元素不是唯一激活profile的方式。settings.xml中还有一个activeProfile元素 -- 带有profile id。可以通过命令行显式的激活相应profile -- 通过-P标志后面跟随逗号间隔的列表。

Properties

Maven的properties都是值占位符,就像Ant中的properties。在POM中通过 ${X}可以访问 -- X 就是property。有5种不同的形式:

  • env.X:前缀"env." 会返回shell的环境变量。例如,${env.PATH}  包含了$path 环境变量 (在windows中是 %PATH%)。
  • project.x:POM中的"点"路径,会包含相应元素的值。例如,通过${project.version}来访问<project><version>1.0</version></project>
  • settings.x:在settings.xml中的"点"路径,会包含相应元素的值。例如,通过${settings.offline}来访问<settings><offline>false</offline></settings>
  • Java System Properties:java.lang.System.getProperties()中的所有properties都可以作为POM properties。例如,${java.home}。
  • 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
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>

这里,如果该profile是active的,那可以从POM中访问property  ${user.install}

Repositories

repositories 是项目的远程集合,Maven用其来填充构建系统的本地仓库。Maven从本地仓库中调用其插件和依赖。不同的远程仓库可能包含不同的项目,Maven会基于active profile 来搜索它们以匹配release还是snapshot artifact。

<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>
<pluginRepositories>
...
</pluginRepositories>
...
</profile>
</profiles>
...
</settings>

releases, snapshots:artifact的每个类型都有这种策略:Release 或者 snapshot。通过这两种设置,POM就能改变策略。。。例如,可能只想启用snapshot下载 -- 用于开发目的。--- 简单的说,就是类似正式版和开发版,给repository设置了之后,Maven就知道这里能不能下正式版、能不能下开发版。

enabled:true代表支持该类型,false不支持。

updatePolicy:用于指定更新频率。Maven会对比本地POM的时间戳(存储于一个仓库的maven-metadata文件)和远程的。选项是:always、daily(默认)、interval:X(整数,分钟)、never。

checksumPolicy:当Maven部署文件到仓库中时,它还会部署相应的checksum文件。选项是:ignore、fail、warn。

layout:在上面的仓库描述中有提到,他们都遵守一个通用的layout。这几乎正确。Maven 2 有一个默认的layout;然而Maven 1.x 有一个不同的layout。本元素用来指定是哪种:default 或者 legacy。

Plugin Repositories

仓库是artifacts的两种主要类型的家。第一种是被用作其他artifacts的依赖的artifacts。在*仓库里,这样的插件是主流。另一种artifact类型是插件。Maven插件是一个特别的artifact类型。鉴于此,插件仓库可以与其他仓库区分开来。无论如何下,pluginRepositories元素块的结构类似于 repositories元素。每个pluginRepository 元素都指定了一个远程位置 - Maven能够在此找到新的插件。

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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<activeProfiles>
<activeProfile>env-test</activeProfile>
</activeProfiles>
</settings>

settings.xml 的最后一块。这里包含了一组activeProfile 元素,每个都有一个profile id。任何被定义为activeProfile的profile id都是active的 - 无论任何环境设置。如果没有找到匹配的profile,什么都不会发生。

例如,如果 env-test 是一个 activeProfile,那么pom.xml或profile.xml中相应的profile就是active的。如果没有找到该profile,那么会按常规方式执行。

官方文档链接:

http://maven.apache.org/settings.html