235

I can install an artifact by install:install-file, but how can I download an artifact?

For example:

mvn download:download-file -DgroupId=.. -DartifactId=.. -Dversion=LATEST
Lii
  • 11,553
  • 8
  • 64
  • 88
Lenik
  • 13,946
  • 17
  • 75
  • 103
  • 11
    Beware that the plugin wants 'repoUrl', despite the documentation saying 'repositoryUrl'. It can drive u crazy as it did to me! – zakmck Aug 30 '11 at 11:39
  • the docs (http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html) shows both xml params (...) and command-line user-properties (mvn ... -DrepoUrl="..."). This particular example is deprecated, so no worries; now it's now uniformly remoteRepositories (in both usages); but note that parameter "destination" is user property "-Ddest=..."; e.g.=> mvn org.apache.maven.plugins:maven-dependency-plugin:2.5.1:get -DremoteRepositories=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 12:23

14 Answers14

245

You could use the maven dependency plugin which has a nice dependency:get goal since version 2.1. No need for a pom, everything happens on the command line.

To make sure to find the dependency:get goal, you need to explicitly tell maven to use the version 2.1, i.e. you need to use the fully qualified name of the plugin, including the version:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
    -DrepoUrl=url \
    -Dartifact=groupId:artifactId:version

UPDATE: With older versions of Maven (prior to 2.1), it is possible to run dependency:get normally (without using the fully qualified name and version) by forcing your copy of maven to use a given version of a plugin.

This can be done as follows:

1. Add the following line within the <settings> element of your ~/.m2/settings.xml file:

<usePluginRegistry>true</usePluginRegistry>

2. Add the file ~/.m2/plugin-registry.xml with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<pluginRegistry xsi:schemaLocation="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0 http://maven.apache.org/xsd/plugin-registry-1.0.0.xsd"
xmlns="http://maven.apache.org/PLUGIN_REGISTRY/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <useVersion>2.1</useVersion>
      <rejectedVersions/>
    </plugin>
  </plugins>
</pluginRegistry>

But this doesn't seem to work anymore with maven 2.1/2.2. Actually, according to the Introduction to the Plugin Registry, features of the plugin-registry.xml have been redesigned (for portability) and the plugin registry is currently in a semi-dormant state within Maven 2. So I think we have to use the long name for now (when using the plugin without a pom, which is the idea behind dependency:get).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Yes, and I've just got the same resolution using dependency:get goal. mvn dependency:get -Dartifact=org.apache.archiva:archiva-webapp:LATEST:war -DrepoUrl=http://repository.sonatype.org/content/repositories/central/ The only inconvenience is that I must provide a repoUrl option. Your information really helps, I'm using mvn 2.2.1 (rdebian-1) and I didn't touch the pluginRegistry. Thanks. – Lenik Dec 14 '09 at 04:27
  • 2
    this "get" goal seems to "just work" again (no additional config/hacking required; plugin 2.5.1, mvn 3.0.4): =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 – michael Sep 10 '12 at 12:25
  • What's the difference between get and install? Don't they both fetch to your local maven repo? --edit: wait, no, I get it now; install is for *your* local deps to be copied to the local m2 repo, get fetches them from a remote. – Chris2048 Jan 03 '14 at 17:56
  • how to also get the sources jar for the specific artifact w/o pom? – ryenus Jun 29 '15 at 03:04
  • 5
    found it, just append `:jar:sources` to the **artifact**, see http://stackoverflow.com/a/31109185/537554 – ryenus Jun 29 '15 at 06:42
  • 5
    With newer versions of Maven (e.g. 3.6.0) you may still do it with just `mvn dependency:get -Dartifact=group-id:artefact-id:version` – scrutari Feb 28 '19 at 19:44
  • To which folder does it download the jar? – ankushbbbr Jul 19 '19 at 13:09
  • 1
    @Dims, you're correct. Doesn't work with Maven 3.6.3 – Debargha Roy Sep 16 '20 at 07:16
  • Is there a way to output the file path to artifact jar? – Cardinal System Oct 31 '22 at 19:38
  • In newer versions `-DrepoUrl` has been replaced by `-DremoteRepositories` – MhagnumDw Jan 25 '23 at 19:05
  • if there are mirrors setting in `settings.xml`, check there is no mirror for repo id `temp`. – Nile Mar 10 '23 at 08:09
