134

I have a library that I distribute using maven 2. The typical user of this library doesn't use maven to build their applications, but is likely somewhat familiar with maven and probably has it installed.

I'd like to document a "simple" one line command they can use to download my library's artifacts to their local ~/.m2/repository without requiring that they set up a pom.xml to do it.

I thought there was a way to do this, but I can't seem to find it after looking through the install:install-file and dependency plugin documentation. I tried things like:

mvn install:install-file -DrepositoryId=java.net -Durl=http://download.java.net/maven/2/ -Dfile=robo-guice-0.4-20091121.174618-1.jar -DpomFile=robo-guice-0.4-20091121.174618-1.pom -DgroupId=robo-guice -DartifactId=robo-guice -Dversion=0.4-SNAPSHOT -Dpackaging=jar

but I think I'm barking up the wrong tree since it appears that the install plugin is used to copy locally built files into the local repository, rather than download remote artifacts into the local repository.

This is the artifact I'd like to install: http://download.java.net/maven/2/robo-guice/robo-guice/0.4-SNAPSHOT/

Is this possible using maven?

emmby
  • 99,783
  • 65
  • 191
  • 249
  • Duplicate of http://stackoverflow.com/questions/1895492/how-can-i-download-a-specific-maven-artifact-in-one-command-line – Isaac Feb 20 '13 at 19:22
  • Possible duplicate of [How can I download a specific Maven artifact in one command line?](http://stackoverflow.com/questions/1895492/how-can-i-download-a-specific-maven-artifact-in-one-command-line) – Naman Jan 26 '17 at 15:53

3 Answers3

185

Since version 2.1 of the Maven Dependency Plugin, there is a dependency:get goal for this purpose. To make sure you are using the right version of the plugin, you'll need to use the "fully qualified name":

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
    -DrepoUrl=https://download.java.net/maven/2/ \
    -Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT
Archie
  • 4,959
  • 1
  • 30
  • 36
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Extra helpful for showing how to get the dependency plugin itself installed. – Sergio Acosta Apr 18 '10 at 06:49
  • 2
    Pascal, can you tell me how to define user+password to reach the repository with? The trick with http://user:password@repourl did not work. – Gábor Lipták Dec 02 '10 at 08:22
  • Also tried with a -DrepositoryId=something which is defined in settings.xml, dont work – Gábor Lipták Dec 02 '10 at 08:34
  • 4
    the goal is nice, but how can I define the target directory where I want the file to be copyed to? (and file name) – domi Jun 16 '11 at 08:44
  • To download the sources for this jar as well, use -Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT:jar:sources – Matthew Farwell Dec 02 '11 at 10:04
  • 9
    @domi (belated answer for posterity): use "-Ddest=path/to/my.jar"; otherwise, it is just copied to your local ~/.m2/repository (makes it look like nothing happened). See http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html#destination =example=> mvn org.apache.maven.plugins:maven-dependency-plugin:2.5.1:get -DremoteRepositories=http://repo.maven.apache.org -Dartifact=org.apache.ant:ant:1.8.1 -Ddest=ant-1.8.1.jar (result: ant-1.8.1.jar in current directory) – michael Sep 10 '12 at 07:30
  • How could I download from authenticated repository? I'm receiving " Not authorized , ReasonPhrase:Unauthorized." Thanks – gavioto Jun 03 '13 at 12:56
  • 3
    Thanks! While, in the doc link you posted, there is `Deprecated. Use remoteRepositories` for `repoUrl`. Please update the answer maybe:) – Weekend Oct 16 '15 at 04:13
  • 1
    Here's a working example of mine: `mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get -DrepoUrl=https://repo.maven.apache.org/maven2/ -Dartifact=com.amazonaws:aws-java-sdk-support:1.10.77` – Sridhar Sarnobat Oct 03 '17 at 00:49
53

Give them a trivial pom with these jars listed as dependencies and instructions to run:

mvn dependency:go-offline

This will pull the dependencies to the local repo.

A more direct solution is dependency:get, but it's a lot of arguments to type:

mvn dependency:get -DrepoUrl=something -Dartifact=group:artifact:version
bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 1
    Good idea, although a command line they could copy-paste would be easier – emmby Nov 21 '09 at 19:55
  • If I could think of one I'd post it. I don't know of any plugin that does quite what's called for. I know how to write one ... – bmargulies Nov 21 '09 at 20:39
  • The `get` mojo actually expects `-DrepoUrl` to be present, not `-DrepositoryUrl`. Regarding the arguments to type, there is no magic, you need to provide these informations in a way or the other. – Pascal Thivent Nov 22 '09 at 18:44
  • In my first scheme, the end user types only three characters: mvn. All the other typing is done by the op. – bmargulies Nov 22 '09 at 20:34
23

As of version 2.4 of the Maven Dependency Plugin, you can also define a target destination for the artifact by using the -Ddest flag. It should point to a filename (not a directory) for the destination artifact. See the parameter page for additional parameters that can be used

mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get \
    -DremoteRepositories=http://download.java.net/maven/2 \
    -Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT \
    -Ddest=c:\temp\robo-guice.jar
Eric B.
  • 23,425
  • 50
  • 169
  • 316