Set Up Artifactory to Resolve Bazel Dependencies

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 define and use external Java dependencies in 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.
load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    name = "<RULE_NAME>",
    artifacts = [
        "<ARTIFACT_NAME>:<ARTIFACT_VERSION>",
        "<ARTIFACT_NAME>:<ARTIFACT_VERSION>"
        # Add more dependencies as needed
    ],
    repositories = [
      # Private repositories are supported through HTTP Basic auth   
      "<JFROG_HOST_DOMAIN>/artifactory/<REPOSITORY_NAME>",
    ],
)

java_binary(
    name = "<APP_NAME>",
    main_class = "<APP_CLASS>",
    runtime_deps = [":<RULE_NAME>"],
    # Add other configuration as needed
)

For example:
load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    name = "my_dependencies",
    artifacts = [
        "com.google.guava:guava:28.2-jre",
        "org.slf4j:slf4j-api:1.7.30",
        "junit:junit:4.13.2",
    ],
    repositories = [
      # Private repositories are supported through HTTP Basic auth   
      "http://username:password@localhost:8081/artifactory/maven-remote",
    ],
)

java_binary(
    name = "my_app",
    main_class = "com.example.MyApp",
    runtime_deps = [":my_dependencies"],
    # Add other configuration as needed
)

This example ruleset will do the following:
1. Load the maven_install rule from the rules_jvm_external package.
load("@rules_jvm_external//:defs.bzl", "maven_install")
2. Define a maven_install rule named my_dependencies which specifies the external Java dependencies you want to use. You can list the artifacts you need, along with their group, name, and version. In this example, we've included dependencies like Guava, SLF4J, and JUnit.
maven_install(
    name = "my_dependencies",
    artifacts = [
        "com.google.guava:guava:28.2-jre",
        "org.slf4j:slf4j-api:1.7.30",
        "junit:junit:4.13.2",
        # Add more dependencies as needed
    ],

3. Set the repositories attribute to specify the remote repositories from which Bazel should fetch these dependencies. In this case, we've included the Artifactory Maven repository.
repositories = [
      # Private repositories are supported through HTTP Basic auth   
      "http://username:password@localhost:8081/artifactory/maven-remote",
    ],

4. Define a java_binary target called my_app that depends on the my_dependencies rule. This target represents your Java application and can be configured with additional settings, such as the main class and any runtime dependencies.
java_binary(
    name = "my_app",
    main_class = "com.example.MyApp",
    runtime_deps = [":my_dependencies"],
    # Add other configuration as needed
)