41

I would like something like the following.

I want just an utility that is able to download jars and their dependencies from the Maven Repository without imposing no constraints on how my project should be built.

I would like something like this:

download-jar --dest=lib/ 'commons-io:commons-io:jar:1.4'

It should be able to download also the dependencies.

Update:

I wouldn't know about a pom.xml should be structured.

The only task I need to be accomplished is the download of the jars, I would like have a tool that could accomplish this task that doesn't bother me with superflous information.

There is something like that?

Andrea Francia
  • 9,737
  • 16
  • 56
  • 70
  • I don't think this is a duplicate, because the referenced question regards fetching deps to the local m2 repo, whereas this question specifies "without imposing no constraints" with an example showing a custom destination (lib/). – Chris2048 Jan 03 '14 at 18:04
  • I totally agree. And the answer is: mvn -Dartifact=commons-io:commons-io:1.4:jar -DoutputDirectory=$(pwd) -Dpackaging=jar dependency:copy – user1050755 Mar 30 '14 at 16:25
  • Improved argument order: mvn dependency:copy -DoutputDirectory=$(pwd) -Dartifact=commons-io:commons-io:1.4:jar – user1050755 Mar 30 '14 at 16:31

5 Answers5

61

If you want to download maven dependencies into your lib directory use the dependency plugin with the copy-dependencies function.

mvn -DoutputDirectory=./lib -DincludeArtifactIds=commons-logging,commons-io dependency:copy-dependencies 

Without the -DincludeArtifactIds part you'll download every dependency.

If you want to download a an artifact without having a specific project *see below** :

mvn -DgroupId=commons-io -DartifactId=commons-io -Dversion=1.4 dependency:get

Resources :

On the same topic :

Interesting comments :

  • *@Pascal Thivent :

    No need to setup a POM, no need to develop your own tool, use mvn dependency:get. That's the right answer to this question.

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 1
    Thank you. This requires to have a proper pom.xml. This is the think I would avoid. Do you know a way to obtain the same result without defining the pom? – Andrea Francia Aug 30 '10 at 15:58
  • Making a proper maven pom.xml and using that to download your dependencies takes about a 5 minutes. Setting up some other software and configuring it to scrape the library and parse POM.xmls for your dependencies and download those dependencies recursively will take quite a bit longer. – Chris Nava Aug 30 '10 at 16:45
  • 9
    @Chris No need to setup a POM, no need to develop your own tool, use `mvn dependency:get`. – Pascal Thivent Aug 31 '10 at 14:34
  • @Pascal Thivent, Nice one, `dependency:get` doesn't appear on the dependency's goals list. – Colin Hebert Aug 31 '10 at 14:37
  • 2
    @Colin You need version 2.1 of the dependency plugin. Follow [the link](http://stackoverflow.com/questions/1776496/a-simple-command-line-to-download-a-remote-maven2-artifact-to-the-local-repositor) of my comment to the question. – Pascal Thivent Aug 31 '10 at 14:43
  • @Pascal Even quicker. I learn something new here every day. – Chris Nava Sep 01 '10 at 04:13
  • @Pascal Just to be clear, dependency:get installs to local m2 repo? According to **Eric B.** s answer [here](http://stackoverflow.com/questions/1776496/a-simple-command-line-to-download-a-remote-maven2-artifact-to-the-local-reposito), it has a **-Ddest** option, but since that specifies a filename then I assume we arene't resolving *its'* deps (i.e. we just fetch that one jar); Is this all correct? – Chris2048 Jan 03 '14 at 18:11
  • Just a note, copy-dependencies requires a project/POM, and **-DincludeArtifactIds** will only get artifacts specified in the POM; Any others specified will be ignored (with no error message regarding this). Also, it appears that there may be an effect on transitive dependencies, as mentioned by **Matthias** [here](http://stackoverflow.com/questions/11973889/copy-dependencies-transitive-and-not-transitive). – Chris2048 Jan 03 '14 at 18:26
  • 9
    `dependency:get` is great, but is there a way to download the artifacts into a particular directory, instead of into the local Maven repo? – Aviv Cohn May 14 '16 at 15:19
  • `-DoutputDirectory=/path/to/output` @AvivCohn – sytech Mar 25 '21 at 21:16
20

I also had to specify -DrepoUrl, after getting the error message:

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]

So here is the command I used:

mvn -DgroupId=edu.umd -DartifactId=cloud9 -Dversion=1.3.5 \
  -DrepoUrl="http://repo1.maven.org/maven2" dependency:get

Furthemore, -Ddest=~ didn't work. It always insisted on installing the jar to ~/.m2/repository.

chbrown
  • 11,865
  • 2
  • 52
  • 60
  • THANK YOU!!! This was super helpful for me. No `pom.xml` needed ;) – steve May 04 '13 at 00:13
  • What is with the `\\` in the middle of the command? -_- – Iulian Onofrei Dec 21 '13 at 14:37
  • "\" allows copy-&-pasting the command into your terminal or shell script. Otherwise the newlines would trigger execution of each line individually. It could be one long line, but SO doesn't use `white-space: pre-wrap`, unfortunately, so too-long lines get cut off. – chbrown Dec 21 '13 at 16:15
9

Maven3 uses dependency plugin v2.1 by default:

$ mvn dependency:get -DrepoUrl=http://download.java.net/maven/2/ \
   -DgroupId=commons-io -DartifactId=commons-io -Dversion=1.4

With Maven2 is still necessary to write the canonical name:

$ mvn2 org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
   -DrepoUrl=http://download.java.net/maven/2/ \
   -DgroupId=commons-io -DartifactId=commons-io -Dversion=1.4

Use parameter artifact to set the name of the artifact as group:artifact:version:

$ mvn dependency:get -DrepoUrl=http://download.java.net/maven/2/ \
   -Dartifact=commons-io:commons-io:1.4

Use LATEST to download the latest version of the artifact:

$ mvn dependency:get -DrepoUrl=http://download.java.net/maven/2/ \
   -Dartifact=commons-io:commons-io:LATEST
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • What is with the `\\` in the middle of the command? -_- – Iulian Onofrei Dec 21 '13 at 14:17
  • @IulianOnofrei nothing related to 'mvn' :) Character '\' is used in bash to concatenate a command that is splitted into two or more lines http://stackoverflow.com/questions/3871332/how-to-tell-bash-that-the-line-continues-on-the-next-line – Diego Pino Dec 21 '13 at 19:40
  • I see, I got annoyed it was not working until I realized I have to delete that, so that's a downside for unexperienced windows users though :( – Iulian Onofrei Dec 21 '13 at 23:56
1

You should take a look at the maven dependency plugin, maybe ... and especially its go-offline mojo

Riduidel
  • 22,052
  • 14
  • 85
  • 185
0

Have a look at Ivy. It allows dependency resolution from maven repositories without the overkill complexity of maven itself.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151