913

I imported my already working project on another computer and it started to download dependencies.

Apparently my internet connection crashed and now I get the following:

    >Build errors for comics; org.apache.maven.lifecycle.LifecycleExecutionException:
    Failed to execute goal on project comicsTest: Could not resolve dependencies for project comicsTest:comicsTest:war:0.0.1-SNAPSHOT:
    The following artifacts could not be resolved:
    org.springframework:spring-context:jar:3.0.5.RELEASE,
    org.hibernate:hibernate-entitymanager:jar:3.6.0.Final,
    org.hibernate:hibernate-core:jar:3.6.0.Final,
    org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final,
    org.aspectj:aspectjweaver:jar:1.6.8,
    commons-lang:commons-lang:jar:2.5,

    >mysql:mysql-connector-java:jar:5.1.13: Failure to transfer org.springframework:spring-context:jar:3.0.5.RELEASE from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced.

    >Original error: Could not transfer artifact org.springframework:spring-context:jar:3.0.5.RELEASE from central (http://repo1.maven.org/maven2): No response received after 60000

How do I force maven to update?

EarlGrey
  • 2,514
  • 4
  • 34
  • 63
M4ks
  • 11,744
  • 7
  • 27
  • 48
  • Side note: I had this issue with Atlassian Maven (from plugin SDK) and upgrading to newer version solved the problem. – Wirone Oct 08 '15 at 05:04

26 Answers26

1894
mvn clean install -U

-U means force update of snapshot dependencies.

Release dependencies will be updated this way if they have never been previously successfully downloaded. ref: https://stackoverflow.com/a/29020990/32453

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Jackie
  • 25,199
  • 6
  • 33
  • 24
  • 38
    I agree with lwpro2, this solved my problem as well. Just doing mvn clean isn't enough. The answer of Navi is not enough to solve the problem. – R. van Twisk Jul 19 '12 at 22:15
  • 1
    I believe this is more complicated if you were using artifactory and used the repository id "central", because the metadata doesn't apparently get cleaned out. – ggb667 May 12 '14 at 20:45
  • 3
    This doesn't seem to actually update the snapshot dependency jars, just the metadata. – Roddy of the Frozen Peas Nov 05 '15 at 21:33
  • 1
    if it's caused by the .lastupdated file, generated from the last unsuccessful dependency downloading, this method will not work, we need something like Rober Reiz's answer – Junchen Liu May 04 '16 at 10:44
  • 1
    I'm looking at the help for previous versions and up to maven 2.0.4, the description for the -U flag was: "-U,--update-snapshots Update all snapshots regardless of repository policies" starting with 2.0.5, it became: " -U,--update-snapshots Forces a check for updated releases and snapshots on remote repositories" And it's been like that since then. Both versions were released in 2007. I wonder if the answer must be edited to account for the releases, too? – cleberz Nov 03 '17 at 20:15
  • How does one do this in an IDE such as IntelliJ 2018? – Basil Bourque Oct 01 '18 at 03:16
  • actually, `mvn compile -U` should suffice in many cases, right? – Qw3ry Apr 08 '20 at 06:58
147

If your local repository is somehow mucked up for release jars as opposed to snapshots (-U and --update-snapshots only update snapshots), you can purge the local repo using the following:

 mvn dependency:purge-local-repository

You probably then want to clean and install again:

 mvn dependency:purge-local-repository clean install

Lots more info available at https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html

Nathan
  • 8,093
  • 8
  • 50
  • 76
FuzzyJulz
  • 2,714
  • 1
  • 16
  • 18
  • 19
    As soon as I did `mvn dependency:purge-local-repository`, it simply re-downloaded all the dependencies again, which is exactly what I wanted – smac89 Apr 05 '18 at 15:48
  • https://stackoverflow.com/questions/71176805/in-xhtml-page-jsf-attribute-are-not-rendering @FuzzyJulz – ritu mansata Feb 19 '22 at 12:44
113

-U seems to force update of all SNAPSHOT dependencies.

If you want to update a single dependency without clean or -U you could just remove it from your local repo and then build.

The example below if for updating slf4j-api 1.7.1-SNAPSHOT:

rm -rf ~/.m2/repository/org/slf4j/slf4j-api/1.7.1-SNAPSHOT
mvn compile
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Ayush Gupta
  • 5,568
  • 2
  • 21
  • 17
  • This works nicely if you're using another tool other than maven that references your local maven repository. – Mansoor Siddiqui Oct 16 '15 at 21:31
  • https://stackoverflow.com/questions/71176805/in-xhtml-page-jsf-attribute-are-not-rendering @Ayush Gupta – ritu mansata Feb 19 '22 at 12:44
  • I used this to update a single snapshot version of a library which was cached locally, but had changed remotely. This is faster than `mvn clean ...` when you have many dependencies. – Lucio Paiva May 10 '22 at 11:21
66

All the answers here didn't work for me. I used the hammer method:

find ~/.m2/ -name "*.lastUpdated" | xargs rm

That fixed the problem :-)

