maven

abstract

pom.XML

maven坐标

Maven坐标元素包括 groupId、artifactId、version、packaging、classifier.
groupId:定义当前Maven项目隶属的实际项目。
artifactId: 定义实际项目中的一个Maven模块。
version: 定义Maven项目当前所处的版本。
packaging: 定义Maven项目的打包方式。
classifier: 帮助定义构件输出的一些附属构件。

传递性依赖

首先我们要了解什么叫直接依赖,A依赖于B,B就是A的直接依赖。
A->B,B->(C,D)。C、D都是A的传递性依赖。

我们在使用A的时候只需要指出他的直接依赖,maven会自动帮我们解析出我们所需要的间接依赖,而不会引入多余的包。

如果间接依赖引用了同一个项目,但版本不同时,maven解析有两个优先原则:

  1. 深度策略。
    A->B->C(1.0) A->C(2.0)。此时导入C(2.0)
  2. FCFS策略。
    A->B->C(1.0) A->B->C(2.0)。此时导入C(1.0)

排除依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.alibaba.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>3.2.7</version>
<exclusions>
<exclusion>
<groupId>apache-lang</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
</exclusions>
</dependency>

引入了rocketmq,但是不想引入rocketmq里面的apache-lang就可以用exclusions元素进行排除。

排除的时候只需要定位groupIdartifactId就可以确定这个依赖。

setting.XML

localRepository

本地仓库路径
默认值为:$ {user.home} /.m2 / repository

1
<localRepository>D:/Program Files/Apache/maven-repository</localRepository>

interactiveMode

Maven是否应该尝试与用户进行交互以进行输入。
默认为:true

usePluginRegistry

Maven是否单独使用plugin-registry.xml文件来管理插件版本
默认值为:false

offline

设置maven是否应该在全离线模式下运行。
默认值为:false

proxies

proxies表示maven的代理

1
2
3
4
5
6
7
8
9
10
11
12
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>

proxies下可以设置多个proxy,使用ID进行唯一标识区分。

  1. id: 每个代理的唯一标识.
  2. active: 代理的激活状态,默认值为:true。设置多个代理时,使用第一个activetrue的代理。
  3. protocol: 代理使用的协议。默认值为:http
  4. username: 代理需要认证时的用户名。
  5. password: 代理需要认证时的密码。
  6. host: 主机名
  7. port: 端口号,默认值为:8080
  8. nonProxyHosts: 表示指定哪些主机名不需要代理,可以用”|” 分隔多个主机名,也支持通配符”*“;

server

用于连接远程仓库时的安全认证

1
2
3
4
5
6
7
8
9
10
11
12
<servers>
<server>
<id>nexus-releases</id>
<username>deployment</username>
<password>deployment</password>
<privateKey>${usr.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</serves>

  1. id: id是最关键的,这个id必须与需要认证的repository元素的id完全一致才行,换句话说,正式这个id将认证信息和仓库配置联系在了一起。
  2. username: 用户名
  3. password: 密码
  4. privateKey: 鉴权时使用的私钥位置
  5. passphrase: 鉴权时使用的私钥密码。
  6. filePermissions: 文件被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限(permission)。
  7. directoryPermissions: 目录被创建时的权限
  8. configuration: 传输层额外的配置项

mirrors

仓库的镜像

1
2
3
4
5
6
7
8
<mirrors>
<mirror>
<id>nexus</id>
<name>all repository mirror</name>
<url>http://172.16.21.3:8081/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

id,name,url与仓库的配置相同

  1. id: 仓库的唯一标识。
  2. name: 仓库名
  3. url: 仓库的地址.http://maven.net.cn/content/groups/public/ 是 中央仓库 http://repo1.maven.org/maven2/ 在中国的镜像。
  4. mirrorOf:*表示任何对中央仓库的请求都会被转到镜像仓库中。

    profile

    个性配置文件,可以通过不同的方式激活。激活条件全部满足时激活该配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<profiles>
<profile>
<id>test</id>
<activation>
···
</activation>

<repositories>
<repository>
···
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
···
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
  1. id: 配置文件的唯一标识
  2. activation: 激活方式
  3. repositories: 依赖仓库
  4. pluginRepositories: 插件仓库

activation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
<os>
<name>Windows 7</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>
  1. activeByDefault: 当设置为true时,没有其他profile激活时,自动激活。
  2. jdk: 当JDK版本满足时激活,可以用开闭区间表示一个JDK满足的范围。
  3. os: 当操作系统满足时激活
    1. name: 操作系统
    2. family: 操作系统类型
    3. arch: 操作系统位数
    4. version: 操作系统版本
  4. property: 键值对的形式,当只存在name时。hello属性存在,即可激活。当name,value都存在时,hello属性存在,并且hello属性的value为world时可以激活。
    1. name: hello
    2. value: world
  5. file: 表示当文件存在或不存在的时候激活
    1. exists: 该路径上的文件存在时激活
    2. missing: 该路径上的文件不存在时激活

repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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>

repositories中可以有多个repository,使用ID进行唯一标识区分。

  1. id: 每个中央仓库的唯一标识。Maven自带的中央仓库使用的id为central,如果其他仓库声明也用该id,就会覆盖中央仓库的配置。
  2. name: 仓库名
  3. url: 中央仓库的地址。
  4. releases: 表示开启仓库的发布版本下载支持。
    1. enabled: 启用状态。默认值为:true
    2. updatePolicy: 更新时间,可选值有(always、daily、interval:minutes和never)
      1. always: 始终
      2. daily: 每天
      3. interval:minutes: 指定时间间隔(单位为分钟)
      4. never: 从不
    3. checksumPolicy: 当Maven在部署项目到仓库的时候会连同校验文件一起提交,checksumPolicy表示当这个校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。
      1. ignore: 忽略
      2. fail: 失败
      3. warn: 警告
  5. snapshots: 表示关闭仓库的快照版本下载支持。参数与releases类似

pluginRepository

插件仓库与依赖仓库配置类似

1
2
3
4
5
6
7
8
9
10
11
12
13
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

activeProfiles

手动激活的构建配置文件列表,按照应用顺序指定。

1
2
3
4
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>

上述配置表示激活所有。

pluginGroups

插件组

1
2
3
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

添加了上面的插件后就可以使用

1
mvn jetty:run