Keywords: Maven | Dependency Plugin | Artifact Download | Command Line Tool | Maven Coordinates
Abstract: This article provides a comprehensive guide on using Maven Dependency Plugin's dependency:get goal to download specific artifacts directly from the command line without POM files. It covers simplified commands for modern Maven versions, compatibility handling for historical versions, plugin version management mechanisms, and advanced usage of coordinate parameters. By comparing implementation differences across Maven versions, it helps developers master best practices for efficient artifact downloading.
Basic Usage of Maven Dependency Plugin
In modern Maven environments (3.x versions), using the dependency:get goal to download artifacts has become remarkably simple. Developers need only execute the following command:
mvn dependency:get -Dartifact=group-id:artefact-id:versionThis command automatically downloads the specified artifact from configured Maven repositories and saves it to the appropriate location in the local repository. The entire process completes entirely from the command line, requiring no POM file support.
Historical Version Compatibility Handling
In Maven 2.1 and earlier versions, explicitly specifying the plugin version was necessary to ensure availability of the dependency:get goal. In such cases, the fully qualified plugin name should be used:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \n -DrepoUrl=url \n -Dartifact=groupId:artifactId:versionThe -DrepoUrl parameter specifies custom repository URLs, particularly useful when downloading artifacts from repositories other than Maven Central.
Plugin Version Management Mechanism
Maven provides a plugin registry mechanism for managing plugin versions. Enable the plugin registry in ~/.m2/settings.xml:
<usePluginRegistry>true</usePluginRegistry>Then create the ~/.m2/plugin-registry.xml file:
<?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>It's important to note that this mechanism entered a semi-dormant state in Maven 2.1/2.2, with modern versions preferring fully qualified names or dependency management configurations.
Advanced Coordinate Parameter Usage
The dependency:get goal supports complete Maven coordinate formats, including optional packaging types and classifiers:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]For example, to download the source JAR for Hibernate Entity Manager version 3.4.0.GA:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.hibernate:hibernate-entitymanager:3.4.0.GA:jar:sourcesThis format flexibility enables developers to precisely control the type and variant of artifacts being downloaded.
Modern Best Practices
For Maven 3.x users, the simplified command format is recommended, allowing Maven to automatically handle plugin versions and repository configurations. When artifacts need to be downloaded to specific locations rather than the local repository, the -Ddest parameter can be combined:
mvn dependency:get \n -Dartifact=com.foo.something:component:LATEST:jar \n -Dtransitive=false \n -Ddest=component.jarHere, -Dtransitive=false ensures only the specified artifact is downloaded without resolving transitive dependencies, while -Ddest specifies the target path for the downloaded file.
Conclusion and Recommendations
The Maven Dependency Plugin's dependency:get goal provides powerful and flexible support for command-line artifact downloading. Modern Maven versions have significantly simplified the usage process, and developers should prioritize using simplified command formats. For special requirements, such as specific plugin versions or custom download locations, fine-grained control can be achieved through corresponding parameters. Understanding complete Maven coordinate formats and plugin management mechanisms will help developers efficiently complete artifact downloading tasks across various scenarios.