Jens
  • 67,715
  • 15
  • 98
  • 113
Robert Reiz
  • 4,243
  • 2
  • 30
  • 43
  • 1
    @DJ2 This deletes all files in the `.m2`-directory named `*.lastUpdated` – sjngm Dec 17 '18 at 07:51
  • 1
    `find ~/.m2/ -name "*.lastUpdated" -delete` would do without the danger of `xargs` or `rm` exceeding some limits (if many files are found). – A Sz Feb 03 '20 at 14:34
  • 1
    @ASz `xargs` reads the arguments from stdin, which shouldn't have any limits. It then splits the arguments into multiple `rm` calls if the number of arguments exceeds the system-defined limit. The `xargs rm` version shouldn't fail on limits, but doing it in one `find` call is of course still nicer (and shouldn't fail if a file name contains a newline). – jazzpi Dec 15 '20 at 08:27
46

You can do effectively from Eclipse IDE. Of course if you are using it.

Project_Name->Maven->Update Project Configuration->Force Update of Snapshots/Releases
Amit Shakya
  • 1,396
  • 12
  • 27
  • 1
    I did the command line version in the answer by @lwpro2, which let me build *on the command line*, but then I still could not build within Eclipse. Doing this got it to build through Eclipse. – Brick Jul 15 '16 at 02:31
  • Works for me only if I close and reopen Eclipse after executing the update. – ceklock Jan 02 '17 at 16:58
  • https://stackoverflow.com/questions/71176805/in-xhtml-page-jsf-attribute-are-not-rendering @Amit Shakya – ritu mansata Feb 19 '22 at 12:45
46

Just in case someone wants only update project's snapshot dependencies and doesn't want to install artifact:

mvn dependency:resolve -U

Don't forget to reimport dependencies in your IDE. In IDEA you need to right click on pom file and choose Maven -> Reimport

GlaIZier
  • 805
  • 9
  • 12
14

If you're unsure what is inside your local repository, I recommend to fire a build with the option:

-Dmaven.repo.local=localrepo

That way you'll ensure to build in a cleanroom environment.

merejy
  • 414
  • 4
  • 17
  • 5
    this command means use folder localrepo to be the local repository. that is fine for a single project workspace but bad if you have more than one project, and few depend on the other. then your better of giving a full path like -Dmaven.repo.local=/data/my/localrepoDir – tgkprog Aug 25 '13 at 10:45
12

In my case first I did was:

mvn clean install -U

Still it was showing same error then I closed project and again reopened it. Finally worked.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
rajeev
  • 499
  • 1
  • 8
  • 17
7

If you are using eclipse IDE then :

  • Select Project.
  • Press alt+F5, window for Update Maven Project will pop up.

  • Check - Force Update of Snapshots/releases and click OK.

If Using Intellij IDE

  • go to settings/Maven
  • check Always update snapshots
Davis Broda
  • 4,102
  • 5
  • 23
  • 37
patidarsnju
  • 439
  • 5
  • 10
6

I used the IntelliJ IDE and I had a similar problem and to solve I clicked in "Generate Sources and Update Folders for All Projects" in Maven tab.

enter image description here

Willyan
  • 123
  • 2
  • 7
6

Previous versions of maven did not force the check for missing releases when used -U with mvn clean install, only the snapshots, though newer version supports this.

For someone still struggling with previous version, following can be helpful-

On Windows:

cd %userprofile%\.m2\repository
for /r %i in (*.lastUpdated) do del %i

On Linux:

find ~/.m2  -name "*.lastUpdated" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \;

Whenever maven can't download dependencies for any reason (connectivity/not exists etc), it will add the ".error=Could not transfer artifact" in dependency-name.lastUpdate file in respective folder under $home/.m2 directory. Removing these files will force maven to try fetching the dependencies again.

ankidaemon
  • 1,363
  • 14
  • 20
  • Can you name, which "previous" and "newer" versions you are talking about and where you found the information, that "newer" versions support this? – Tobias Liefke May 18 '22 at 07:59
4
mvn clean install -e -U -Dmaven.test.skip=true
  1. -e Detailed exception
  2. -U forced update
  3. -DskipTests does not execute test cases, but compiles test case classes to generate corresponding class files under target/test classes.
  4. -Dmaven.test.skip=true, do not execute test cases or compile test case classes.Using maven. test. skip not only skips running unit tests, but also skips compiling test code.

A small suggestion. If you use the IntelliJ Idea compiler, it is recommended to clean the cache

qingmu
  • 402
  • 2
  • 12
2

I've got the error in an other context. So my solution might be useful to others who stumple upon the question:

The problem: I've copied the local repository to another computer, which has no connection to a special repository. So maven tried to check the artifacts against the invalid repository.

My solution: Remove the _maven.repositories files.

Matthias M
  • 12,906
  • 17
  • 87
  • 116
2

You need to check your settings.xml file under <maven_home>/conf directory.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1

This is one of the most annoying things about Maven. For me the following happens: If I add a dependency requesting more dependencies and more and more but have a slow connection, it seams to stop while downloading and timing out. While timing out all dependencies not yet fetched are marked with place holders in the .m2 cache and Maven will not (never) pick it up unless I remove the place holder entry from the cache (as other stated) by removing it.

So as far as I see it, Maven or more precise the Eclipse Maven plugin has a bug regarding this. Someone should report this.

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77
1

It's important to add that the main difference of running mvn with -U and without -U is that -U will override your local SNAPSHOT jars with remote SNAPSHOT jars.

Local SNAPSHOT jars created from local mvn install in cases where you have other modules of your proj that generate jars.

Johnny
  • 14,397
  • 15
  • 77
  • 118
1

For fixing this issue from Eclipse:

1) Add below dependency in Maven pom.xml and save the pom.xml file.

