maven-2

How can I download a specific Maven artifact in one command line?


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

Solution

  • In the 2020s and with recent versions of Maven (3.x), you should not need to worry about plugin version and repository URL anymore and be able to just do:

    mvn dependency:get -Dartifact=group-id:artefact-id:version
    

    Original answer

    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).