Maven
基本概念
Maven是一个项目管理工具,涉及:
-
构建(build)
-
报告(reporting)
-
文档的生成(documentation)
-
依赖(Dependencies)
-
软件配置管理(SCMs)
-
Releases
-
Distribution
遵循约定大于配置。例如按Maven的约定编写好测试用例后,即可在构建过程中自动运行测试用例。
完全基于插件,通过调用不同的插件实现要实现的功能。
安装
-
下载解压
-
添加路径
vi ~/.bash_profile
export M2_HOME=/Users/xxx/DevInstall/apache-maven-3.6.2
export PATH=$PATH:$M2_HOME/bin
source ~/.bash_profile
-
查看版本
mvn -v
常用命令
mvn -h
# 打包指定模块
mvn pakcage -pl groupId:artifactId1,groupId:artifactId1
# 打包指定模块及其依赖模块
mvn pakcage -pl groupId:artifactId1,groupId:artifactId1 -am
# 打包指定模块、依赖模块、被依赖模块
mvn pakcage -pl groupId:artifactId1,groupId:artifactId1 -amd
生命周期
-
Maven将构建(build)和发布(distribute)的过程称为一个生命周期(build lifecycle),maven有三个内置的生命周期:clean、default、site。生命周期定义了各个构建环节的执行顺序。Maven有三套相互独立的生命周期,相互独立的,可以分别调用,也可以一起调用。
-
每套生命周期都由一组阶段(Phase)组成,如default包含validate、compile、test、package、install、deploy等。不同的phase是由不同的plugin实现功能,(phase由plugin goal组成),如compile阶段由Compiler插件实现,Compiler插件包含compile:compile、compile:help、compile:testCompile 3个goal
-
通过命令行调用不同的phase实现不同阶段的构建目标
# 获取jar包
maven package
# 执行单元测试
mvn test
-
当执行一个phase时,Maven会依顺序依次执行当前lifecycle中的所有phase,直至目标phase。
# 执行validate compile test package verify
mvn verify
# clean和package是phase,dependency:copy-dependencies是插件的goal
# 会依次执行 clean前的所有phase、clean phase、dependency的copy-dependencie goal、package前的所有phase、package phase
mvn clean dependency:copy-dependencies package
# 忽略失败的测试
mvn test -Dmaven.test.failure.ignore=true
# 不执行测试用例,也不编译测试用例类
mvn package -Dmaven.test.skip=true
# 不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下
mvn package -DskipTests
-
多项目情况下,Maven会在每个子项目中执行相同的命令。
-
部分phase无法直接通过命令行调用,例如带有pre-、post-、process-前缀的的phase,只会在顺序执行的过程中被调用并生成中间结果供使用。
-
一个phase负责lifecycle中的特定阶段,其具体功能通过绑定不同的plugin goal实现。plugin goal负责构建过程中更精细的任务,一个phase可以绑定0到多个goal。
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:
-
validate:确认项目信息正确且完整
-
compile: 编译源代码
-
test: 执行单元测试
-
pakcage: 将编译后的代码打包
-
verify: 根据Verifications文件中的表达式验证集成测试的结果
-
install: 将构建制品安装到本地仓库,用于本地其它项目的使用
-
deploy: 将构建制品发布到远程仓库
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. |
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. |
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 lifecycle
配置packaging
在不同的packaging下lifecycle有不同的goal绑定。
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 pom
Phase | plugin:goal |
---|---|
package |
|
install |
install:install |
deploy |
deploy:deploy |
配置plugin
通过配置好的插件提供goal给maven执行。
插件
-
Maven 的核心仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的。
-
每个插件都能实现多个功能,每个功能就是一个插件目标。
-
当需要自定义构建过程时需要配置插件
分为两种类型
-
Build plugins will be executed during the build and they should be configured in the
<build/>
element from the POM. -
Reporting plugins will be executed during the site generation and they should be configured in the
<reporting/>
element from the POM.
通用配置
mojo
显示指定版本号可以保证构建的稳定,推荐在pluginManagement中指定。
通过<configuration>
标签进行配置,<configuration>
标签的子标签与Mojo的字段一一对应(a Mojo maps to a goal)。
help goal
mvn javadoc:help -Ddetail -Dgoal=javadoc
参数类型
基础类型
对象类型
集合
map
build插件配置项
executions
使用executions标签,指定插件参与生命周期中的特定phases
未指定phase参与插件goal的默认phase
无默认phase则不参与build过程
绑定多个phase会执行多次
execution标签内的configurations只在当前phase生效
dependencies
指定build时的依赖
inherited
指定configurations是否继承,默认true
report插件配置项
reportSets
inherited
可用插件
(http://maven.apache.org/plugins/index.html):
Core plugins:
failsafe
:在独立的classloader中执行集成测试
surefire
:在独立的classloader中执行单元测试
verifier
:验证指定情况是否存在,集成测试下使用
Packaging types/tools:
Reporting plugins
changelog
:修改日志
checkstyle
:风格检查
surefire-report
:根据单元测试的结果生成报告
Tools
archetype
:创建一个项目骨架
surefire
单元测试插件
By default the tests included are:
-
**/*Test.java
-
**/Test*.java
-
**/*TestCase.java
And the default excludes are:
-
**/Abstract*Test.java
-
**/Abstract*TestCase.java
<!-- 多线程执行 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
failsafe
集成测试插件
如何使用
多级项目的使用
<plugins>
<!-- 独立的集成测试目录 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-it-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-it-resource</id>
<!--<phase>pre-integration-test</phase>-->
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>