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 :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenlifecycle ---
[INFO] Building jar: E:\javaworkspace\mavenlifecycle\target\mavenlifecycle-1.0.jar
[INFO]
[INFO] --- maven-shade-plugin:2.4.3:shade (default) @ mavenlifecycle ---
[INFO] Including org.slf4j:slf4j-log4j12:jar:1.7.25 in the shaded jar.
[INFO] Including org.slf4j:slf4j-api:jar:1.7.25 in the shaded jar.
[INFO] Including log4j:log4j:jar:1.2.17 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing E:\javaworkspace\mavenlifecycle\target\mavenlifecycle-1.0.jar with E:\javaworkspace\mavenlifecycle
\target\mavenlifecycle-1.0-shaded.jar
[INFO] Dependency-reduced POM written at: E:\javaworkspace\mavenlifecycle\dependency-reduced-pom.xml
[INFO] Dependency-reduced POM written at: E:\javaworkspace\mavenlifecycle\dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.664 s
[INFO] Finished at: 2019-07-23T13:10:36+08:00
[INFO] Final Memory: 18M/157M
[INFO] ------------------------------------------------------------------------
E:\javaworkspace\mavenlifecycle>java -jar target\mavenlifecycle-1.0.jar
[INFO ] 2019-07-23 13:41:58 com.xxx.mavenlifecycle.App this is a maven lifecycle test app.
打包过程中,maven进行的操作是:

其中很多默认的阶段并没有在这里体现出来。 这里面需要说明的是clean这个阶段,一般项目构建首先是需要清理之前的项目构建,避免漏掉新的内容。其中,每一步操作都有各自对应的插件去完成,这里给出每个阶段与各阶段对应的插件:

我们说默认的package打包任务,会显示的包含process-resources,compile,process-test-resources,test-compile,test,package这几个步骤,我们可以通过maven-antrun-plugin这个插件来定义一些节点,看看package这个任务会包含具体的多少个阶段phase,具体插件代码配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>0</id>
<phase>clean</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>clean</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>1</id>
<phase>validate</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>validate</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>generate-sources</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>generate-sources</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>3</id>
<phase>generate-resources</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>generate-resources</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>4</id>
<phase>compile</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>compile</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>5</id>
<phase>generate-test-sources</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>generate-test-sources</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>6</id>
<phase>generate-test-resources</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>generate-test-resources</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>7</id>
<phase>test-compile</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>test-compile</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>8</id>
<phase>test</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>test</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>9</id>
<phase>package</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<echo>package</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
然后我们运行mvn clean package,会打印这里面所包含的所有阶段。
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-antrun-plugin:1.8:run (0) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] clean
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (1) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] validate
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (2) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] generate-sources
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (3) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] generate-resources
[INFO] Executed tasks
[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-antrun-plugin:1.8:run (4) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] compile
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (5) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] generate-test-sources
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (6) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] generate-test-resources
[INFO] Executed tasks
[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-antrun-plugin:1.8:run (7) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] test-compile
[INFO] Executed tasks
[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 22:06:04 com.xxx.mavenlifecycle.Log4jJunitTest test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.173 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (8) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] test
[INFO] Executed tasks
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenlifecycle ---
[INFO] Building jar: E:\javaworkspace\mavenlifecycle\target\mavenlifecycle-1.0.jar
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (9) @ mavenlifecycle ---
[WARNING] Parameter tasks is deprecated, use target instead
[INFO] Executing tasks
main:
[echo] package
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.717 s
[INFO] Finished at: 2019-07-23T22:06:05+08:00
[INFO] Final Memory: 18M/167M
[INFO] ------------------------------------------------------------------------
这里面package默认不包含clean即清理这个阶段,所以如果要清理的话,需要这么来执行命令:mvn clean package。
Maven生命周期详解
52

被折叠的 条评论
为什么被折叠?



