You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何清理Maven项目缓存?仅删除.m2中项目构建缓存的实现方法

清理Maven本地仓库(.m2)中项目专属构建缓存的方案

一、通过pom.xml配置实现的两种方式

默认的mvn clean只会清理项目根目录的target文件夹,但可以通过配置Maven插件,让它同时清理.m2/repository中当前项目的缓存,且不会影响其他项目的依赖。

1. 扩展maven-clean-plugin(推荐)

利用官方的maven-clean-plugin,通过动态变量定位当前项目在.m2中的缓存路径,配置后执行mvn clean即可同时清理target和项目缓存:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.3.2</version>
            <configuration>
                <filesets>
                    <!-- 保留默认清理target的逻辑 -->
                    <fileset>
                        <directory>${project.basedir}/target</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                    </fileset>
                    <!-- 添加清理当前项目在.m2中的缓存 -->
                    <fileset>
                        <directory>${settings.localRepository}/${project.groupId}/${project.artifactId}</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                        <!-- 可选:如果要保留正式发布版本,可排除非快照目录 -->
                        <!-- <excludes>
                            <exclude>*/RELEASE</exclude>
                        </excludes> -->
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
    </plugins>
</build>
  • ${settings.localRepository}:自动读取Maven配置的本地仓库路径,无需硬编码~/.m2/repository
  • ${project.groupId}/${project.artifactId}:精准定位当前项目的缓存目录,避免误删其他项目

2. 使用exec-maven-plugin执行系统命令

如果需要更灵活的清理规则(比如只删特定版本),可以用exec插件执行系统删除命令,绑定到clean阶段:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>clean-m2-project-cache</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <!-- Linux/Mac 环境配置 -->
                        <executable>rm</executable>
                        <arguments>
                            <argument>-rf</argument>
                            <argument>${settings.localRepository}/${project.groupId}/${project.artifactId}</argument>
                        </arguments>
                        <!-- Windows 环境替换为以下配置:
                        <executable>cmd</executable>
                        <arguments>
                            <argument>/c</argument>
                            <argument>rmdir /s /q "${settings.localRepository}\${project.groupId}\${project.artifactId}"</argument>
                        </arguments>
                        -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意:跨平台使用需要通过Maven Profile区分操作系统,避免命令不兼容。

二、触发清理的方式

  • 执行标准mvn clean命令:会同时触发项目target清理和.m2中项目缓存的清理
  • 单独执行缓存清理:
    # 仅执行clean-plugin的清理逻辑(含.m2缓存)
    mvn clean:clean
    # 仅执行exec-plugin的清理步骤
    mvn exec:exec@clean-m2-project-cache
    

三、重要提醒

  • 不要直接清理整个.m2仓库,必须通过${project.groupId}和${project.artifactId}精准定位,避免误删其他项目的依赖
  • 如果只想清理快照版本(SNAPSHOT),可以在fileset的includes中指定**/*SNAPSHOT*/**
  • 插件版本建议使用最新稳定版,可通过Maven Central确认版本号

内容的提问来源于stack exchange,提问作者user10053673

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 19:16:28