<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.3.1</version>
</dependency>

2) Go to project >> Maven >> Update Project

select the project and click OK.

3) Optional step, if it's not resolved till step 2 then do below step after doing step-1

Go to project >> Maven >> Update Project >> check in the checkbox 'Force Update of Snapshots/Releases'

select the project and click OK.

Rodia
  • 1,407
  • 8
  • 22
  • 29
TarunChhabra
  • 99
  • 1
  • 4
1

-U is used to force update maven Repo. Use

mvn -U clean install
Sanjeev Guglani
  • 2,114
  • 1
  • 7
  • 13
  • @KerwinSneijders I had added answer 2years ago at that time , accepted answer did not provide information about what -U does here – Sanjeev Guglani Apr 23 '20 at 16:02
0

I've got the same error with android-maps-utils dependency. Using aar type package in dependency section solve my problem. By default type is jar so It might be checked what type of dependency in repository is downloaded.

zhen_khokh
  • 69
  • 1
  • 8
0

I tried all the answers here but nothing seemed to work. Restarted my computer first then ran mvn clean install -U. That solved my problem.

Gelo
  • 105
  • 10
0

What maven does is, it downloads all your project's dependencies into your local repo (.m2 folder). Because of the internet causing issues with your local repo, you project is facing problems. I am not sure if this will surely help you or not but you can try deleting all the files within the repository folder inside the .m2 folder. Since there would be nothing in the local repo, maven would be forced to download the dependencies again, thus forcing an update. Generally, the .m2 folder is located at c:users:[username]:.m2

Ketan R
  • 4,039
  • 1
  • 13
  • 15
0

after using mvn clean install -U run as maven test also and after that update your project using maven-update project this works in my case

ritu mansata
  • 77
  • 2
  • 13
-2

I had this problem for a different reason. I went to the maven repository https://mvnrepository.com looking for the latest version of spring core, which at the time was 5.0.0.M3/ The repository showed me this entry for my pom.xml:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.0.M3</version>
</dependency>

Naive fool that I am, I assumed that the comment was telling me that the jar is located in the default repository.

However, after a lot of head-banging, I saw a note just below the xml saying "Note: this artifact it located at Alfresco Public repository (https://artifacts.alfresco.com/nexus/content/repositories/public/)"

So the comment in the XML is completely misleading. The jar is located in another archive, which was why Maven couldn't find it!

user1208639
  • 303
  • 3
  • 4
-3

We can force to get latest update of release and snapshot repository with below command :

mvn --update-snapshots clean install
josliber
  • 43,891
  • 12
  • 98
  • 133
-4

I had the same error and running mvn install -U and then running mvn install worked for me.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 11
    Please don't post *"the thing mentioned in all the other answers worked for me too"* answers. – kryger Oct 17 '16 at 14:00
-6

mvn clean install -U doesn't work. However mvn -U clean followed by mvn clean install does.

mroman
  • 1,354
  • 9
  • 14