Maven 配置

1. 全局配置 settings.xml 详解

参考官网资料:https://maven.apache.org/settings.html

1.1. settings.xml 简介

是用来设置 maven 参数的配置文件。settings.xml 中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

  • settings.xml 是 maven 的全局配置文件
  • pom.xml 文件是本地项目配置文件

    1.1.1. settings.xml 文件位置

  • 全局配置:${maven.home}/conf/settings.xml

  • 用户配置:${user.home}/.m2/settings.xml

注意:用户配置优先于全局配置。${user.home}和和所有其他系统属性只能在 3.0+ 版本上使用。请注意 windows 和 Linux 使用变量的区别。

1.1.2. 配置优先级

局部配置优先于全局配置
配置优先级从高到低:pom.xml > user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的

1.2. settings.xml 元素详解

1.2.1. 顶级元素概览

  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>

1.2.2. LocalRepository 标签

  • 作用:该值表示构建系统本地仓库的路径。
  • 其默认值:~/.m2/repository ```${user.home}/.m2/repository
  1. #### 1.2.3. InteractiveMode 标签
  2. - 作用:表示 maven 是否需要和用户交互以获得输入。
  3. - 如果 maven 需要和用户交互以获得输入,则设置成 true,反之则应为 false。默认为 true
true
#### 1.2.4. UsePluginRegistry 标签

- 作用:maven 是否需要使用 plugin-registry.xml 文件来管理插件版本。
- 如果需要让 maven 使用文件`~/.m2/plugin-registry.xml` 来管理插件版本,则设为 true。默认为 false。
false
#### 1.2.5. Offline 标签

- 作用:表示 maven 是否需要在离线模式下运行。
- 如果构建系统需要在离线模式下运行,则为 true,默认为 false。
- 当由于网络设置原因或者安全因素,构建服务器不能连接远程仓库的时候,该配置就十分有用。
false
#### 1.2.6. PluginGroups 标签

- 作用:当插件的组织 id(groupId)没有显式提供时,供搜寻插件组织 Id(groupId)的列表。
- 该元素包含一个 pluginGroup 元素列表,每个子元素包含了一个组织 Id(groupId)。
- 当使用某个插件,并且没有在命令行为其提供组织 Id(groupId)的时候,Maven 就会使用该列表。默认情况下该列表包含了 org.apache.maven.plugins 和 org.codehaus.mojo。
org.codehaus.mojo
#### 1.2.7. Servers 标签

- 作用:一般,仓库的下载和部署是在 pom.xml 文件中的 repositories 和 distributionManagement 元素中定义的。然而,一般类似用户名、密码(**有些仓库访问是需要安全认证的**)等信息不应该在 pom.xml 文件中配置,这些信息可以配置在 settings.xml 中。
server001 my_login my_password ${usr.home}/.ssh/id_dsa some_passphrase 664 775
#### 1.2.8. Mirrors 标签

- 作用:为仓库列表配置的下载镜像列表。
planetmirror.com PlanetMirror Australia http://downloads.planetmirror.com/pub/maven2 central
#### 1.2.9. Proxies 标签

- 作用:用来配置不同的代理。
myproxy true http proxy.somewhere.com 8080 proxyuser somepassword *.google.com|ibiblio.org
#### 1.2.10. Profiles 标签

- 作用:根据环境参数来调整构建配置的列表。
- settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的**裁剪版本**。
- 它包含了id、activation、repositories、pluginRepositories 和 properties 元素。这里的 profile 元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是 settings.xml 文件的角色定位),而非单独的项目对象模型设置。如果一个 settings.xml 中的 profile 被激活,它的值会覆盖任何其它定义在 pom.xml 中带有相同 id 的 profile。
test
##### 1.2.10.1. properties 标签

- 作用:对应profile的扩展属性列表。
- maven 属性和 ant 中的属性一样,可以用来存放一些值。这些值可以在 pom.xml 中的任何地方使用标记`${X}`来使用,这里 X 是指属性的名称。属性有五种不同的形式,并且都能在 settings.xml 文件中访问。
${user.home}/our-project

> 注:如果该 profile 被激活,则可以在pom.xml中使用`${user.install}`


##### 1.2.10.2. Repositories 标签

- 作用:`<profiles>`标签的子标签。远程仓库列表,它是 maven 用来填充构建系统本地仓库所使用的一组远程仓库。
codehausSnapshots Codehaus Snapshots false always warn http://snapshots.maven.codehaus.org/maven2 default
#### 1.2.11. Activation 标签

- 作用:自动触发 profile 的条件逻辑。
- 如 pom.xml 中的 profile 一样,profile 的作用在于它能够在某些特定的环境中自动使用某些特定的值;这些环境通过 activation 元素指定。
- activation 元素并不是激活 profile 的唯一方式。settings.xml 文件中的 activeProfile 元素可以包含 profile 的 id。profile 也可以通过在命令行,使用 `-P` 标记和逗号分隔的列表来显式的激活(如,`-P test`)。
false 1.5 Windows XP Windows x86 5.1.2600 mavenVersion 2.0.3 ${basedir}/file2.properties ${basedir}/file1.properties

> 注:在 maven 工程的 pom.xml 所在目录下执行 `mvn help:active-profiles` 命令可以查看中央仓储的 profile 是否在工程中生效。


#### 1.2.12. pluginRepositories 标签

- 作用:发现插件的远程仓库列表。
- 和 repository 类似,只是 repository 是管理 jar 包依赖的仓库,pluginRepositories 则是管理插件的仓库。
- maven 插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。pluginRepositories 元素的结构和 repositories 元素的结构类似。每个 pluginRepository 元素指定一个 Maven 可以用来寻找新插件的远程地址。
#### 1.2.13. ActiveProfiles 标签

- 作用:手动激活 profiles 的列表,按照profile被应用的顺序定义activeProfile。
- 该元素包含了一组 activeProfile 元素,每个 activeProfile 都含有一个 profile id。任何在 activeProfile 中定义的 profile id,不论环境设置如何,其对应的 profile 都会被激活。如果没有匹配的 profile,则什么都不会发生。
- 例如,env-test 是一个 activeProfile,则在 pom.xml(或者 profile.xml)中对应 id 的 profile 会被激活。如果运行过程中找不到这样一个 profile,Maven 则会像往常一样运行。
env-test
## 2. 项目构建 pom.xml 配置详解(网上资源)

> 引用网上资源的最全面版本pom.xml,后期根据理解修改

<?xml version=”1.0” encoding=”UTF-8”?>

xxx xxx xxx xxx 4.0.0 xxx xxx jar 1.0-SNAPSHOT xxx-maven http://maven.apache.org A maven project to study maven. jira http://jira.baidu.com/banseon
Demo banseon@126.com banseon@126.com banseon@126.com http:/hi.baidu.com/banseon/demo/dev/ HELLO WORLD banseon banseon@126.com Project Manager Architect demo http://hi.baidu.com/xxx No -5 Project Manager Architect No Apache 2 http://www.baidu.com/banseon/LICENSE-2.0.txt repo A business-friendly OSS license scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk) scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk http://svn.baidu.com/banseon demo http://www.baidu.com/banseon true/false true/false true/false true/false true/false true/false true/false jdk版本,如:1.7 Windows XP Windows x86 5.1.2600 mavenVersion 2.0.3 /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ true/false true/false true/false true/false true/false true/false banseon-repository-proxy banseon-repository-proxy http://192.168.1.169:9999/repository/ default org.apache.maven maven-artifact 3.8.1 jar test spring-core org.springframework true true/false true/false banseon-maven2 banseon maven2 file://${basedir}/target/deploy banseon-maven2 Banseon-maven2 Snapshot Repository scp://svn.baidu.com/banseon:/usr/local/maven-snapshot banseon-site business api website scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web value
## 3. POM 文件帮助文档(网上资源)
### 3.1. 附件1:POM文件总体配置说明
xxx xxx xxx xxx 4.0.0 xxx xxx jar 1.0-SNAPSHOT xxx-maven http://maven.apache.org A maven project to study maven. jira http://jira.baidu.com/banseon
Demo banseon@126.com banseon@126.com banseon@126.com http:/hi.baidu.com/banseon/demo/dev/ HELLO WORLD banseon banseon@126.com Project Manager Architect demo http://hi.baidu.com/xxx No -5 Project Manager Architect No
Apache 2 http://www.baidu.com/banseon/LICENSE-2.0.txt repo A business-friendly OSS license scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk) scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk http://svn.baidu.com/banseon demo http://www.baidu.com/banseon true/false true/false true/false true/false true/false true/false true/false jdk版本,如:1.7 Windows XP Windows x86 5.1.2600 mavenVersion 2.0.3 /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ /usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/ true/false true/false true/false true/false true/false true/false banseon-repository-proxy banseon-repository-proxy http://192.168.1.169:9999/repository/ default org.apache.maven maven-artifact 3.8.1 jar test spring-core org.springframework true true/false true/false banseon-maven2 banseon maven2 file://${basedir}/target/deploy banseon-maven2 Banseon-maven2 Snapshot Repository scp://svn.baidu.com/banseon:/usr/local/maven-snapshot banseon-site business api website scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web value
### 3.2. 附件2:POM文件单项配置说明
#### 3.2.1. localRepository
usr/local/maven
#### 3.2.2. interactiveMode
true
#### 3.2.3. usePluginRegistry
false
#### 3.2.4. offline
false
#### 3.2.5. pluginGroups
org.codehaus.mojo
#### 3.2.6. proxies
myproxy true http proxy.somewhere.com 8080 proxyuser somepassword *.google.com|ibiblio.org
#### 3.2.7. servers
server001 my_login my_password ${usr.home}/.ssh/id_dsa some_passphrase 664 775
#### 3.2.8. mirrors
planetmirror.com PlanetMirror Australia http://downloads.planetmirror.com/pub/maven2 central
#### 3.2.9. profiles
test #### 3.2.10. Activation false 1.5 Windows XP Windows x86 5.1.2600 mavenVersion 2.0.3 ${basedir}/file2.properties ${basedir}/file1.properties #### 3.2.11. Repositories codehausSnapshots Codehaus Snapshots false always warn http://snapshots.maven.codehaus.org/maven2 default

#### 3.2.12. activeProfiles

env-test

```