Download the latest artifact from an Artifactory repository

July 31, 2019

Do you know how to download the latest version of a Java Application from a JFrog Artifactory repository? This blog post gives you one solution. To work it requires at least the Artifactory Pro version. The Artifactory API enables us to search for last modified items/artifacts.

General

All examples are for the terminal. We use curl as a simple REST client. For convenience, we define some variables.

# defines your Artifactory Pro endpoint
export ARTIFACTORY_URL="https://mimacom.jfrog.io/mimacom"
# your user with admin privileges
export ARTIFACTORY_ADMIN="le-mappeur"
# your ultra secure password ;-)
export ARTIFACTORY_PASSWD="mimacom-20-year-anniversary"
# define your repository and artifact path
export REPO_URL="api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service"

We use example data. Adapt it to your circumstances.

Step 1 - Retrieve Latest Modified Artifact Information

Now we use the lastModified search for the given REPO_URL.

curl -s "$ARTIFACTORY_URL/$REPO_URL?lastModified"

We get the following JSON output:

{
  "uri" : "https://mimacom.jfrog.io/mimacom/api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar",
  "lastModified" : "2019-07-29T14:31:43.632+0000"
}

The given uri is the only information we need from this JSON response. We use jq to retrieve the URI information. We pass the -r option to return the value without any quotation like ".

curl -s "$ARTIFACTORY_URL/$REPO_URL?lastModified" | jq -r '.uri'
https://mimacom.jfrog.io/mimacom/api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar

Step 2 - Retrieve Storage Information

The link in step 1 is not the link to the Spring Boot Application! It is the storage information to the artifact. If we call the URI with curl:

curl -s https://mimacom.jfrog.io/mimacom/api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar

, we get the following output.

{
  "repo" : "libs-snapshots-local",
  "path" : "/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar",
  "created" : "2019-07-29T14:31:44.272Z",
  "createdBy" : "deployer",
  "lastModified" : "2019-07-29T14:31:43.632Z",
  "modifiedBy" : "deployer",
  "lastUpdated" : "2019-07-29T14:31:44.273Z",
  "downloadUri" : "https://mimacom.jfrog.io/mimacom/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar",
  "mimeType" : "application/java-archive",
  "size" : "30827705",
  "checksums" : {
    "sha1" : "3de28d2ca8783c2f1bb66cce47ad0c1d13440399",
    "md5" : "3984bbd5010972d925b7ad60b6d2ad79",
    "sha256" : "a69bb7e8969eed8eba7473cc5f23a1acbb88d467bbefa7e5d2b963b6dabc9106"
  },
  "originalChecksums" : {
    "sha1" : "3de28d2ca8783c2f1bb66cce47ad0c1d13440399",
    "md5" : "3984bbd5010972d925b7ad60b6d2ad79",
    "sha256" : "a69bb7e8969eed8eba7473cc5f23a1acbb88d467bbefa7e5d2b963b6dabc9106"
  },
  "uri" : "https://mimacom.jfrog.io/mimacom/api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar"
}

This response contains the downloadUri for the desired Spring Boot Application. We use jq again to obtain the only relevant information.

TARGET_URL="https://mimacom.jfrog.io/mimacom/api/storage/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar"
curl -s "$TARGET_URL" | jq -r '.downloadUri'

https://mimacom.jfrog.io/mimacom/libs-snapshots-local/com/mimacom/virus-scanning-service/1.0.0-SNAPSHOT/virus-scanning-service-1.0.0-20190729.143012-1.jar 

We have now finally the URI to download our latest version of our desired Spring Boot Application.

The Full Monty

In the previous sections, we explained in detail how to retrieve the download URL. This section uses the short form.

To test that the correct download url is returned, echo the output of curl.

echo $(curl -s $(curl -s "$ARTIFACTORY_URL/$REPO_URL?lastModified" | jq -r '.uri') | jq -r '.downloadUri' ) 

Three times is the charm. To download the Java artifact use curl three times.

curl -s $(curl -s $(curl -s "$ARTIFACTORY_URL/$REPO_URL?lastModified" | jq -r '.uri') | jq -r '.downloadUri' ) 
About the author: Vinh Nguyên

Loves to code, hike and mostly drink black coffee. Favors Apache Kafka, Elasticsearch, Java Development and 80's music.

Comments
Join us