Maven命令的格式如下:
# options用来指定运行时参数 # goals用来表示plugin的特定的任务 # phases用来表示Maven Life Cycle的某个阶段 mvn [options] [<goal(s)>] [<phase(s)>]运行mvn --help可以查看options参数说明。
在IntellijIDEA的右边栏点击Maven Projects,展开Lifecycle可以查看Maven Life Cycle有哪些phase,展开Plugins可以查看指定Maven插件有哪些goal。
Maven默认有3个Life Cycle:
clean 清理项目default构建测试和部署项目site项目site文档实践上,都是clean和defaultLife Cycle一起使用,很少使用site。
常见的Life Cycle按照顺序包括:
clean - 清理项目和删除target目录 validate - 验证项目是否可以构建compile - 编译源代码test-compile - 编译测试代码test - 运行单元测试package - 打包integration-test - 运行集成测试verify - 检查构建结果和集成测试结果install - 在本地Maven缓存中安装构建好的jar包deploy - 部署jar包到Nexus当运行Maven Life Cycle的后面的phase时,默认会包含前面的phase。比如运行mvn clean package时,会自动运行前面的validate, compile, test-compile和test phase。
完整的Life Cycle参见:
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_ReferenceMaven Plugin的各个execution(包含一个或多个goal)一般有一个默认的phase ,但是也可以为execution指定另外个phase。
示例:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.2</version> <executions> <!-- default phase is initialize --> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <!-- change phase from verify to test --> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>当运行mvn clean test时,上面示例的JaCoCo插件会在initializephase准备和运行JaCoCo Agent,在运行单元测试时收集测试覆盖率信息,并在test phase时根据收集到的测试覆盖率信息生成测试覆盖率报告。
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
https://www.baeldung.com/maven-goals-phases
nklinsirui 认证博客专家 DevOps DevOps的实践者和布道者。现在某国际开源软件公司担任高级专家顾问。曾在某电子政务公司担任高级系统架构师。曾在某国际咨询公司担任技术架构交付经理。曾在某跨国银行中国软件开发中心担任高级专家顾问。DevOps Master, CSM, PMP。公众号:乒乓咨询