介绍
Maven是一个构建工具,类似ant/gradle
安装
详情可以参考官方安装说明
二进制发行包
到官方下载二进制发行包,解压缩,设定环境变量即可。
通过包管理器
*nix系统可以使用SDKMAN,Mac可以使用Homebrew,Windows可以使用Scoop
1 |
|
1 |
|
命令
1 |
|
MAVEN_OPTS
MAVEN_DEBUG_OPTS
Lifecycle
maven包含三套生命周期:default、clean和site。
Lifecycle Reference
Clean Lifecycle
Phase | Description |
---|---|
pre-clean |
execute processes needed prior to the actual project cleaning |
clean |
remove all files generated by the previous build |
post-clean |
execute processes needed to finalize the project cleaning |
Default Lifecycle
Phase | Description |
---|---|
validate |
validate the project is correct and all necessary information is available. |
initialize |
initialize build state, e.g. set properties or create directories. |
generate-sources |
generate any source code for inclusion in compilation. |
process-sources |
process the source code, for example to filter any values. |
generate-resources |
generate resources for inclusion in the package. |
process-resources |
copy and process the resources into the destination directory, ready for packaging. |
compile |
compile the source code of the project. |
process-classes |
post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources |
generate any test source code for inclusion in compilation. |
process-test-sources |
process the test source code, for example to filter any values. |
generate-test-resources |
create resources for testing. |
process-test-resources |
copy and process the resources into the test destination directory. |
test-compile |
compile the test source code into the test destination directory |
process-test-classes |
post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above. |
test |
run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
prepare-package |
perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above) |
package |
take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test |
perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test |
process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test |
perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify |
run any checks to verify the package is valid and meets quality criteria. |
install |
install the package into the local repository, for use as a dependency in other projects locally. |
deploy |
done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
Site Lifecycle
Phase | Description |
---|---|
pre-site |
execute processes needed prior to the actual project site generation |
site |
generate the project’s site documentation |
post-site |
execute processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy |
deploy the generated site documentation to the specified web server |
Build-in Lifecycle Bindings
Clean Lifecycle Bindings
Phase | plugin:goal |
---|---|
clean |
clean:clean |
Default Lifecycle Bindings - Packaging ejb
/ ejb3
/ jar
/ par
/ rar
/ war
Phase | plugin:goal |
---|---|
process-resources |
resources:resources |
compile |
compiler:compile |
process-test-resources |
resources:testResources |
test-compile |
compiler:testCompile |
test |
surefire:test |
package |
ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install |
install:install |
deploy |
deploy:deploy |
Default Lifecycle Bindings - Packaging ear
Phase | plugin:goal |
---|---|
generate-resources |
ear:generate-application-xml |
process-resources |
resources:resources |
package |
ear:ear |
install |
install:install |
deploy |
deploy:deploy |
Default Lifecycle Bindings - Packaging maven-plugin
Phase | plugin:goal |
---|---|
generate-resources |
plugin:descriptor |
process-resources |
resources:resources |
compile |
compiler:compile |
process-test-resources |
resources:testResources |
test-compile |
compiler:testCompile |
test |
surefire:test |
package |
jar:jar and plugin:addPluginArtifactMetadata |
install |
install:install |
deploy |
deploy:deploy |
Default Lifecycle Bindings - Packaging pom
Phase | plugin:goal |
---|---|
package |
|
install |
install:install |
deploy |
deploy:deploy |
Site Lifecycle Bindings
Phase | plugin:goal |
---|---|
site |
site:site |
site-deploy |
site:deploy |
pom.xml
坐标
groupId:
artifactId:
version:
packaging:
classifier:
1 |
|
插件(Plugins)
插件是实际执行任务或目标(goals)的集合,可以直接使用,也可以绑定到生命周期中(添加到pom.xml的build-》plugins下),由生命周期流程驱动执行,注意:添加插件后需要配置要执行的goal,也可以定制绑定phase(有些插件的goal默认绑定了phase)。比如:
<plugins>
1 |
|
<pluginManagement/>
1 |
|
Maven生命周期中某些phrase已经预先绑定了一些插件。
插件一般都有一个help的目标,用来显示插件的基本信息和包含的goals等。
1 |
|
可以查看更详细的信息,比如参数等,以及指定goal
1 |
|
更详细信息可以使用下面常用插件中介绍的maven-help-plugin
插件前缀(plugin-prefix):简化插件的标示,如果插件的artifactId命名符合规范:{plugin-prefix}-maven-plugin或maven-{plugin-prefix}-plugin可会自动识别插件前缀,也可以通过goalPrefix自定义插件前缀。
插件解析时,默认groupId取值为org.apache.maven.plugins和org.codehaus.mojo,可以通过settings.xml中新增其他groupId,否则会报找不到指定插件前缀的插件。
1 |
|
1 |
|
依赖
<dependencies/>
1 |
|
<dependencyManagement/>
1 |
|
依赖坐标
groupId:
artifactId:
version:
type:默认值是jar,如果是pom,则表示依赖其<dependencies/>
或<dependencyManagement/>
例如:
1 |
|
classifier:
scope:
import:用于<dependencyManagement/>
例如:
1 |
|
依赖传递
optional为false,则不会传递依赖
依赖调解
- dependencyManagement优先,且第一声明者优先
- 路径最近者优先
- 第一声明者优先
Dependency Mediation and Conflict Resolution
仓库
The Central Repository Search Engine
maven本地仓库默认路径:${home}/.m2/repository
安装构建到本地仓库,例如:
1 |
|
默认远程仓库为中央仓库(central),地址:https://repo1.maven.org/maven2/
模块
父模块
1 |
|
备注:
子模块
module1
1 |
|
继承
<properties/>
<dependencies/>
<dependencyManagement/>
<plugins/>
<pluginManagement/>
超级POM
settings.xml
用户层面的settings.xml存放在${home}/.m2/
1 |
|
pluginGroups
server
maven仓库服务器相关配置,主要用于配置认证信息。
server
中的id与repository或mirror中的id一致。
比如:
1 |
|
比如项目的pom.xml里面定义的repository
1 |
|
mirror
镜像,其实也是一个仓库,根据匹配规则作为远程仓库的镜像。
1 |
|
Maven Wrapper
Maven Wrapper的作用是将maven的版本纳入项目管理中,实现maven的版本由项目控制,与运行环境无关。首次运行时自动下载相应版本的maven到当前环境,升级也会非常容易。不会使用当前环境已安装的maven。
安装
有如下两种方法,区别主要再生成的目录,一个是.mvn,另一个是maven。
配置环境变量MAVEN_USER_HOME
,maven下载存放到其下的wrapper/dist目录下。
1 |
|
方法一
参考:maven-wrapper,takari-maven-plugin
1 |
|
默认使用最新稳定版本作为maven wrapper的maven版本,也可以通过如下配置定制,比如指定maven版本
1 |
|
1 |
|
maven-wrapper.properties中可以指定maven下载路径(可以使用相对目录),比如:
1 |
|
方法二
参考:maven-mvnd
方法三
添加plugin
1 |
|
1 |
|
默认使用当前环境的maven版本,也可以通过如下配置定制,比如指定maven版本
1 |
|
1 |
|
maven-wrapper.properties中可以指定maven下载路径(可以使用相对目录),比如:
1 |
|
使用
1 |
|
首次会自动下载相应版本的maven
升级
指定新版本的maven,重新安装即可【生成的相关文件可能会变化】
properties
1 |
|
可以通过java系统属性提供更高优先级的设定,比如:-Dit.skip=true
plugin属性
plugin一般都定义有属性,可以通过describe插件查看,例如:
1 |
|
- 可以在使用插件的地方设置属性(
<configuration>
),例如:
1 |
|
- 可以在pom的
<properties>
中定义属性(只有User property属性支持),例如:
1 |
|
- 可以通过java系统属性来设置(只有User property属性支持),例如:
1 |
|
常用插件
maven-help-plugin
比插件自带的help显示更多信息,包括插件前缀,goal绑定生命周期,参数的User property等。
1 |
|
maven-surefire-plugin
maven-failsafe-plugin
maven-dependency-plugin
copy-dependencies
导出项目的依赖包
1 |
|
参数 | 描述 |
---|---|
-Dmdep.useRepositoryLayout | 按坐标目录结构,默认值:false |
-DoutputDirectory | 导出目录,默认值:target/dependency |
-DincludeScope | 导出指定级别的依赖 |
includeScope (Default: )
1 |
|
1 |
|
tree
1 |
|
javadoc
1 |
|
exec
1 |
|
QA
使用sun私有代码
使用sun私有代码,编译时可能会类似错误package com.sun.image.codec.jpeg does not exist
,解决方法如下:
1 |
|
或者
1 |
|