Programming

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

19 September 2026 · 9 min read

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

Managing dependencies is a crucial part of software development, and Maven simplifies this process for Java projects. Sometimes, you need to download a specific Maven artifact in one command line, whether it’s for testing purposes, examining its contents, or integrating it into a different build system. Learning how to do this efficiently can save you time and streamline your workflow. We’ll explore different approaches using the Maven command-line interface (CLI), offering practical solutions and examples that you can readily apply. This guide will cover various techniques and provide insights into using Maven effectively for artifact retrieval, ensuring you can quickly access the libraries you need for your projects. Mastering this skill will empower you to manage dependencies with greater control and flexibility.

Understanding Maven Artifacts and Repositories

Before diving into the command-line instructions, it’s important to understand what Maven artifacts are and how they are stored. A Maven artifact is essentially a package containing code, resources, and metadata. Typically, it’s a JAR file, but it can also be a WAR, POM, or any other type of packaged resource. Each artifact is uniquely identified by its Group ID, Artifact ID, and Version (GAV coordinates). These coordinates are essential for locating and downloading the correct artifact from a Maven repository.

Maven repositories serve as storage locations for these artifacts. The central Maven repository (https://repo1.maven.org/maven2/) is the default public repository where a vast collection of open-source libraries is hosted. However, organizations often set up their own internal repositories (such as Nexus, Artifactory, or Archiva) to manage proprietary artifacts or cache dependencies for faster access and better control. Understanding the structure and hierarchy of these repositories is key to efficiently retrieving artifacts using the command line. The local repository (usually located in your user’s home directory under .m2/repository) caches downloaded artifacts for reuse across projects, reducing the need to repeatedly download the same dependencies.

Knowing how Maven resolves dependencies can significantly improve your troubleshooting skills when encountering issues with artifact downloads. When Maven needs an artifact, it first checks the local repository. If the artifact isn’t found locally, Maven consults the configured remote repositories, downloads the artifact, and stores it in the local repository. This process ensures that you always have access to the required dependencies, whether you’re online or offline.

Downloading Artifacts with the mvn dependency:get Goal

The mvn dependency:get goal from the Maven Dependency Plugin is a straightforward way to download a specific Maven artifact in one command line. This goal allows you to specify the GAV coordinates directly in the command, making it quick and easy to retrieve the desired artifact. It’s a simple, direct approach that avoids the need to create a full Maven project just to download a single dependency.

Here’s how to use the mvn dependency:get goal:

  1. Open your terminal or command prompt.
  2. Execute the following command, replacing the placeholders with the actual GAV coordinates: ``` mvn dependency:get -DgroupId=your.group.id -DartifactId=your-artifact-id -Dversion=1.0.0
  3. By default, the artifact will be downloaded to your local repository. To specify a different output directory, use the -DoutputDirectory parameter: ``` mvn dependency:get -DgroupId=your.group.id -DartifactId=your-artifact-id -Dversion=1.0.0 -DoutputDirectory=/path/to/your/output/directory

For example, to download version 3.2.0 of the Apache Commons Lang library, you would use the following command:

mvn dependency:get -DgroupId=org.apache.commons -DartifactId=commons-lang3 -Dversion=3.2.0

This command will download the commons-lang3-3.2.0.jar file to your local Maven repository. Using the -DoutputDirectory option allows you to place the downloaded JAR file in a location of your choosing, outside of the default Maven repository structure. This is particularly useful when you need to integrate the artifact into a non-Maven project or perform specific analysis on the JAR file. Using the mvn dependency:copy Goal

Another useful approach is the mvn dependency:copy goal. While similar to dependency:get, dependency:copy is designed to copy artifacts from the local repository to a specified location. This can be helpful if you’ve already resolved the dependency in a Maven project and want to extract it for other purposes. The primary use case is when the artifact is already present in the local repository, and you need to move it somewhere else.

To use the mvn dependency:copy goal effectively, consider these steps:

  1. Ensure the artifact is already in your local Maven repository. You can achieve this by including the dependency in a Maven project and running mvn compile.
  2. Use the following command to copy the artifact: ``` mvn dependency:copy -Dartifact=your.group.id:your-artifact-id:1.0.0 -DoutputDirectory=/path/to/your/output/directory -Dmdep.stripClassifier=false -Dmdep.useBaseVersion=true
  3. Replace your.group.id, your-artifact-id, and 1.0.0 with the actual GAV coordinates of the artifact you want to copy. The -DoutputDirectory specifies the destination directory, and -Dmdep.stripClassifier=false -Dmdep.useBaseVersion=true ensures the full version and classifier are included in the copied file’s name.

For example, if you want to copy the commons-codec artifact version 1.15 to a directory named /tmp/libs, the command would look like this:

mvn dependency:copy -Dartifact=commons-codec:commons-codec:1.15 -DoutputDirectory=/tmp/libs -Dmdep.stripClassifier=false -Dmdep.useBaseVersion=true

This command copies the commons-codec-1.15.jar file from your local repository to the /tmp/libs directory. The dependency:copy goal is particularly useful in scenarios where you need to package dependencies along with your application for deployment or distribution. Alternatives: Using wget or curl

While Maven provides built-in tools for downloading artifacts, you can also use standard command-line utilities like wget or curl to download a specific Maven artifact in one command line. This approach requires you to know the exact URL of the artifact in a Maven repository. It’s a more manual process, but it can be useful if you don’t have Maven installed or if you need to automate artifact retrieval in a scripting environment.

Here’s how to use wget:

wget https://repo1.maven.org/maven2/your/group/id/your-artifact-id/1.0.0/your-artifact-id-1.0.0.jar

And here’s how to use curl:

curl -O https://repo1.maven.org/maven2/your/group/id/your-artifact-id/1.0.0/your-artifact-id-1.0.0.jar

Replace your/group/id, your-artifact-id, and 1.0.0 with the appropriate values. For example, to download the JUnit artifact, you would use:

wget https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar

or

curl -O https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar

The -O option in curl tells it to save the downloaded file with the same name as it has on the server. This method is effective when you know the exact location of the artifact in the repository. However, it requires manual construction of the URL, which can be error-prone. For more complex scenarios, using Maven’s built-in tools is generally recommended.

Best Practices and Troubleshooting

When working with Maven and downloading artifacts, following best practices can save you time and prevent common issues. One key practice is to always specify the version of the artifact you want to download. Omitting the version can lead to unpredictable results, as Maven might download the latest version, which may not be compatible with your project. Always double-check the GAV coordinates to ensure they are correct before executing the command. Typographical errors in the Group ID, Artifact ID, or Version can lead to Maven failing to find the artifact.

Here’s a list of best practices:

  • Always specify the version when downloading artifacts.
  • Verify the GAV coordinates before executing the command.
  • Use the -DoutputDirectory parameter to control where the artifact is downloaded.

Troubleshooting common issues can also be streamlined. If you encounter errors like “Artifact not found,” first verify that the artifact exists in the specified repository and that the GAV coordinates are correct. Check your Maven settings file (settings.xml) to ensure that the correct repositories are configured. If you’re behind a proxy, make sure your proxy settings are correctly configured in Maven. According to a Stack Overflow survey, incorrect proxy settings are a common cause of Maven download failures. You can also try updating your Maven installation to the latest version, as newer versions often include bug fixes and performance improvements.

Here’s a summary of troubleshooting tips:

  • Verify artifact existence and GAV coordinates.
  • Check Maven settings for correct repository and proxy configurations.
  • Update Maven to the latest version.

Featured Snippet: To download a specific Maven artifact in one command line using the Maven Dependency Plugin, use the mvn dependency:get goal. Specify the Group ID, Artifact ID, and Version (GAV coordinates) using the -DgroupId, -DartifactId, and -Dversion parameters. For example: mvn dependency:get -DgroupId=org.apache.commons -DartifactId=commons-lang3 -Dversion=3.2.0. This command downloads the specified artifact to your local Maven repository.

Infographic here
FAQ ---
How do I find the GAV coordinates of a Maven artifact?
You can find the GAV coordinates on Maven Central () or in the documentation of the library you are using. The coordinates are typically listed as Group ID, Artifact ID, and Version.
Can I download multiple artifacts with one command?
No, the mvn dependency:get goal is designed to download one artifact at a time. To download multiple artifacts, you would need to execute the command multiple times or use a script to automate the process. However, you can achieve this by creating a simple pom.xml file and using the dependencies to download all artifacts at once. This is the best option for downloading multiple artifacts using Maven.
What if the artifact is not in Maven Central?
If the artifact is not in Maven Central, you need to configure the appropriate repository in your Maven settings file (settings.xml). This file is located in your .m2 directory. Add a <repository> section with the ID, name, and URL of the repository.
Mastering the art of dependency management is essential for efficient software development. You’ve learned several methods to **download a specific Maven artifact in one command line**, each with its strengths and use cases. Whether you choose the simplicity of mvn dependency:get, the targeted approach of mvn dependency:copy, or the manual control of wget or curl, you now have the tools to retrieve artifacts quickly and effectively. Don't hesitate to experiment with these techniques and adapt them to your specific needs. Remember to always verify your coordinates and configurations to avoid common pitfalls.

Ready to take your Maven skills to the next level? Explore other powerful Maven plugins and goals to streamline your build process. Learn about managing dependencies effectively in multi-module projects, or dive deeper into advanced repository configurations. You can also find more helpful articles on our site to expand your knowledge and improve your development workflow.

Question & Answer :
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 

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