最近,Apache Maven似乎遇到了缓存问题。使用Windows Vista或Windows 7在我们的项目上执行干净安装,有时会产生具有与以前构建相同数据的工件,即使较新的工件的文件应该已经更新。

有没有办法清除这个缓存,迫使maven总是触发应该构建的本地工件的干净构建?

特别是,我们在使用war插件构建web应用程序时遇到了问题。Maven版本是3.0.3。War插件版本为2.1.1。


删除c:\Users\<username>\。M2 \资源库的手。


您是否检查/更改了settings.xml中存储库的updatePolicy设置。

此元素指定更新应该尝试发生的频率。 Maven将比较本地POM的时间戳(存储在存储库中) maven-元数据文件)到远程。选择是:总是,每天 (默认值),间隔:X(其中X为整数,单位为分钟)或从不。

试着把它设置为always。


这适用于Spring Tool Suite v 3.1.0。RELEASE,但是我猜它也可以在Eclipse上使用。

在/用户名/。M2目录,重新索引文件,执行以下操作:

至:

Windows->首选项->Maven->用户设置菜单。

单击“本地存储库”文本框旁边的“重新索引”按钮。点击“应用”,然后点击“确定”,你就完成了。


若要清除本地缓存,请尝试使用依赖项插件。

mvn dependency:purge-local-repository: This is an attempt to delete the local repository files but it always goes and fills up the local repository after things have been removed. mvn dependency:purge-local-repository -DreResolve=false: This avoids the re-resolving of the dependencies but seems to still go to the network at times. mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false: This was added by Paweł Prażak and seems to work well. I'd use the third if you want the local repo emptied, and the first if you just want to throw out the local repo and get the dependencies again.


我会做以下几点:

mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false --fail-at-end

标志告诉maven不要尝试解析依赖项或攻击网络。删除您在本地看到的内容。

为了更好地衡量,忽略错误(——fail-at-end)直到最后。这有时对于依赖项集有点混乱或依赖于有点混乱的内部存储库(这种情况时有发生)的项目很有用。


正如一些回答所指出的,有时您确实想要完全删除本地存储库,例如,可能有一些工件不能被清除,因为它们不再被pom引用。

如果你想把这个删除嵌入到maven阶段,比如clean,你可以使用maven-clean-plugin并通过设置访问存储库,例如:

 <plugin>
    <inherited>false</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <phase>clean</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Base clean is attached to deleting local maven cache</echo>
                    <echo>${settings.localRepository}</echo>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <inherited>false</inherited>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <filesets>
            <fileset>
                <directory>${settings.localRepository}</directory>
            </fileset>
        </filesets>
    </configuration>
</plugin>

这里有一些命令可以用于清理

 1. mvn clean cache   
 2. mvn clean install 
 3. mvn clean install -Pclean-database

此外,从.m2中删除存储库文件夹也会有所帮助。


使用mvn dependency:purge-local-repository - dacttransitive =false -Dskip=true如果你有maven插件作为模块之一。否则,Maven将尝试重新编译它们,从而再次下载依赖项。


我也遇到过同样的问题,我在shell中编写了一行程序来解决这个问题。

rm -rf $(mvn help:evaluate -Dexpression=settings.localRepository\
                       -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN -B \
                       -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn | grep -vF '[INFO]')/*

我把它当做一行代码,因为我想有一个jenkins项目,只要我需要,就可以简单地运行它,这样我就不必登录到东西上,等等。 如果你允许自己为它编写一个shell脚本,你可以把它写得更清楚:

#!/usr/bin/env bash
REPOSITORY=$(mvn help:evaluate \
  -Dexpression=settings.localRepository \
  -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN \
  -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
  --batch-mode \
  | grep -vF '[INFO]')

rm -rf $REPOSITORY/*

Should work, but I have not tested all of that script. (I've tested the first command, but not the whole script.) This approach has the downside of running a large complicated command first. It is idempotent, so you can test it out for yourself. The deletion is its own command afterwards, and this lets you try it all out and check that it does what you think it does, because you shouldn't trust deletion commands without verification. However, it is smart for one good reason: It's portable. It respects your settings.xml file. If you're running this command, and tell maven to use a specific xml file (the -s or --settings argument), this will still work. So you don't have to fiddle with making sure everything is the same everywhere.

这有点笨拙,但在我看来,这是一种体面的做生意方式。