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...

相关推荐
shane-u15 小时前
Maven私服搭建与登录全攻略
java·maven
半部论语15 小时前
jdk多版本切换,通过 maven 指定编译jdk版本不生效,解决思路
java·开发语言·maven·intellij-idea
我喜欢山,也喜欢海17 小时前
Jenkins Maven 带权限 搭建方案2025
java·jenkins·maven
kaikaile199518 小时前
Jenkins集成Maven
servlet·jenkins·maven
.生产的驴18 小时前
Docker 部署Nexus仓库 搭建Maven私服仓库 公司内部仓库
java·运维·数据库·spring·docker·容器·maven
.生产的驴1 天前
Maven 公司内部私服中央仓库搭建 局域网仓库 资源共享 依赖包构建共享
java·maven
Brilliant Nemo1 天前
五、框架实战:SSM整合原理和实战
maven·mybatis
亮1111 天前
GITLAB跑gradle项目 不借助maven-publish直接上传到nexus私人仓库
java·gitlab·gradle·maven
极小狐1 天前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
Meta392 天前
解决IDEA Maven编译时@spring.profiles.active@没有替换成具体环境变量的问题
spring·maven·intellij-idea