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

3,118 阅读4分钟

命令执行区别

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生命周期的所有构建阶段:

PhaseDescription
validatevalidate the project is correct and all necessary information is available.
initializeinitialize build state, e.g. set properties or create directories.
generate-sourcesgenerate any source code for inclusion in compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compilecompile the source code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes.
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
verifyrun any checks to verify the package is valid and meets quality criteria.
installinstall the package into the local repository, for use as a dependency in other projects locally.
deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

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

PhaseDescription
pre-cleanexecute processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecute processes needed to finalize the project cleaning

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

PhaseDescription
pre-siteexecute processes needed prior to the actual project site generation
sitegenerate the project's site documentation
post-siteexecute processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploy the generated site documentation to the specified web server

学习资料

maven.apache.org/guides/intr…

stackoverflow.com/questions/1…

www.liaoxuefeng.com/wiki/125259…