152

I'm working on a project with dependency X. X, in turn, depends on Y.

I used to explicitly include Y in my project's pom. However, it was not used and to make things cleaner, I instead added it to X's pom as a dependency. X is marked as a release dependency.

The problem is that after removing Y from my project's pom and adding it to X's pom, my project isn't picking it up on mvn -U clean package. I know -U update snapshots but not releases.

So, without deleting the ~/.m2/repository directory how can I force a re-download of X's pom? Also, I tried running dependency:purge-local-repository and it didn't work either.

Yi Jiang
  • 46,385
  • 16
  • 133
  • 131
volni
  • 4,636
  • 8
  • 35
  • 44
  • 1
    Just from a methodology perspective, if you change `X`'s pom, then it should constitute a new release, even if it's only a patch number change: `1.3` -> `1.3.1`. So, that would eliminate this problem from the get-go. – jpaugh Jul 27 '16 at 14:55
  • However, it's still possible to have similar issues with snapshots as well, so it's good to have some answers. – jpaugh Jul 27 '16 at 14:56
  • 1
    You've tagged the questions as "maven-2". The -U option was added in Maven 3 to solve this problem. I'd suggest upgrading. – Mark O'Connor Oct 31 '11 at 21:52
  • As a point of reference -U does "affect" released versions, but only by downloading them if they have never been successfully downloaded before. Weird mix. https://stackoverflow.com/questions/29020716/maven-what-does-u-update-snapshots-really-do/29020990#29020990 – rogerdpack Apr 21 '21 at 17:13
  • `dependency:purge-local-repository` should work, does it not delete it? – rogerdpack Apr 21 '21 at 17:23

13 Answers13

187

You cannot make Maven re-download dependencies, but what you can do instead is to purge dependencies that were incorrectly downloaded using mvn dependency:purge-local-repository

See: http://maven.apache.org/plugins/maven-dependency-plugin/purge-local-repository-mojo.html

This looks up the list of all transitive dependencies of the current project, deletes them, then redownloads them. You can even add it as a plugin into your pom if you want to run it with every build.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
Ali Tokmen
  • 1,886
  • 1
  • 11
  • 2
  • 15
    Isn't that kind of the same thing? – Svish Mar 18 '13 at 17:19
  • 4
    It doesn't require access to the file system which might be an issue if you're only configuring build jobs (for a CI system for example). – Oliver Drotbohm Oct 09 '14 at 15:13
  • 3
    If multiple project run `mvn dependency:purge-local-repository clean package`, may be one project delete the same dependency in local repository while another project is running compile. How to avoid? – vikyd Nov 17 '17 at 04:43
  • @vikyd, not sure if this is the best way, but you could use the `MAVEN_OPTS` environment variable to force maven to download/use dependencies from a different folder from the standard `.m2/respository` location, like so: `export MAVEN_OPTS="-Dmaven.repo.local=${PROJECT_DIR}/maven.repository` This will prevent maven from using any dependencies in the normal folder and use the one specified by `Dmaven.repo.local=` instead. – Anomalyzero May 04 '21 at 19:59
60

I just deleted my ~/.m2/repository and that forced a re-download ;)

Ryan Angilly
  • 1,473
  • 15
  • 16
  • 1
    It helps as a last resort, I had a project where eclipse and maven were a bit "confused". Simply deleting the .m2 folder made the project compile and run correctly. – Leonardo Apr 28 '14 at 06:25
  • 7
    It's also possible to selectively delete the dependencies that you know need to be refreshed from inside the repository. A manual purge, if you will. – jpaugh Jul 27 '16 at 14:56
46

I think following command may help you!


mvn -U clean install
Richard
  • 1,840
  • 14
  • 16
  • 6
    **release dependency using Maven** in the question. No, this might have been better a comment. – Naman Dec 15 '16 at 19:17
43

Based on Ali Tokmen answer. I managed to force delete a single specific local dependency with the following command:

mvn dependency:purge-local-repository -DmanualInclude=com.skyfish:utils

With this, it removes utils from my .m2/repository and it always re-download the utils JAR dependency when I proceed to run mvn clean install. If you want something more programmatic possibly also see the -Dinclude=... option.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
skyfish
  • 431
  • 4
  • 3
  • 2
    I simply needed a way to force maven to re-download a dependency. This is the only answer that actually does that, whereas other answers suggest removing everything. Thank you – smac89 Apr 30 '18 at 18:26
16
mvn clean install -U

-U means force update of 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.

ArpitBora
  • 516
  • 8
  • 24
14

Project right click-> Maven -> Update Project and check the checkboxes as in the screen shot. It will update releases as well :)

enter image description here

nanosoft
  • 2,164
  • 4
  • 32
  • 50
  • 6
    It would be nice to know how they implemented that "/Releases" part. – Ulises Layera Oct 12 '17 at 19:24
  • Thanks for this just what I needed. – JamesG Dec 07 '17 at 03:51
  • Is this Netbeans? Regardless, I'm guessing that checkbox means to supply the `-U` command to maven, which "kind of" updates releases https://stackoverflow.com/a/29020990/32453 but not the way the OP was looking for :) – rogerdpack Apr 21 '21 at 17:02
9

If you know the group id of X, you can use this command to redownload all of X and it's dependencies

mvn clean dependency:purge-local-repository -DresolutionFuzziness=org.id.of.x

It does the same thing as the other answers that propose using dependency:purge-local-repository, but it only deletes and redownloads everything related to X.

smac89
  • 26,360
  • 11
  • 91
  • 124
7

When you added it to X, you should have incremented X's version number i.e X-1.2
Then X-1.2 should have been installed/deployed and you should have changed your projects dependency on X to be dependent on the new version X-1.2

crowne
  • 8,132
  • 2
  • 31
  • 45
5

If you really want to force-download all dependencies, you can try to re-initialise the entire maven repository. Like in this article already described, you could use:

mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install
Community
  • 1
  • 1
Bart den Haak
  • 71
  • 1
  • 2
3

Just delete ~/.m2/repository...../actual_path where the invalid LOC is coming as it forces to re-download the deleted jar files. Dont delete the whole repository folder instead delete the specific folder from where the error is coming.

Saurabh Oza
  • 131
  • 3
  • 18
-1

Deleting the ~/.m2/repository will solve your problem. But, if you still need to keep the old ~/.m2/repository you can just change the maven local path temporarily.

If you are working on IntelliJ just go to Maven Settings and change the Local Repository path to somewhere else. You may need to tick the override checkbox near there.

enter image description here

-3

Go to build path... delete existing maven library u added... click add library ... click maven managed dependencies... then click maven project settings... check resolve maven dependencies check box..it'll download all maven dependencies

  • 2
    This assumes use of some IDE, an assumption not supported by the question, and you didn't even specify what IDE! – Phil Apr 03 '17 at 23:53
-3

Most answers provided above would solve the problem.

But if you use IntelliJ and want it to just fix it for you automatically, go to Maven Settings.

Build,Execution, Deployment -> Build Tools -> Maven

enter image description here

Disable Work Offline

Enable Always update snapshots (Switch when required)

Caffeinated Coder
  • 642
  • 1
  • 8
  • 22