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

相关推荐
哆啦 AI 梦16 分钟前
【Maven】Maven打包机制详解
maven·打包插件
哆啦 AI 梦26 分钟前
【Maven】Maven的快照库和发行库
maven·快照库·发行库
诸葛小猿17 小时前
maven的scope详解
maven·scope
李少兄20 小时前
如何从远程Maven仓库下载JAR包并手动放置到本地仓库
java·maven·jar
一休哥助手21 小时前
Spring Boot 项目中 Maven 剔除无用 Jar 引用的最佳实践
spring boot·maven·jar
seasugar1 天前
记一次Maven拉不了包的问题
java·maven
seasugar1 天前
Maven怎么会出现一个dependency-reduced-pom.xml的文件
xml·java·maven
music0ant1 天前
Idea 配置环境 更改Maven设置
java·maven·intellij-idea
昙鱼1 天前
springboot创建web项目
java·前端·spring boot·后端·spring·maven
栗豆包1 天前
w118共享汽车管理系统
java·spring boot·后端·spring·tomcat·maven