290

I've added a new dependency to my POM.

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

naXa
  • 26,677
  • 15
  • 154
  • 213
DJ180
  • 15,554
  • 19
  • 59
  • 105
  • 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 Jul 24 '19 at 07:47

5 Answers5

687

If you want to only download dependencies without doing anything else, 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=...

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
Andrew Spencer
  • 12,045
  • 4
  • 24
  • 44
  • 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 Mar 28 '12 at 11:12
  • 1
    I think I've found the solution. The parameter should be "repoUrl" and not "repositoryUrl". – Chry Cheng Mar 29 '12 at 05: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 Feb 22 '18 at 10:21
  • Your repository URLs might be missing/wrong. In any case, I don't think this answer is the answer to the problem you're having. This answer covers the case when Maven can find all the artefacts it needs in the configured repositories. – Andrew Spencer Feb 22 '18 at 10:36
  • I don't care if you are a believer or not, a christian or not... God bless you this awesome simple answer. – Salathiel Genèse Feb 28 '19 at 20:51
  • I noted that mvn eclipse:eclipse does not download all dependencies, because if i run eclipse plugin first and after mvn dependency:resolve, more jars are downloaded in m2 repo. I guess that eclipse:eclipse is somehow a plugin that readout the pom and configure eclipse workspace adding proper classpath and buildpath in order to resolve classes, generated sources and so inside the eclipse IDE, but may you tell me where the eclipse:eclipse command will end its work in the maven lifecycle? – Paolo Mar 25 '20 at 07:13
  • I don't know - worth asking as a top-level question if you need an answer. – Andrew Spencer Mar 25 '20 at 13:29
206

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.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
  • 2
    Thanks, I also discovered that adding it to the pom in STS will automatically download it for you. – DJ180 Dec 19 '11 at 17:35
  • 20
    @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. – botchniaque Jun 01 '16 at 10:11
  • 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 – Binita Bharati Jul 07 '16 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 May 11 '17 at 19:08
  • Hi I have ` org.json json 20180813 ` in dependencies inside pom.xml but mvn install throws error `package org.json.simple does not exist` during compilation – Kishan Mehta Jan 31 '19 at 08:43
  • 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. – Aaron Digulla Feb 05 '19 at 14:42
  • Ok @Aron sure! Would add it soon – Kishan Mehta Feb 05 '19 at 14:50
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.

Community
  • 1
  • 1
cнŝdk
  • 28,676
  • 7
  • 47
  • 67
  • 3
    Running eclipse:eclipse after dependency:resolve helped me see downloaded jars in eclipse, thanks! – Anatoly Yakimchuk Mar 22 '17 at 09: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 Apr 26 '17 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 Apr 26 '17 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 Mar 25 '20 at 07: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 Mar 25 '20 at 08: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.

NullPointer
  • 195
  • 1
  • 1
  • 6
-4

Right, click on the project. Go to Maven -> Update Project.

The dependencies will automatically be installed.