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

相关推荐
爬山算法3 小时前
Maven(6)如何使用Maven进行项目构建?
java·maven
CS_GaoMing1 天前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
Java探秘者2 天前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑2 天前
JavaWeb(二)
java·数据仓库·hive·hadoop·maven
忙里偷闲的sin2 天前
整理Maven坐标,Spring Boot集成工具依赖版本差异问题
java·spring boot·maven
芝法酱2 天前
芝法酱学习笔记(0.6)——nexus与maven私库
java·maven·nexus
-$_$-3 天前
【黑马点评】2 商户查询缓存
java·jmeter·缓存·maven
Pluto_CSND3 天前
maven安装本地jar包到本地仓库
maven·jar
丶21363 天前
【IDE】解决 IDEA-Maven Dependencies 中出现红色波浪线的问题
java·maven·intellij-idea
尘浮生3 天前
Java项目实战II基于Java+Spring Boot+MySQL的免税商品优选购物商城(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·maven·intellij-idea