How to get a jar file from nexus using Jenkins?

Tags: , , , ,



I have a Jenkins job which contains 3 parameters: ARTIFACT_ID, GROUP_ID, and APP_VERSION. I would like to use those 3 params to download any specific jar file from Nexus.

Note that the Nexus URL is provided in the Jenkins config, so it will automatically know from where to lookup.

Do you have an idea how to do that!!

For example, for python apps, I use pip download --no-deps ARTIFACT_ID==APP_VERSION and it works fine.

Answer

You can use a shell script using curl and xmllint to access the Nexus api and download an artifact.

Here an example how I retrieve artifacts from Nexus:

NEXUS_BASE_URL=https://nexus.example.com
REPOSITORY="reponame"
GROUP_ID="groupid"
ARTIFACT_ID="artifact_id"
LOCAL_FILE="destination.jar"

NEXUS_RESOLVE_URL="${NEXUS_BASE_URL}artifact/maven/resolve?g=${GROUP_ID}a=${ARTIFACT_ID}&r=${REPOSITORY}&v=${VERSION}"
REPOSITORY_LOCAL_PATH=`curl -s "${NEXUS_RESOLVE_URL}" | xmllint --xpath "//artifact-resolution/data/repositoryPath/text()" -`
ARTIFACT_DOWNLOAD_URL="${NEXUS_BASE_URL}repositories/${REPOSITORY}/content${REPOSITORY_LOCAL_PATH}"
curl -o "${LOCAL_FILE}" "${ARTIFACT_DOWNLOAD_URL}"

Further information about resolving atrifacts can be found in the nexus api reference https://repository.sonatype.org/nexus-restlet1x-plugin/default/docs/path__artifact_maven_resolve.html



Source: stackoverflow