What are the phases of the maven clean lifecycle?

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] ------------------------------------------------------------------------
如果你遇到 Maven 插件无法正常工作的状况,手动安装和配置 Maven 可能会有所帮助。以下是手动安装和配置的步骤: 1. **下载并安装 Maven**: - 访问 Maven 官网(https://maven.apache.org/download.cgi)下载最新版本的 Maven。 - 下载适合你操作系统的安装包(比如 `apache-maven-x.x.x-bin.zip`),然后解压。 - 将 Maven 的 bin 目录添加到你的系统路径中,以便在命令行直接访问 Maven 命令。 2. **配置环境变量**: - 在 Windows 上,打开“系统属性”,然后选择“高级”标签,点击“环境变量”按钮,找到“Path”变量,在其值末尾添加 Maven 的 bin 目录。 - 在 macOS 或 Linux 上,编辑 `~/.bash_profile` 或 `~/.zshrc` 文件,添加 `export PATH=$PATH:/path/to/maven/bin`(将 `/path/to/maven/bin` 替换为实际的 Maven 安装路径)。 3. **验证安装**: 打开命令行,输入 `mvn -v` 或 `mvn --version`,如果能显示出 Maven 的版本信息,说明安装成功。 4. **创建 Maven 工程**: 在命令行中,进入你想要创建 Maven 工程的目录,运行 `mvn archetype:generate` 来创建一个新的 Maven 项目。 5. **配置 Maven 仓库**: - 你可以全局配置 Maven 仓库,通常在 `~/.m2/settings.xml` 文件中。确保 `<localRepository>` 标签下的路径指向一个有效的本地仓库。 - 如果需要使用外部仓库,需要在 `<repositories>` 和 `<pluginRepositories>` 节点中添加相应的远程仓库地址。 6. **问题排查**: - 如果插件还是无法工作,检查你的 `pom.xml` 文件中是否有正确的插件定义,包括groupId、artifactId、version等信息。 - 检查插件是否已正确地配置在生命周期阶段(lifecycle phases)中。 7. **错误日志**: 查看 Maven 的日志文件(通常是 `.mvn/maven-logs/maven.log`)来寻找关于插件安装或执行失败的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值