121

With the latest version (2.8) of the Maven Dependency Plugin, downloading an artifact from the Maven Central Repository is as simple as:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]

where groupId:artifactId:version, etc. are the Maven coordinates

An example, tested with Maven 2.0.9, Maven 2.2.1, and Maven 3.0.4:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sources

(Thanks to Pascal Thivent for providing his wonderful answer in the first place. I am adding another answer, because it wouldn't fit in a comment and it would be too extensive for an edit.)

Community
  • 1
  • 1
Danilo Piazzalunga
  • 7,590
  • 5
  • 49
  • 75
  • I used this to download maven archetype. This works great if you are behind proxy and working with eclipse which fails to register to identify maven archtypes even though you have configured proxy in settings xml. – Acewin Sep 27 '16 at 18:07
  • mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.apache.maven.archetypes:maven-archetype-webapp:1.0:jar – Acewin Sep 27 '16 at 18:07
  • Can you explain what `jar:sources` means/does? why do you need to specify "jar"? and does "sources" mean it will download additional dependencies the artifact requires? – red888 Nov 12 '19 at 20:34
  • 1
    "jar" is the packaging type, "sources" is the classifier. Given one dependency (for example, [hibernate-core](https://repo1.maven.org/maven2/org/hibernate/hibernate-core/5.4.8.Final/)), there could be more than one artifact. Often, there are: 1. the binary-only library, 2. `sources`, a JAR file containing only Java sources, 3. `javadoc`, a JAR file with API docs only. See also: [Maven Default Artifact Handlers](http://maven.apache.org/ref/current/maven-core/artifact-handlers.html). – Danilo Piazzalunga Nov 12 '19 at 20:48
  • Thanks about clarifying how to use the `[:classifier]` correctly, if I have a assemlby plugin created zip file that I want to download. – jonashackt Apr 03 '20 at 12:35
  • This worked as of Maven version 3.6.3. – mbmast Jul 02 '22 at 19:40
61

Here's what worked for me to download the latest version of an artifact called "component.jar" with Maven 3.1.1 in the end (other suggestions did not, mostly due to maven version changes I believe)

This actually downloads the file and copies it into the local working directory

From bash:

mvn dependency:get \
    -DrepoUrl=http://.../ \
        -Dartifact=com.foo.something:component:LATEST:jar \
        -Dtransitive=false \
        -Ddest=component.jar \
verveguy
  • 2,103
  • 17
  • 16
  • Great, thanks! I didn't event need the `-DrepoUrl`, I guess it was implied from my pom anyway. Also I didn't use `-Ddest` as I actually *did* want to it download to the regular `.m2` directory. – yair Aug 08 '15 at 22:58
  • 25
    The `dest` parameter [is deprecated](http://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html#destination), and can be replaced with an invokation of [`copy`](http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html) after `get` like so: `mvn dependency:copy -Dartifact=com.foo.something:component:LATEST:jar -DoutputDirectory=.` (in this case placing the jar in the working directory). – Jacob Raihle Aug 08 '16 at 15:26
  • @JacobRaihle Then I cannot download file without pom.xml, getting the following error: `Goal requires a project to execute but there is no POM in this directory`. – Athlan Jun 03 '19 at 12:26
  • 5
    @Athlan No-pom support may be a newer feature, I suppose. Nowadays I can just run `mvn dependency:copy` (without `dependency:get` first). – Jacob Raihle Jun 03 '19 at 12:48
  • 1
    instead of dest parameter; i specify an alternate local repo path. this gives me all the transitive dependencies in a single place. e.g.: ´ mvn -Dmaven.repo.local=./temp_repo org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=com.jayway.jsonpath:json-path:2.5.0 ´ – chaixdev Apr 15 '21 at 14:03
  • It looks like `dependency:copy` also downloads it first, so using `dependency:get` to download explicitly is redundant. I first tried `copy` with the file already in local repo and then again after removing it from local repo and could clearly see that it got downloaded again. – haridsv Jun 30 '21 at 10:49
36

Regarding how to get the artifact binary, Pascal Thivent's answer is it, but to also get the artifact sources jar, we can use:

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

e.g.

mvn dependency:get -Dartifact=junit:junit:4.12:jar:sources

This works because the artifact parameter actually consists of groupId:artifactId:version[:packaging][:classifier]. Just the packaging and classifier are optional.

With jar as packaging and sources as classifier, the maven dependency plugin understands we're asking for the sources jar, not the artifact jar.

Unfortunately for now sources jar files cannot be downloaded transitively, which does make sense, but ideally I do believe it can also respect the option downloadSources just like the maven eclipse plugin does.

Community
  • 1
  • 1
ryenus
  • 15,711
  • 5
  • 56
  • 63
  • This works for me on mvn 3.9.1, however because I'm running in powershell I needed to quote my artifact like this ... -Dartifact="org.testng:testng:7.7.1" Otherwise I get [ERROR] Error resolving version for plugin '.testng:testng' – crowne Apr 06 '23 at 14:10
23

One could use dependency:copy (http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html) which takes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in either the local repository or the reactor.

Not all the properties of the plugin could be used in maven CLI. The properties which have "User Property:" property defined could be specified. In the below example I am downloading junit to my temp folder and stripping the vesion from the jar file.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=junit:junit:4.11 -DoutputDirectory=/tmp -Dmdep.stripVersion=true

where artifact=junit:junit:4.11 is the maven coordinates. And you specify artifcat as groupId:artifactId:version[:packaging[:classifier]]

(Thanks to Pascal Thivent for providing his https://stackoverflow.com/a/18632876/2509415 in the first place. I am adding another answer)

Community
  • 1
  • 1
aloksahoo
  • 433
  • 4
  • 6
12

The usage from the official documentation:

https://maven.apache.org/plugins/maven-dependency-plugin/usage.html#dependency:get

For my case, see the answer below:

mvn dependency:get -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false
mvn dependency:copy -Dartifact=$2:$3:$4:$5 -DremoteRepositories=$1 -Dtransitive=false -DoutputDirectory=$6

#mvn dependency:get -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false
#mvn dependency:copy -Dartifact=com.huya.mtp:hynswup:1.0.88-SNAPSHOT:jar -DremoteRepositories=http://nexus.google.com:8081/repository/maven-snapshots/ -Dtransitive=false -DoutputDirectory=.

Use the command mvn dependency:get to download the specific artifact and use the command mvn dependency:copy to copy the downloaded artifact to the destination directory -DoutputDirectory.

user2310670
  • 121
  • 1
  • 3
9

one liner to download latest maven artifact without mvn:

curl -O -J -L  "https://repository.sonatype.org/service/local/artifact/maven/content?r=central-proxy&g=io.staticcdn.sdk&a=staticcdn-sdk-standalone-optimizer&e=zip&v=LATEST"
mrduguo
  • 944
  • 8
  • 5
5

maven command: if you use maven, you can use dependency:copy to download the artifact to the local folder.

mvn dependency:copy -Dartifact=groupId:artifactId:version[:packaging[:classifier]] -DoutputDirectory=<your local path>. -U

Refer: https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html

Curl command:

# guide: https://help.sonatype.com/repomanager3/rest-and-integration-api/search-api

# https://msnexus.xxxx.com/service/rest/v1/search/assets?sort=version&repository=public&maven.groupId=<groupId>&maven.artifactId=<>&maven.baseVersion=1.46.0-SNAPSHOT&maven.extension=war

download_artifact() {
    local host_url=$1
    local group_id=$2
    local artifact_id=$3
    local artifact_type=$4
    local artifact_version=$5
    local final_name=$6
    local location=$7

    local search_version=$5
    local prerelease="false"
    if [[ "${artifact_version}" == *"SNAPSHOT" ]]; then
        prerelease="true"
    fi
    if [[ "${artifact_version}" == "latest"* ]]; then
        search_version="*"
    fi
    assets_url="${host_url}/service/rest/v1/search/assets?sort=version&repository=public&maven.groupId=${group_id}&maven.artifactId=${artifact_id}&maven.baseVersion=${search_version}&prerelease=${prerelease}&maven.extension=${artifact_type}"

    echo "INFO: Assets url: $assets_url"

    download_url=$(curl "$assets_url" -H "accept: application/json" | jq -r ".items[0].downloadUrl // empty")

    echo "INFO: Downloading artifact from url: $download_url"

    if [[ -z "$download_url" ]]; then
        echo "ERROR: Artifact not exists in Nexus, please check your version [${version}] for [${service_name}]"
        exit 1
    fi

    pre_dir=$(pwd)

    if [[ ! -d "$location" ]]; then
        mkdir -p $location
    fi

    cd $location

    curl -o "${final_name}.${artifact_type}" "$download_url"

    cd $pre_dir
}
Haber
  • 109
  • 2
  • 4
2

Here's an example to get ASM-7 using Maven 3.6:

mvn dependency:get -DremoteRepositories=maven.apache.org -Dartifact=org.ow2.asm:7.0:sources:jar

Or you can download the jar from here: https://search.maven.org/search?q=g:org.ow2.asm%20AND%20a:asm and then

mvn install:install-file -DgroupId=org.ow2.asm -DartifactId=asm -Dversion=7.0 -Dclassifier=sources -Dpackaging=jar -Dfile=/path/to/asm-7.0.jar
Adobe
  • 12,967
  • 10
  • 85
  • 126
2

To copy artifact in specified location use copy instead of get.

mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:copy \
  -DrepoUrl=someRepositoryUrl \
  -Dartifact="com.acme:foo:RELEASE:jar" -Dmdep.stripVersion -DoutputDirectory=/tmp/
MariuszS
  • 30,646
  • 12
  • 114
  • 155
2

You can also do this using docker in PowerShell:

docker run -it --rm -v ${PWD}:/build/source -v ${HOME}/.m2:/build/.m2 --net=host aemdesign/centos-java-buildpack:jdk8 /bin/bash --login -c 'mvn dependency:get -Dmaven.repo.local=/build/.m2/repository -DrepoUrl=https://repo1.maven.org/maven2 -Dartifact=io.prometheus.jmx:jmx_prometheus_javaagent:LATEST -Ddest=/build/source/jmx_prometheus_javaagent.jar'

or in bash:

docker run -it --rm -v $PWD:/build/source -v $HOME/.m2:/build/.m2 --net=host aemdesign/centos-java-buildpack:jdk8 /bin/bash --login -c 'mvn dependency:get -Dmaven.repo.local=/build/.m2/repository -DrepoUrl=https://repo1.maven.org/maven2 -Dartifact=io.prometheus.jmx:jmx_prometheus_javaagent:LATEST -Ddest=/build/source/jmx_prometheus_javaagent.jar'
Max Barrass
  • 2,776
  • 1
  • 19
  • 10
0

The command:

mvn install:install-file 

Typically installs the artifact in your local repository, so you shouldn't need to download it. However, if you want to share your artifact with others, you will need to deploy the artifact to a central repository see the deploy plugin for more details.

Additionally adding a dependency to your POM will automatically fetch any third-party artifacts you need when you build your project. I.e. This will download the artifact from the central repository.

Clinton
  • 3,638
  • 3
  • 26
  • 33
0

LATEST is deprecated, try with range [,)

./mvnw org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \  
-DremoteRepositories=repoId::default::https://nexus/repository/maven-releases/ \
"-Dartifact=com.acme:foo:[,)"
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • Have you tried it? This will not work: *maven-dependency-plugin:get* do NOT support version ranges. Way I made version rages work was like this https://stackoverflow.com/a/73801763/603314 – bugs_ Jan 23 '23 at 12:40
0

Unfortunately maven-dependency-plugin:get do NOT support version ranges e.g. [2.17.1,) or [,)

If you need download a specific maven artifact but using version range, as I do, look here:

Download Maven artifact with version range

bugs_
  • 3,544
  • 4
  • 34
  • 39