[INFO] Finished at: 2025-06-10T18:18:05+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:3.4.1:clean (default-clean) on project cmcc: Failed to clean project: Failed to delete C:\Users\ASUS\IdeaProjects\cmcc\target: C:\Users\ASUS\IdeaProjects\cmcc\target: 另一个程序正在使用此文件,进程无法访问。 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException 进程已结束,退出代码为 1
时间: 2025-06-10 20:43:48 浏览: 31
### 问题分析
当执行 `maven clean` 命令时,如果出现“Failed to delete target folder”错误,通常是因为目标文件夹中的某些文件被其他程序占用或锁定。这种情况在 Windows 环境中尤为常见[^2]。
具体来说,以下几种情况可能导致此问题:
1. Web 应用正在运行,导致应用服务器(如 Tomcat)锁定了目标文件夹中的文件。
2. 文件资源被其他工具(如 IDE、文件管理器或文本编辑器)打开。
3. 某些系统进程可能正在访问这些文件。
---
### 解决方案
#### 方法一:停止应用服务器
如果项目正在运行,例如通过嵌入式 Tomcat 或其他应用服务器启动了 Web 应用,则需要先停止该服务器。可以通过以下方式解决:
- 在 IDE 中停止项目的运行实例。
- 如果使用命令行启动了应用服务器,确保其已完全退出。
- 再次运行 `mvn clean` 命令以清理目标文件夹。
#### 方法二:关闭相关文件
检查是否有任何文件资源被占用,例如:
- 在文件管理器中打开了 `target` 文件夹。
- 正在编辑 `target` 文件夹中的某些文件。
- 日志文件或其他临时文件被其他工具占用。
关闭所有相关的文件和工具后,重新尝试执行 `mvn clean`。
#### 方法三:手动删除目标文件夹
如果上述方法无效,可以尝试手动删除 `target` 文件夹:
```bash
rm -rf <project-path>/target
```
或者在 Windows 系统中使用命令行:
```cmd
rmdir /s /q <project-path>\target
```
随后再次运行 `mvn clean`,确保目标文件夹能够正常生成。
#### 方法四:调整 Maven 清理插件配置
有时可以通过调整 `maven-clean-plugin` 的配置来避免文件锁定问题。例如,在 `pom.xml` 中添加以下配置:
```xml
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
```
此配置确保插件在清理时更灵活地处理文件锁定问题[^1]。
#### 方法五:检查第三方依赖
如果问题由第三方依赖引起,可以尝试将依赖的 JAR 包手动安装到本地 Maven 仓库。参考以下命令:
```bash
mvn install:install-file -Dfile=<jar包路径> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar -DgeneratePom=true
```
例如:
```bash
mvn install:install-file -Dfile=G:\ES\yuzhouwan_netflow-rest\lib\ite-esmanage-0.7.2-SNAPSHOT.jar -DgroupId=ite -DartifactId=esmanage -Dversion=0.7.2-SNAPSHOT -Dpackaging=jar -DgeneratePom=true
```
这有助于解决因依赖冲突或版本不匹配导致的文件锁定问题[^4]。
---
### 注意事项
- 如果问题仍然存在,可以使用系统工具(如 Process Explorer)检查哪些进程占用了 `target` 文件夹中的文件,并终止这些进程。
- 确保 Maven 版本与插件版本兼容。例如,`maven-clean-plugin` 的版本应与当前使用的 Maven 版本匹配。
---
### 示例代码
以下是一个完整的 `pom.xml` 配置示例,包含优化后的 `maven-clean-plugin` 设置:
```xml
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
```
---
阅读全文
相关推荐

















