392

I've added a new dependency to my POM.

Is there a simple command I can run to download this dependency to my repository?

2
  • For those looking on how to do this in a Spring Boot project: use mvnw to call the wrapper layer. The commands on this page work with it.
    – G_V
    Commented Jul 24, 2019 at 7:47
  • @G_V Can you expand on that? How would we use `mvnw`` to add the dependencies? Commented Sep 29, 2023 at 17:21

4 Answers 4

852

Per other answer, Maven will download the dependency as part of mvn compile, mvn install etc. By default it happens in the generate-sources phase of the default lifecycle - so invoking any phase from generate-sources onward will download dependencies.

If you want to only download dependencies without doing other activities such as compilation, then it's:

mvn dependency:resolve

Or to download a single dependency:

mvn dependency:get -Dartifact=groupId:artifactId:version

If you need to download from a specific repository, you can specify that with -DrepoUrl=...

12
  • 2
    I get this error when I run that command: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get (default-cli) on project standalone-pom: The parameters 'repositoryUrl' for goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get are missing or invalid -> [Help 1]. Specifying -DrepositoryUrl=... doesn't work.
    – Chry Cheng
    Commented Mar 28, 2012 at 11:12
  • 1
    I think I've found the solution. The parameter should be "repoUrl" and not "repositoryUrl".
    – Chry Cheng
    Commented Mar 29, 2012 at 5:08
  • 1
    Doesn't solve the issue for me: the I run mvn package -o right after — I get error that plugins can not be downloaded. Running mvn dependency:resolve-plugins doesn't fully solve the issue either.
    – Innokenty
    Commented Feb 22, 2018 at 10:21
  • 1
    @Basti dependencies will be downloaded when running Maven lifecycle phases such as maven compile so for most people, that's all you need. But sometimes people want to download dependencies for whatever reason, without also invoking any other activities bound to any lifecycle stages. And that's what this question is for. In other words, if you're just using Maven normally, you don't need this question/answer. Commented Mar 5 at 8:50
  • 1
    I updated the answer to first point out that it happens anyway in most lifecycle phases. When I first wrote the answer, it was below the other answer that explained this. Commented Mar 5 at 9:19
249

mvn install (or mvn package) will always work.

You can use mvn compile to download compile time dependencies or mvn test for compile time and test dependencies but I prefer something that always works.

7
  • 2
    Thanks, I also discovered that adding it to the pom in STS will automatically download it for you.
    – DJ180
    Commented Dec 19, 2011 at 17:35
  • 26
    @Andrew Spencer's reply is more accurate - mvn dependency:xxx deal with dependencies only and don't do any additional stuff - and that what the question was about. Commented Jun 1, 2016 at 10:11
  • 1
    At times, 'mvn package' may not update dependencies. Has happened to me more than once.One needs to run 'mvn dependency:resolve' in such cases Commented Jul 7, 2016 at 15:21
  • 1
    @BinitaBharati, you can add a -U to the Maven command line to force dependency downloads. This is useful if Maven doesn't download an updated dependency because of a cache timeout.
    – BamaPookie
    Commented May 11, 2017 at 19:08
  • 1
    @Kishan Ask a new question. Show the layout of your project (especially where the import happens) and whether you use a multi-module build. Commented Feb 5, 2019 at 14:42
13

I know it is an old question now, but for users who are using Maven plugin with Eclipse under Windows, you have two options:

  1. If you got Maven installed as a standalone application:

    You can use the following command in the CMD under your project path:

    mvn eclipse:eclipse
    

    It will update your repository with all the missing jars, according to your dependencies in your pom.xml file.

  2. If you haven't got Maven installed as a standalone application you can follow these steps on your eclipse:

    Right click on the project ->Run As -- >Run configurations.

    Then select mavenBuild.

    Then click new button to create a configuration of the selected type .Click on Browse workspace then select your project and in goals specify eclipse:eclipse

You can refer to how to run the command mvn eclipse:eclipse for further details.

6
  • 3
    Running eclipse:eclipse after dependency:resolve helped me see downloaded jars in eclipse, thanks! Commented Mar 22, 2017 at 9:39
  • 2
    While this answer will help the poor folk stuck with Eclipse, I strongly recommend that anyone using Eclipse find a better alternative. Especially if you're going to be using Maven. Netbeans and IntelliJ are light years ahead.
    – 64BitBob
    Commented Apr 26, 2017 at 13:20
  • @64BitBob Assuming that Netbeans and IntelliJ are better than eclipse, we should always give a solution for those who use it. :)
    – cнŝdk
    Commented Apr 26, 2017 at 13:26
  • I see that the plugin is not available anymore in marketplace but yes it works in Eclipse 2020 without downloading anything. I wonder if mvn eclipse:eclipse is the command sent by eclipse itself when we rightclick->Maven->Update Project...
    – Paolo
    Commented Mar 25, 2020 at 7:06
  • @Paolo In my opinion they have automatically added the plugin in new versions of Eclipse, and yes I think it's the same command behind the "Update project" option.
    – cнŝdk
    Commented Mar 25, 2020 at 8:37
-1

Pay attention to your dependency scope I was having the issue where when I invoke clean compile via Intellij, the pom would get downloaded, but the jar would not. There was a xxx.jar.lastUpdated file created. Then realized that the dependency scope was test, but I was triggering the compile. I deleted the repos, and triggered the mvn test, and issue was resolved.

Not the answer you're looking for? Browse other questions tagged or ask your own question.