Build Maven Project with Jfrog CLI

Jfrog Artifactory is a popular binary repository manager. There are three ways to publish an artifact to Artifactory. Use a CLI tool (npm, nuget etc) of a package manager, use web UI in Artifactory, use REST API. Jfrog has its own CLI tool called jfrog CLI which is a wrapper to REST API.

Here is how Jfrog CLI has been defined in the official website.

JFrog CLI is a compact and smart client that provides a simple interface that automates access to JFrog products simplifying your automation scripts and making them more readable and easier to maintain.

Let’s go through basics of using jfrog CLI to build and publish a maven project to artifactory in a unix based system.

First step is to download it and add it to the executable path. Or you can download it directly to the parent directory of the maven Project.

curl -fL https://getcli.jfrog.io | sh

Then configure CLI with artfiactory server. Artifactory server id is a unique id to identify the artifactory instance.

./jfrog rt config <artifactory_server_id> --url=<artifactory_url> --user=<user> --password=<password>

To see the configuration run below command

./jfrog rt config show

Next add a configuration.yml file to maven project root directory to define artifactory repositories to resolve the dependencies and publish build artifacts to. (If you have not setup default maven repositories in your artifactory instance, please follow the instructions here)

version: 1
type: maven
resolver:
  snapshotRepo: libs-snapshot
  releaseRepo: libs-release
  serverID: <artifactory_server_id>
deployer:
  snapshotRepo: libs-snapshot-local
  releaseRepo: libs-release-local
  serverID: <artifactory_server_id>

Finally run the maven build command

#You can define build_name and build_number as you desire  
./jfrog rt mvn 'clean install' configuration.yml --build-name=<build_name> --build-number=<build_number>

#Once the build is done. Run the below command to publish the artifacts to artifactory
./jfrog rt build-publish <build_name> <build_number>

We can put the above commands in a build script and setup a CI pipeline. Example project can be found here. In the example project, build environment information as well as git repository information are published alongside with the build artifacts.