While Maven is primarily a Java tool, Python developers often need to automate the retrieval of JARs, POMs, or other assets for CI/CD pipelines, container building, or cross-platform integration. You can achieve this using specialized libraries like maven-artifact or by making direct HTTP requests to repositories like Maven Central . Option 1: Using the maven-artifact Library
Option 3: Working with Private Repositories (Nexus/Artifactory)
This library allows you to define a repository and resolve specific coordinates (GroupId, ArtifactId, Version). python download maven artifact
The maven-artifact library is a dedicated tool for resolving and downloading Maven artifacts directly from Python. pip install maven-artifact Use code with caution.
from maven_artifact import ArtifactRequest, MavenDownloader # Define the artifact you want artifact = ArtifactRequest("org.apache.commons", "commons-lang3", "3.12.0") # Initialize downloader (defaults to Maven Central) downloader = MavenDownloader() # Download to a specific directory path = downloader.download(artifact, dest_dir="./libs") print(f"Downloaded to: {path}") Use code with caution. Option 2: Manual Download via HTTP Requests While Maven is primarily a Java tool, Python
Maven repositories follow a standardized URL structure, making them easy to query using the Python Requests library without needing external Maven tools.
import requests import os def download_maven_artifact(group_id, artifact_id, version, extension="jar"): base_url = "https://repo1.maven.org/maven2/" group_path = group_id.replace(".", "/") filename = f"{artifact_id}-{version}.{extension}" url = f"{base_url}{group_path}/{artifact_id}/{version}/{filename}" response = requests.get(url, stream=True) if response.status_code == 200: with open(filename, "wb") as f: f.write(response.content) print(f"Successfully downloaded {filename}") else: print(f"Failed to download. Status: {response.status_code}") # Example: Download JUnit download_maven_artifact("junit", "junit", "4.13.2") Use code with caution. The maven-artifact library is a dedicated tool for
The path is constructed as: base_url / group_id_slashes / artifact_id / version / artifact_id-version.extension . Implementation Example:
If you are working with internal repositories like JFrog Artifactory or Sonatype Nexus , you must handle authentication.
# Downloading from a private Nexus repository repo_url = "https://your-nexus-server.com" auth = ("username", "password") response = requests.get(full_artifact_url, auth=auth) Use code with caution. Summary of Approaches Complex resolution Handles version resolution and metadata. requests library Simple, standalone scripts No specialized dependencies required. Subprocess + mvn Environments with Maven Reliable but requires Maven installed. PyPIhttps://pypi.org maven-artifact - PyPI