Set Up Artifactory to Publish Bazel Artifacts

ARTIFACTORY: Integrate Artifactory with Bazel

AuthorFullName__c
Michael Akushky
articleNumber
000005942
FirstPublishedDate
2023-12-25T13:59:44Z
lastModifiedDate
2025-05-14
VersionNumber
4
See the following example of how to configure the rules_jvm_external ruleset in a BUILD file to publish Java dependencies to a Bazel project:

Make sure to replace the placeholders in bold with your rule name, artifact name and versions for the artifacts you’d like to resolve, app name, and app class. See the example below for more details.
# user_project/BUILD
load("@rules_jvm_external//:defs.bzl", "java_export")

java_export(
  name = "<APP_NAME>",
  maven_coordinates = "com.example:project:0.0.1",
  pom_template = "pom.tmpl",  # You can omit this
  srcs = glob(["*.java"]),
  deps = [
    "//user_project/utils",
    "@maven//:com_google_guava_guava",
  ],
)

For example:
# user_project/BUILD
load("@rules_jvm_external//:defs.bzl", "java_export")

java_export(
  name = "exported_lib",
  maven_coordinates = "com.example:project:0.0.1",
  pom_template = "pom.tmpl",  # You can omit this
  srcs = glob(["*.java"]),
  deps = [
    "//user_project/utils",
    "@maven//:com_google_guava_guava",
  ],
)

This example ruleset will:
1. Load the java_export rule from the rules_jvm_external package.
load("@rules_jvm_external//:defs.bzl", "java-export")

2. Create a java_export target called exported-lib. This is similar to a regular java_library parameter, but it allows two additional parameters to set Maven coordinates and add an optional template for the pom.xml file.
java_export(
  name = "exported_lib",

3. Set the target Maven coordinates.
Maven_coordinates = "com.example:project:0.0.1",
4. (Optional) Add a template file to use for the pom.xml file.
pom_template = "pom.tmpl",  # You can omit this
5. Finally, set the sources and dependencies for the build process. In this case, we set the ruleset to use files ending with .java as sources, and to use a dependency called guava from Google to publish the build results: 
srcs = glob(["*.java"]),
  deps = [
    "//user_project/utils",
    "@maven//:com_google_guava_guava",
  ],
)
After setting the java_export rule, run the following command to publish the artifact to Artifactory local Maven repository:

When setting gpg-sign to true, the current default key will be used for signing: make sure that the gpg binary is installed on your machine before running this command.

Make sure to replace the placeholders in bold with your Maven username and password, your local repository path, and export rule name.
MAVEN_USER=<USERNAME> MAVEN_PASSWORD=<TOKEN> bazel run --stamp \
  --define "maven_repo=<JFROG_HOST_DOMAIN>/artifactory/<REPOSITORY_NAME>" \
  --define gpg_sign=true \
  //user_project:<EXPORT_RULE_NAME>.publish`

For example:
MAVEN_USER=admin MAVEN_PASSWORD=password bazel run --stamp \
  --define "maven_repo=https://acme.jfrog.io/artifactory/maven_local" \
  --define gpg_sign=true \
  //user_project:exported_lib.publish`