构建生命周期是什么?
构建生命周期阶段的目标是执行顺序是一个良好定义的序列。
这里使用一个例子,一个典型的 Maven 构建生命周期是由下列顺序的阶段:
![](images/2018030921.png)
可用于注册必须执行一个特定的阶段之前或之后的目标,有之前处理和之后阶段。
当 Maven 开始建立一个项目,它通过定义序列阶段步骤和执行注册的每个阶段的目标。 Maven有以下三种标准的生命周期:
clean
default(或 build)
site
目标代表一个特定的任务,它有助于项目的建设和管理。可以被绑定到零个或多个生成阶段。一个没有绑定到任何构建阶段的目标,它的构建生命周期可以直接调用执行。
执行的顺序取决于目标和构建阶段折调用顺序。例如,考虑下面的命令。清理和打包(mvn clean)参数的构建阶段,而
dependency:copy-dependencies package 是一个目标。
mvn clean dependency:copy-dependencies package
在这里,清洁的阶段,将首先执行,然后是依赖关系:复制依赖性的目标将被执行,并终于将执行包阶段。
清洁生命周期
当我们执行命令 mvn clean 命令后,Maven 调用清洁的生命周期由以下几个阶段组成:
pre-clean
clean
post-clean
Maven 清洁目标(clean:clean)被绑定清洁干净的生命周期阶段。clean:clean
目标删除 build 目录下的构建输出。因此,当 mvn clean 命令执行时,Maven会删除编译目录。
目标清洁生命周期在上述阶段,我们可以自定义此行为。
在下面的示例中,我们将附加 maven-antrun-plugin:run 对目标进行预清洁,清洁和清洁后这三个阶段。这将使我们能够调用的信息显示清理生命周期的各个阶段。
现在来创建了一个 pom.xml 文件在 C:\MVN\ 项目文件夹中,具体内容如下:
<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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<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>
|
现在,打开命令控制台,到该文件夹包含 pom.xml 并执行以下 mvn 命令。
C:\MVN\project>mvn post-clean
Maven将开始处理并显示清理生命周期的所有阶段。
[INFO] Scanning
for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [post-clean]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-clean}]
[INFO] Executing tasks
[echo] pre-clean phase
[INFO] Executed tasks
[INFO] [clean:clean {execution: default-clean}]
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
[echo] clean phase
[INFO] Executed tasks
[INFO] [antrun:run {execution: id.post-clean}]
[INFO] Executing tasks
[echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sat Jul 07 13:38:59
IST 2012
[INFO] Final Memory: 4M/44M
[INFO] ------------------------------------------------------------------
|
你可以尝试调整 mvn 清洁命令,该命令将显示清洁前什么都不会被执行。
默认(或生成)生命周期
![](images/2018030922.png)
有涉及到Maven 生命周期值得一提几个重要概念:
当一个阶段是通过 Maven命令调用,例如:mvn compile,只有阶段到达并包括这个阶段才会被执行。
不同的 Maven 目标绑定到 Maven生命周期的不同阶段这是这取决于包类型(JAR/WAR/EAR)。
在下面的示例中,将附加 Maven 的 antrun 插件:运行目标构建生命周期的几个阶段。这将使我们能够回显的信息显示生命周期的各个阶段。
我们已经更新了在 C:\MVN\ 项目文件夹中的 pom.xml 文件。
<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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.validate</id>
<phase>validate</phase>
<goals>
<goal>run</goal> </goals>
<configuration>
<tasks>
<echo>validate phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>compile phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.test</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>test phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>package phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.deploy</id>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> |
现在,打开命令控制台,进入包含 pom.xml 并执行以下 mvn 命令。
C:\MVN\project>mvn
compile |
编译阶段,Maven 将开始构建生命周期的阶段处理并显示。
[INFO] Scanning
for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.validate}]
[INFO] Executing tasks
[echo] validate phase
[INFO] Executed tasks
[INFO] [resources:resources {execution:
default-resources}]
[WARNING] Using platform encoding (Cp1252
actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory
C:\MVN\project\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes
are up to date
[INFO] [antrun:run {execution: id.compile}]
[INFO] Executing tasks
[echo] compile phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Jul 07 20:18:25
IST 2012
[INFO] Final Memory: 7M/64M
[INFO] ------------------------------------------------------------------ |
网站的生命周期
Maven的网站插件通常用于创建新的文档,创建报告,部署网站等。
阶段
pre-site
site
post-site
site-deploy
在下面的示例中,我们将附加 maven-antrun-plugin:run 目标网站的生命周期的所有阶段。这将使我们能够调用短信显示的生命周期的各个阶段。
现在更新 pom.xml 文件在 C:\MVN\ 项目文件夹中。
<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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.pre-site</id>
<phase>pre-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site</id>
<phase>site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration><tasks>
<echo>site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-site</id>
<phase>post-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>site-deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> |
打开命令控制台,进入该文件夹包含 pom.xml 并执行以下 mvn 命令。
Maven将开始处理并显示网站的生命周期阶段的各个阶段。
[INFO] Scanning
for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [site]
[INFO] ------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-site}]
[INFO] Executing tasks
[echo] pre-site phase
[INFO] Executed tasks
[INFO] [site:site {execution: default-site}]
[INFO] Generating "About" report.
[INFO] Generating "Issue Tracking"
report.
[INFO] Generating "Project Team"
report.
[INFO] Generating "Dependencies"
report.
[INFO] Generating "Project Plugins"
report.
[INFO] Generating "Continuous Integration"
report.
[INFO] Generating "Source Repository"
report.
[INFO] Generating "Project License"
report.
[INFO] Generating "Mailing Lists"
report.
[INFO] Generating "Plugin Management"
report.
[INFO] Generating "Project Summary"
report.
[INFO] [antrun:run {execution: id.site}]
[INFO] Executing tasks
[echo] site phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Sat Jul 07 15:25:10
IST 2012
[INFO] Final Memory: 24M/149M
[INFO] -
|
|