What are the phases of the maven clean lifecycle?

本文详细介绍了Maven Clean生命周期的各个阶段,包括pre-clean、clean和post-clean,并通过实例展示了如何使用maven-antrun-plugin在这些阶段执行特定任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven Clean Lifecycle Phases

pre-clean
clean
post-clean

Calling one phase of the clean lifecycle results in the execution of all phases up to an including that phase. So, if we perform a "mvn clean", we will execute the pre-clean and the clean phases. If we perform a "mvn post-clean", we will execute the pre-clean, clean, and post-clean phases.

The maven "clean:clean" goal is typically bound to the clean phase. This goal 'cleans' the project's build (usually 'target') directory, which typically involves deleting old files. The pre-clean phase can be used for any tasks required prior to cleanup, and the post-clean phase can be used for tasks following the cleanup.

We can bind other goals to the phases of the clean lifecycle. In the following pom.xml file, I bind the maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This will allow me to echo text messages displaying the phases of the clean lifecycle.
pom.xml file that binds antrun:run to pre-clean, clean, and post-clean phases

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maventest</groupId>
<artifactId>aproject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>aproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<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>in pre-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>in clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-clean</id>
<phase>post-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>in post-clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>


If we perform a "mvn clean" on the project with the above pom.xml, we can see that the pre-clean and clean phases are executed based on our text messages.
Console output from 'mvn clean'

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building aproject
[INFO] task-segment: [clean]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-clean}]
[INFO] Executing tasks
[echo] in pre-clean phase
[INFO] Executed tasks
[INFO] [clean:clean]
[INFO] Deleting directory C:\dev\workspace\aproject\target
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
[echo] in clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Feb 13 14:44:26 PST 2008
[INFO] Final Memory: 3M/7M
[INFO] ------------------------------------------------------------------------


If we perform a "mvn post-clean" on the project, we can see that the pre-clean, clean, and post-clean phases are all executed.
Console output from 'mvn post-clean'

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building aproject
[INFO] task-segment: [post-clean]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: id.pre-clean}]
[INFO] Executing tasks
[echo] in pre-clean phase
[INFO] Executed tasks
[INFO] [clean:clean]
[INFO] Deleting directory C:\dev\workspace\aproject\target
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
[echo] in clean phase
[INFO] Executed tasks
[INFO] [antrun:run {execution: id.post-clean}]
[INFO] Executing tasks
[echo] in post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Feb 13 14:44:55 PST 2008
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值