import java.net.Authenticator import java.net.PasswordAuthentication // Configuration def nexusUrl = "http://your-nexus-server:8081/repository/maven-releases/com/example/my-app/1.0.0/my-app-1.0.0.jar" def destination = new File("my-app.jar") def user = "admin" def pass = "admin123" // Setup Authentication Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, pass.toCharArray()) } }) // Download File println "Downloading ${nexusUrl}..." destination.withOutputStream { out -> out << new URL(nexusUrl).openStream() } println "Download complete: ${destination.absolutePath}" Use code with caution. Method 2: Jenkins Pipeline Groovy Script

def response = httpRequest authentication: 'nexus-creds', url: "https://example.com", outputFile: "artifact.zip" Use code with caution. Key Considerations Download an artifact from Nexus to Jenkins

Downloading artifacts from Sonatype Nexus using Groovy is a common requirement in CI/CD pipelines, particularly for Jenkins environments. While Nexus 3 has moved toward a primary REST API, Groovy remains a powerful tool for automating these downloads through direct HTTP requests or specialized Jenkins plugins. Choosing Your Method

For a standalone script, you can use Groovy's built-in networking capabilities to hit the Nexus Download URI .

There are two primary ways to download an artifact using Groovy:

Register for Technext Coinference 2023, the Largest blockchain and DeFi Gathering in Africa.

groovy script to download artifact from nexus

Technext Newsletter

Get the best of Africa’s daily tech to your inbox – first thing every morning.
Join the community now!