生命周期是指项目的构建过程,它包含了一系列的有序的阶段(phase),而一个阶段就是构建过程中的一个步骤。
Maven有一下三种标准的生命周期,最常用的是默认的Maven生命周期(default Maven lifecycly):
- clean
- default(or build)
- site
目标代表一个特定的任务,这有助于项目的建设和管理。目标可以被绑定到零个或多个生成阶段。一个没有绑定到任何构建阶段的目标的构建生命周期可以执行直接调用。执行的顺序取决于目标和构建阶段被调用的顺序。
一 clean 生命周期
执行mvn clean之后,maven的清洁生命周期由一下三个阶段组成:
- pre-clean
- clean
- post-clean
下面我们将使用 maven-antrun-plugin:run进行调用clean生命周期。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maven.demo</groupId> <artifactId>MavenDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MavenDemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
执行 "mvn post-clean“之后,显示如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ MavenDemo ---
[INFO] Executing tasks
[echo] pre-clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting D:workspace_lunaMavenDemo arget
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ MavenDemo ---
[INFO] Executing tasks
[echo] clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ MavenDemo ---
[INFO] Executing tasks
[echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.888 s
[INFO] Finished at: 2015-02-11T15:57:21+08:00
[INFO] Final Memory: 5M/16M
[INFO] ------------------------------------------------------------------------
可以看到依次执行了 pre-clean、clean以及 post-clean。并且执行结束之后项目中target路径中的编译好的文件被删除。说明清洁成功。
二 default 生命周期
默认生命周期是maven使用最多的生命周期,用于构建程序。它由一下23个阶段组成:
描述 | |
---|---|
validate | Validates whether project is correct and all necessary information is available to complete the build process. |
initialize | Initializes build state, for example set properties |
generate-sources | Generate any source code to be included in compilation phase. |
process-sources | Process the source code, for example, filter any value. |
generate-resources | Generate resources to be included in the package. |
process-resources | Copy and process the resources into the destination directory, ready for packaging phase. |
compile | Compile the source code of the project. |
process-classes | Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. |
generate-test-sources | Generate any test source code to be included in compilation phase. |
process-test-sources | Process the test source code, for example, filter any values. |
test-compile | Compile the test source code into the test destination directory. |
process-test-classes | Process the generated files from test code file compilation. |
test | Run tests using a suitable unit testing framework(Junit is one). |
prepare-package | Perform any operations necessary to prepare a package before the actual packaging. |
package | Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file. |
pre-integration-test | Perform actions required before integration tests are executed. For example, setting up the required environment. |
integration-test | Process and deploy the package if necessary into an environment where integration tests can be run. |
pre-integration-test | Perform actions required after integration tests have been executed. For example, cleaning up the environment. |
verify | Run any check-ups to verify the package is valid and meets quality criterias. |
install | Install the package into the local repository, which can be used as a dependency in other projects locally. |
deploy | Copies the final package to the remote repository for sharing with other developers and projects. |
当我们通过命令调用一个阶段的时候,比如说 mvn compile,这个阶段以及这个阶段之前的所有阶段都将被执行。根据打包的种类(JAR/WAR/EAR),每个阶段会绑定不同的goal。
执行 mvn compile,target路径下会多一个maven-status的文件夹,里面存放了编译的结果。控制台输出结果如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:workspace_lunaMavenDemosrcmain
esources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.999 s
[INFO] Finished at: 2015-02-11T16:19:31+08:00
[INFO] Final Memory: 7M/16M
[INFO] ------------------------------------------------------------------------
三 网站的生命周期
该阶段分为四个阶段:
- pre-site
- site
- post-site
- site-deploy
运行 mvn site之后,log如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-site-plugin:3.3:site (default-site) @ MavenDemo ---
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin.
[INFO] Generating "Dependencies" report --- maven-project-info-reports-plugin:2.8
[WARNING] The repository url 'https://oss.sonatype.org/content/repositories/snapshots' is invalid - Repository 'sonatype-nexus-snapshots' will be blacklisted.
[INFO] Generating "Dependency Convergence" report --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Dependency Information" report --- maven-project-info-reports-plugin:2.8
[INFO] Generating "About" report --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Plugin Management" report --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Project Plugins" report --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Project Summary" report --- maven-project-info-reports-plugin:2.8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:34 min
[INFO] Finished at: 2015-02-11T16:27:07+08:00
[INFO] Final Memory: 14M/42M
[INFO] ------------------------------------------------------------------------
运行之后,会在target路径下生成一个site文件夹,里面包括很多文件,包括css,images以及一些项目描述文件。