mvn clean package 与 mvn clean install 命令的区别

命令执行区别

mvn clean package命令:先清除工程(即清除工程target目录下的内容),然后编译工程,运行单测,将编译后的代码打包,如打成jar包。

mvn clean install命令:先清除工程(即清除工程target目录下的内容),然后编译工程,运行单测,将编译后的代码打包,将包安装到本地仓库。

通过对比可以看出:mvn clean packagemvn clean install命令都会先清除工程,然后编译,再打包,而mvn clean install命令多了一步将打好的包安装到本地仓库。

差异的本质

要了解mvn clean packagemvn clean install命令在执行上的差异,就要了解Maven的构建流程。

Maven三个内置构建生命周期

Maven有3个内置的构建生命周期,即defaultcleansite

  • default:处理项目工程(project)部署
  • clean:处理项目工程清理
  • site:处理项目工程网站的创建

Maven的生命周期由多个阶段(phase) 组成,以内置的default生命周期为例,由以下几个阶段组成:

  • validate:验证工程project是正确的,且所有必要信息都是可用的。
  • compile:编译工程源码。
  • test:使用合适的单元测试框架测试编译后的源代码,这些测试不应该要求打包或部署代码。
  • package:将编译后的代码打包成可发布的格式,如jar
  • verify:检查验证集成测试的结果。
  • install:将包安装到本地仓库。
  • deploy:将包推到远程仓库。

Maven命令的执行,对应的生命周期以及各阶段(phase)的执行是怎样的?

根据具体命令,可分为指定单个生命周期 的命令 和 指定多个生命周期的命令,如下:

1)指定单个生命周期的phase执行

当运行mvn compile命令时,Maven会执行default生命周期,从开始阶段一直运行到compile阶段为止,即依次执行的阶段为:validate --> compile

当运行mvn package命令时,Maven会执行default生命周期,从开始阶段一直运行到package阶段为止,即依次执行的阶段为:validate --> compile --> test --> package

2)指定多个生命周期的phase执行

当运行mvn clean package命令时,Maven会先执行clean生命周期,然后再执行default生命周期,从开始阶段一直运行到package阶段为止,即依次执行的阶段为:pre-clean --> clean --> validate --> compile --> test --> package

当运行mvn clean install命令时,Maven会先执行clean生命周期,然后再执行default生命周期,从开始阶段一直运行到install阶段为止,即依次执行的阶段为:pre-clean --> clean --> validate --> compile --> test --> package --> verify --> install

官网上Maven三个内置构建生命周期所有构建阶段

官网地址:maven.apache.org/guides/intr...

1)default生命周期的所有构建阶段:

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.

2)clean生命周期的所有构建阶段:

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

3)site生命周期的所有构建阶段:

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

学习资料

maven.apache.org/guides/intr...

stackoverflow.com/questions/1...

www.liaoxuefeng.com/wiki/125259...

相关推荐
duration~8 小时前
Maven随笔
java·maven
狂放不羁霸10 小时前
idea | 搭建 SpringBoot 项目之配置 Maven
spring boot·maven·intellij-idea
雷神乐乐10 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven
尘浮生18 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
aloha_7891 天前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
尢词1 天前
SpringMVC
java·spring·java-ee·tomcat·maven
wrx繁星点点1 天前
享元模式:高效管理共享对象的设计模式
java·开发语言·spring·设计模式·maven·intellij-idea·享元模式
前 方1 天前
若依入门案例
java·spring boot·maven
咕哧普拉啦1 天前
乐尚代驾十订单支付seata、rabbitmq异步消息、redisson延迟队列
java·spring boot·mysql·spring·maven·乐尚代驾·java最新项目
不像程序员的程序媛1 天前
mybatisgenerator生成mapper时报错
maven·mybatis