273

Recently, Apache Maven seems to be having caching issues. Performing clean installs on our projects using Windows Vista or Windows 7 sometimes produce artifacts with the same data as a previous build even though the newer artifact's files should have been updated.

Is there any way to clear this cache to force maven to always trigger a clean build of the local artifact that should be built?

In particular, we're having issues building a webapp with the war plugin. Maven version is 3.0.3. War plugin version is 2.1.1.

f_puras
  • 2,521
  • 4
  • 33
  • 38
MetroidFan2002
  • 29,217
  • 16
  • 62
  • 80
  • 5
    Have you tried the `-U` flag which will update artifacts? – Amir Raminfar Sep 13 '11 at 21:13
  • 3
    Can you give more details? Are the dependants not getting updated in the war? If so, are the dependants having SNAPSHOT versions? – Raghuram Sep 14 '11 at 04:35
  • 1
    Is it a multimodule project? Have you checked the version number of the depdendent modules? Could you reproduce? `mvn clean install` put the new artifacts to the local repository? – palacsint Sep 14 '11 at 07:55
  • @MetroidFan2002 Can we please have a marked answer here. Or you can write a solution if you found any on your own. – Naman Jan 09 '17 at 05:02
  • 1
    `mvn clean install -Dmaven.repo.local=/alternate/repo/location` – Gayan Weerakutti Apr 19 '17 at 07:09

11 Answers11

277

Delete the artifacts (or the full local repo) from c:\Users\<username>\.m2\repository by hand.

Peter G
  • 2,773
  • 3
  • 25
  • 35
palacsint
  • 28,416
  • 10
  • 82
  • 109
  • 4
    Tried that already, didn't work. Thanks for the suggestion though. – MetroidFan2002 Sep 14 '11 at 00:34
  • 3
    it seems like, even after a restart, a heck of a lot of maven artifacts have handles to them: `The action can't be completed because the folder or file in it is open in another program. Close the folder or file and try again.` – liltitus27 Feb 11 '16 at 22:15
  • 2
    Note that the folder location might vary on your system - see [this answer](http://stackoverflow.com/a/16218772/245966) for how to get the maven repo folder path – jakub.g Jan 16 '17 at 18:03
  • 15
    @liltitus27 same here, just wiping out ~/.m2/repository did not work, `mvn dependency:purge-local-repository` finally worked – qbert65536 Oct 02 '17 at 15:47
  • yes this worked for me 100% - Windows10 on a corporate network as a user not admin – Graeme Phillips Jun 08 '18 at 10:32
  • @KorayTugay It might be using an internally bundled Gradle distribution. Many tools do that, annoyingly. IntelliJ IDEA is one of them – rath Sep 26 '18 at 12:22
218

To clean the local cache try using the dependency plug-in.

  1. 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.
  2. 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.
  3. 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.
Captain Man
  • 6,997
  • 6
  • 48
  • 74
Brian C.
  • 6,455
  • 3
  • 32
  • 42
  • From the command line on a linux system. You will need to install mvn. http://maven.apache.org/install.html – Brian C. Oct 21 '16 at 13:53
  • I'll add that you can target specific groups/artifacts with an additional parameter. This answer is definitely what I was looking for (option 3) . Here is that extra parameter ::: mvn dependency:purge-local-repository -DmanualInclude="myGroupId" -DsnapshotsOnly=true -DactTransitively=false -DreResolve=false – granadaCoder Jan 27 '19 at 12:00
  • I tried the 3rd option with the purpose to delete all the contents within the _local repository_, but it does not work - until this point the unique way to accomplish this goal is deleting manually all the contents of that directory. I have the _local repository_ in a different location than `/.m2` location. Not sure if it would be the reason. – Manuel Jordan May 27 '20 at 14:57
16

I would do the following:

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

The flags tell maven not to try to resolve dependencies or hit the network. Delete what you see locally.

And for good measure, ignore errors (--fail-at-end) till the very end. This is sometimes useful for projects that have a somewhat messed up set of dependencies or rely on a somewhat messed up internal repository (it happens.)

luis.espinal
  • 10,331
  • 6
  • 39
  • 55
  • 1
    Is it not enough to simply delete the local cache folder for this kinda cleanup? –  Mar 30 '20 at 15:18
  • 1
    You could if you know every single transitive dependency. But then it becomes tedious (or we could just delete the whole cache, but that will force maven to download everything again for things unrelated to the artifact in question.) It all depends on the specifics of the situation. – luis.espinal Mar 30 '20 at 17:57
14

Have you checked/changed the updatePolicy settings for your repositories in your settings.xml.

This element specifies how often updates should attempt to occur. Maven will compare the local POM's timestamp (stored in a repository's maven-metadata file) to the remote. The choices are: always, daily (default), interval:X (where X is an integer in minutes) or never.

Try to set it to always.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
8

Use mvn dependency:purge-local-repository -DactTransitively=false -Dskip=true if you have maven plugins as one of the modules. Otherwise Maven will try to recompile them, thus downloading the dependencies again.

vladimirror
  • 729
  • 12
  • 8
6

This works on the Spring Tool Suite v 3.1.0.RELEASE, but I'm guessing it's also available on Eclipse as well.

After deleting the artifacts by hand (as stated by palacsint above) in the /username/.m2 directory, re-index the files by doing the following:

Go to:

  • Windows->Preferences->Maven->User Settings menu.

Click the Reindex button next to the Local Repository text box. Click "Apply" then "OK" and you're done.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Felby
  • 4,045
  • 4
  • 26
  • 23
5

I'm surprised no one has mentioned it yet but

mvn clean install -U

The -U flag tells Maven to pull fresh copies of all dependencies without using the local cache.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
2

As some answers have pointed out, sometimes you really want to delete the local repository entirely, for example, there might be some artifacts that can't be purged as they are not anymore referenced by the pom.

If you want to have this deletion embedded in a maven phase, as for example clean you can use the maven-clean-plugin and access the repository through the settings, for example:

 <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>
0

I agree on using mvn clean install -U as first option. You don't really need to clear all the local repository.

-1

I've had this same problem, and I wrote a one-liner in shell to do it.

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]')/*

I did it as a one-liner because I wanted to have a Jenkins-project to simply run this whenever I needed, so I wouldn't have to log on to stuff, etc. If you allow yourself a shell-script for it, you can write it cleaner:

#!/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.

It's a bit wieldy, but it's a decent way of doing business, IMO.

Haakon Løtveit
  • 1,009
  • 10
  • 18
-11

So there are some commands which you can use for cleaning

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

also deleting repository folder from .m2 can help.

Codiee
  • 3,047
  • 2
  • 17
  • 18