Groovy Download [patched] File From Nexus -
The most modern and readable way to handle downloads in Groovy is through the HTTPBuilder-NG library. It simplifies credential handling and file streaming. 📦 Prerequisites Add the dependency to your script using @Grab :
def nexusUrl = "https://example.com" def filePath = "com/example/app/1.0.0/app-1.0.0.jar" def destination = "downloads/app.jar" def user = "admin" def pass = "your-password" configure { request.uri = nexusUrl request.auth.basic user, pass }.get { request.uri.path = filePath response.success { FromServer from, Object body -> Paths.get(destination).withOutputStream { out -> from.inputStream.transferTo(out) } println "✅ Download complete: ${destination}" } response.failure { FromServer from -> println "❌ Failed to download: ${from.statusCode}" } } Use code with caution. 🛠️ Alternative: Using Native Java/Groovy (No Deps)
Avoid hardcoding your main password. Use Nexus or API Keys if your organization allows them. 2. Handle Snapshots groovy download file from nexus
In a Jenkinsfile, you can use the httpRequest step, but if you need complex logic, a Groovy script block is better:
If you cannot use external libraries, you can use the built-in URL and URLConnection classes. This is slightly more "wordy" but works out of the box in any Groovy environment (like Jenkins). The "One-Liner" Approach For public repositories or simple setups: The most modern and readable way to handle
Note: Using curl via sh is often the most reliable method in Linux-based CI environments as it handles large files and retries natively. 💡 Let me know: Are you using Jenkins , Gradle , or a standalone script ?
This script authenticates with Nexus, fetches the file, and saves it locally. Handle Snapshots In a Jenkinsfile, you can use
@Grab('org.io7m.httpbuilder-ng:httpbuilder-ng-core:1.0.4') import static groovyx.net.http.HttpBuilder.configure import java.nio.file.Paths Use code with caution. 💻 The Script