maven是一个使用广泛的项目构建工具,他能够很方便的和eclipse和idea集成,最新的IDE都内置了maven插件,我们在使用maven构建项目的时候,都是只关注依赖,以及构建插件。当我们编码完成,需要发布的时候,直接通过插件运行install,会生成jar包,同时也会生成在本地仓库中。
maven的生命周期这个问题,是一个很容易被忽视的问题,因为我在面试中就被问倒了,一个简单的不能再简单的问题,居然把我问住了,因为我平时就是使用package,install,run这些命令,殊不知,这些命令之中还包含了很多maven的生命周期的其他命令,为了这个问题,今天来彻底搞懂maven生命周期。
maven内置了三个生命周期:默认构建(default),项目清理(clean),建站构建(site),一般用的最多的是默认构建和项目清理。
maven默认生命周期如下:
默认生命周期中很多阶段在正常的操作中不会体现出来,以打包package为例,我们进行打包,这里为了打一个可执行程序的包,我们加入了maven-shade-plugin,打包流程如下:
E:\javaworkspace\mavenlifecycle>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenlifecycle 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavenlifecycle ---
[INFO] Deleting E:\javaworkspace\mavenlifecycle\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenlifecycle ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenlifecycle ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\javaworkspace\mavenlifecycle\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenlifecycle ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\javaworkspace\mavenlifecycle\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenlifecycle ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\javaworkspace\mavenlifecycle\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenlifecycle ---
[INFO] Surefire report directory: E:\javaworkspace\mavenlifecycle\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xxx.mavenlifecycle.Log4jJunitTest
[INFO ] 2019-07-23 13:10:35 com.xxx.mavenlifecycle.Log4jJunitTest test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 sec
Results :