Bazel Modules Repositories

Create, connect, publish, and resolve Bazel modules in Artifactory.

Bazel is an open-source build tool from Google. Bazel supports remote caching, working as a solution in JFrog Artifactory for storing build artifacts, build metadata, and dependency resolution. You can use Bazel repositories in Artifactory to cache and proxy the public Bazel Central Registry (BCR), store your own modules in a local repository, and aggregate local and remote repositories in a virtual repository. This makes sure that you pull approved and vetted dependencies. It enhances supply chain security and guarantees consistent builds. You can integrate Bazel with Artifactory for streamlined software development and robust artifact management. For more information, see the Bazel documentation.

package_management_images_for_Bazel.png

Artifactory supports the following capabilities for Bazel Module repositories:

  • Security and Authentication: You can authenticate to your Bazel repository in Artifactory for secure access to your proxy and cache of the BCR.
  • CLI Support: You can use the bazel CLI to natively manage modules, relying on the MODULE.bazel file to resolve dependencies from the Artifactory repository.
  • Local repositories: You can store your own Bazel modules and resolve them with the Bazel client.
  • Virtual repositories: You can aggregate local and remote Bazel Modules repositories so the client uses one registry.
  • Bazel 9 Readiness: Bazel repositories in Artifactory support the Bazel 9 requirement to remove the legacy WORKSPACE system and use Bazel modules, bzlmod, for dependency management.
📘

Note

Bazel Modules repositories can proxy the Bazel Central Registry, store your own modules in a local repository, or aggregate local and remote repositories in a virtual repository. You can use Generic and Maven repositories in Artifactory for build artifact storage and build dependencies in your Bazel projects.

For more information, see Additional Bazel Information.

Get Started with Bazel Modules

To get started working with Bazel Module repositories in Artifactory, complete the following main steps:

  1. Create a Bazel Modules Repository
  2. Connect Bazel to Artifactory
  3. Publish a Bazel Module and Resolve Bazel Modules

Create a Bazel Modules Repository

This topic describes how to create a Bazel Modules repository. You need this repository before you resolve Bazel modules. You can create a local repository for your own modules, a remote repository to cache the Bazel Central Registry, or a virtual repository that aggregates local and remote repositories.

For more information on JFrog repositories, see Repository Management.

Prerequisite: You need Admin or Project Admin permissions in Artifactory to create a repository.

To create a Bazel Modules repository:

  1. In the Administration tab, click Repositories > Create a Repository.

    create_remote.png
  2. Select Local, Remote, or Virtual.

  3. Select the Bazel Modules package type.

  4. In the Repository Key field, enter a meaningful name for the repository. For example, bazel-local.

  5. For a remote repository, verify the Repository URL and update it as needed. For more information, see Remote Repositories.

  6. For a virtual repository, select the local and remote Bazel Modules repositories to include.

  7. Click Create Repository. The repository is created.

Connect Bazel to Artifactory

To use Artifactory as the registry for your Bazel client, complete the following tasks:

Prerequisite: Before connecting the Bazel client to Artifactory, you must have an existing Bazel Modules repository in Artifactory. For more information, see Create a Bazel Modules Repository.

Authenticate Bazel to Artifactory

Before using your Bazel client with Artifactory, you need to add Artifactory authentication information to your netrc file. Bazel uses these credentials to authenticate to your Artifactory repository.

To authenticate Bazel to Artifactory:

  1. Open the netrc file in a text editor. The file location varies by operating system. For example:

    • Linux, Unix, and macOS: ~/.netrc
    • Windows: %USERPROFILE%\_netrc
  2. In the netrc file, add the following snippet:

    machine <JFrogPlatformURL>
    login <USERNAME>
    password <AUTH>

    Where:

    • <JFrogPlatformURL>: Your organization's Artifactory URL
    • <USERNAME>: Your Artifactory username
    • <AUTH>: Your Artifactory identity token

    For example:

    machine company.jfrog.io
    login jeffry
    password <Token>
  3. Save the changes to your file.

📘

Note

You can also use JFrog Set Me Up to copy the snippet populated with your token and environment. For more information, see Use Artifactory Set Me Up for Configuring Package Manager Clients.

Add Artifactory Registry to Bazel

After adding Artifactory credentials to your netrc file, you need to configure your Bazel client with the repository information.

To add the Artifactory registry to Bazel:

  1. Open or create the .bazelrc file. If the file already exists, its location varies by operating system. For example:

    • Linux, Unix, and macOS: ~/etc/bazel.bazelrc
    • Windows: %ProgramData%\bazel.bazelrc
  2. In the .bazelrc file, add the following snippet:

    <COMMAND> --registry=https://<JFrogPlatformURL>/artifactory/api/bazelmodules/<REPO_NAME>

    Where:

    • <COMMAND>: The command you want to configure to work with the Artifactory repository. Options include:

      • common: Set all commands to resolve from Artifactory.
      • build: Set build commands to resolve from Artifactory.
      • fetch: Set fetch commands to resolve from Artifactory.
      • test: Set test commands to resolve from Artifactory.
    • <JFrogPlatformURL>: Your organization's Artifactory URL

    • <REPO_NAME>: The name of your Bazel Modules repository in Artifactory

    For example:

    common --registry=https://company.jfrog.io/artifactory/api/bazelmodules/bazel-remote
  3. Save the changes to the file.

📘

Note

You can also use JFrog Set Me Up to copy the snippet populated with your token and environment. For more information, see Use Artifactory Set Me Up for Configuring Package Manager Clients.

Next steps:

Publish a Bazel Module

You publish your own Bazel modules to Artifactory with the JFrog CLI. Artifactory stores each version in the Bazel Central Registry layout, modules/<MODULE_NAME>/<VERSION>/. Bazel uses that path to find a module by name and version. When you run the upload from the folder that contains your module directories, the CLI preserves this layout.

Prerequisite: Configure the JFrog CLI to connect to your Artifactory instance. For more information, see Configuring the CLI.

To upload modules:

  1. Upload two files for each version: the module archive and MODULE.bazel. Artifactory doesn't extract MODULE.bazel from the archive, so upload that file on its own. Leave source.json out of the upload. After both files are uploaded, Artifactory generates source.json from the archive, and Bazel can resolve the module.

  2. Run one of these commands from the folder that contains the module directories. You can upload to a virtual repository only when it has a default deployment repository configured.

    • To upload all modules and versions:

      jf rt upload "*" <REPO_NAME>/modules/ --flat=false

      Where:

      • <REPO_NAME>: The local or virtual repository key

      For example:

      jf rt upload "*" bazel-local/modules/ --flat=false
    • To upload all versions of one module:

      jf rt upload "<MODULE_NAME>/*" <REPO_NAME>/modules/ --flat=false

      Where:

      • <MODULE_NAME>: The name of the module
      • <REPO_NAME>: The local or virtual repository key

      For example:

      jf rt upload "my-module/*" bazel-local/modules/ --flat=false
    • To upload one version of a module:

      jf rt upload "<MODULE_NAME>/<VERSION>/*" <REPO_NAME>/modules/ --flat=false

      Where:

      • <MODULE_NAME>: The name of the module
      • <VERSION>: The module version
      • <REPO_NAME>: The local or virtual repository key

      For example:

      jf rt upload "my-module/1.0.0/*" bazel-local/modules/ --flat=false

Resolve Bazel Modules

After you configure Bazel to connect to the secure Artifactory cache, you can resolve Bazel module dependencies from your Artifactory repository. Use a virtual repository for the registry URL so one URL resolves both your private modules and public modules from the Bazel Central Registry. You can resolve modules in the following ways:

Resolve Modules from Default Registry

Bazel commands automatically resolve modules from the registry you configure in the .bazelrc file.

To resolve Bazel modules from the default registry:

  • Run this command to resolve a module from the registry in your .bazelrc file:

    bazel build @<MODULE_NAME>

    Where:

    • <MODULE_NAME>: The name of the module you want to resolve

    For example:

    bazel build @rules_go

Resolve Modules from a Specific Registry

If you want to resolve Bazel modules from a different registry than the one in your .bazelrc file, you can specify a repository in the command.

To resolve Bazel modules from a specific registry:

  • Run this command to resolve a module from one Artifactory registry:

    bazel build --registry=https://<JFrogPlatformURL>/artifactory/api/bazelmodules/<REPO_NAME> @<MODULE_NAME>

    Where:

    • <JFrogPlatformURL>: Your organization's Artifactory hostname
    • <REPO_NAME>: The key of the virtual Bazel Modules repository you want to resolve from
    • <MODULE_NAME>: The name of the module you want to resolve

    For example:

    bazel build --registry=https://company.jfrog.io/artifactory/api/bazelmodules/bazel-modules-virtual @rules_go

Additional Bazel Information

The following additional information is available for Bazel in Artifactory:



Use Generic Repositories as Bazel Remote Cache Storage

While Bazel repositories in Artifactory support Bazel modules and proxying the Bazel Central Repository, you can also use a Generic repository in Artifactory as your Bazel remote cache. This allows you to set up your Bazel project to publish and resolve build artifacts to Artifactory.

📘

Note

This configuration involves security and access control considerations. Authorize only select users or build users to access and modify the cached artifacts.

To use Artifactory as your Bazel remote cache, complete the following steps:

Create a Generic Repository

Create a generic repository to act as your Bazel remote cache.

Prerequisite: You need Admin or Project Admin permissions in Artifactory to create a repository.

To create a Generic repository:

  1. In the Administration tab, click Repositories > Create a Repository.
  2. Select Local.
  3. Select the Generic package type.
  4. In the Repository Key field, enter a meaningful name for the repository. For example, bazel-remote-cache. For more information on local repositories and their settings, see Local Repositories.
  5. Click Create Repository. The repository is created and the Repositories page is displayed.

Configure Artifactory as Bazel Remote Cache

Once you create the Generic repository, configure Bazel to point to this repository as the remote cache.

To configure Artifactory as your Bazel remote cache:

  • Run the following command to set Artifactory as the remote cache:

    bazel build --remote_cache=https://<USERNAME>:<AUTH>@<JFrogPlatformURL>/<REPOSITORY_PATH>

    Where:

    • <USERNAME>: Your Artifactory username
    • <AUTH>: Your Artifactory identity token
    • <JFrogPlatformURL>: The URL of your JFrog Platform
    • <REPOSITORY_PATH>: The path to the target repository

    For example:

    bazel build --remote_cache=https://jeffry:<Token>@company.jfrog.io/bazel-remote-cache

During the build process, action result metadata is stored under the path /ac/, and output files are stored under the path /cas/, to be reused during the next build executions.

Validate Connection

You can validate the connection after you configure the remote cache.

To validate the connection:

  • Find the PUT and GET requests from the HTTP caching protocol in your Artifactory logs.

Bazel supports remote caching through the HTTP/1.1 protocol, which uploads binary data (BLOB) through PUT requests and downloads it through GET requests.

For example, for an environment with a Bazel remote cache where:

  • Generic repository name: cache-bazel
  • JFrog Platform URL: http://localhost:8082/artifactory/cache-bazel
  • SHA256 value of the action to upload or download: 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b

The PUT request will appear in the logs like this:

PUT /artifactory/cache-bazel/cas/15e2b0d3c33891ebb0f1ef609ec419420c20e320ce94c65fbc8c3312448eb225 HTTP/1.1
Host: localhost:8082
Accept: _/_
Content-Length: 9
Connection: Keep-Alive

0x310x320x330x340x350x360x370x380x39

The GET request will appear in the logs like this:

GET /artifactory/cache-bazel/ac/01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b HTTP/1.1
Host: localhost:8082
Accept: _/_
Connection: Keep-Alive

Use Maven Repositories as a Bazel Dependencies Cache

You can configure Maven repositories in Artifactory to act as a Bazel dependencies cache using the rules_jvm_external Bazel ruleset. This ruleset simplifies the management of external Java dependencies and integrates them seamlessly into your Bazel builds, so you can declare and manage dependencies from popular sources like Artifactory.

To use Artifactory as your Bazel dependencies cache:

Configure Bazel to Resolve Dependencies from Artifactory

Use the rules_jvm_external ruleset in a BUILD file to define and use external Java dependencies in your Bazel project.

Once you load the maven_install rule from the rules_jvm_external package, you can name and define a rule that specifies the external Java dependencies you want to use. List the artifacts you need, along with their group, name, and version.

Specify the remote repositories from which Bazel should fetch these dependencies. Lastly, define a java_binary target that depends on the rule. This target represents your Java application and can be configured with additional settings, such as the main class and any runtime dependencies.

To configure Bazel to resolve dependencies from Artifactory:

  1. Open your BUILD file for editing.

  2. Add the following ruleset to the file:

    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   
          "<JFrogPlatformURL>/artifactory/<REPO_NAME>",
        ],
    )
    
    java_binary(
        name = "<APP_NAME>",
        main_class = "<APP_CLASS>",
        runtime_deps = [":<RULE_NAME>"],
        # Add other configuration as needed
    )
    

    Where:

    • <RULE_NAME>: A descriptive name for the rule

    • <ARTIFACT_NAME>: The name of the dependency artifact

    • <ARTIFACT_VERSION>: The version of the dependency artifact

    • <JFrogPlatformURL>: The URL of your JFrog Platform

      📘

      Note

      If Artifactory requires authentication, pass your credentials in the URL. Use http://<USERNAME>:<AUTH>@<HOSTNAME> or https://<USERNAME>:<AUTH>@<HOSTNAME>.

    • <REPO_NAME>: The name of the Maven remote repository you want to use as a Bazel dependencies cache

    • <APP_NAME>: The label for the target

    • <APP_CLASS>: The full path to the Java class

    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:<Token>@localhost:8081/artifactory/maven-remote",
        ],
    )
    
    java_binary(
        name = "my_app",
        main_class = "com.example.MyApp",
        runtime_deps = [":my_dependencies"],
        # Add other configuration as needed
    )

Configure Bazel to Publish Dependencies to Artifactory

Use the rules_jvm_external ruleset in a BUILD file to publish Java dependencies to a Bazel project.

You can create a java_export target similar to a regular java_library parameter, but with two additional parameters to set Maven coordinates and add an optional template for the pom.xml file.

You can also set the sources and dependencies for the build process.

To configure Bazel to publish Java dependencies:

  1. Open your BUILD file for editing.

  2. Add the following ruleset to the file:

    # user_project/BUILD
    load("@rules_jvm_external//:defs.bzl", "java_export")
    
    java_export(
      name = "<APP_NAME>",
      maven_coordinates = "<GROUP_ID>:<ARTIFACT_ID>:<VERSION>",
      pom_template = "pom.tmpl",  # You can omit this
      srcs = glob(["*.java"]),
      deps = [
        "//<PACKAGE_PATH>",
        "@maven//:<DEPENDENCY_IDENTIFIER>",
      ],
    )

    Where:

    • <APP_NAME>: The label for the target
    • <GROUP_ID>:<ARTIFACT_ID>:<VERSION>: The target's unique identifier for Maven, using the group ID, artifact ID, and version values
    • <PACKAGE_PATH>: The directory path from the workspace root to the internal library
    • <DEPENDENCY_IDENTIFIER>: The Bazel label for the dependency, usually the group ID and artifact ID

    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", 
      srcs = glob(["*.java"]),
      deps = [
        "//user_project/utils",
        "@maven//:com_google_guava_guava",
      ],
    )
  3. Run the following command to publish the artifact to Artifactory local Maven repository:

    📘

    Note

    When you set gpg-sign to true, signing uses the current default key. Make sure that the gpg binary is installed on your machine before running this command.

    MAVEN_USER=<USERNAME> MAVEN_PASSWORD=<TOKEN> bazel run --stamp \
      --define "maven_repo=<JFrogPlatformURL>/artifactory/<REPO_NAME>" \
      --define gpg_sign=true \
      //user_project:<RULE_NAME>.publish

    Where:

    • <USERNAME>: Your Artifactory username
    • <TOKEN>: Your Artifactory identity token
    • <JFrogPlatformURL>: The URL of your JFrog Platform
    • <REPO_NAME>: The name of the local Maven repository to publish to
    • <RULE_NAME>: The name value of your previously defined java_export rule

    For example:

    MAVEN_USER=admin MAVEN_PASSWORD=<Token> bazel run --stamp \
      --define "maven_repo=https://acme.jfrog.io/artifactory/maven_local" \
      --define gpg_sign=true \
      //user_project:exported_lib.publish

Limitations of Bazel Modules in Artifactory

The following are the limitations of Bazel Modules repositories in Artifactory:

  • Bazel Build Artifacts: Bazel Modules repositories are for module dependency management only, and don't support build artifacts. You can use Generic and Maven repositories for Bazel build artifacts and other dependencies. For more information, see Additional Bazel Information.
  • Xray: JFrog Xray doesn't scan Bazel Modules repositories.
  • Curation: JFrog Curation doesn't apply to Bazel Modules repositories.
  • JFrog CLI: Artifactory doesn't support the jf bazel command. You can use the bazel client to resolve modules.

Frequently Asked Questions

This section provides answers to frequently asked questions.

plusFAQs
Q: How do I publish a Bazel module to Artifactory?

A: Upload the module archive and MODULE.bazel with the JFrog CLI, and leave source.json out of the upload. Artifactory generates source.json so Bazel can resolve the module. For more information, see Publish a Bazel Module.

Q: What happens if I upload only the module archive?

A: Artifactory doesn't extract MODULE.bazel from the archive, so upload that file on its own. Leave source.json out of the upload. For more information, see Publish a Bazel Module.

Q: What is the difference between a Bazel Modules repository and a Generic repository?

A: A Bazel Modules repository stores modules for dependency resolution. A Generic repository stores build artifacts and can be the remote cache. For more information, see Limitations of Bazel Modules in Artifactory.

Q: What do I need before I connect Bazel to Artifactory?

A: You need a Bazel Modules repository. Create one with Admin or Project Admin permissions, then add your credentials to the netrc file and the registry to .bazelrc. For more information, see Connect Bazel to Artifactory.

Q: Can I resolve public modules from the Bazel Central Registry through Artifactory?

A: Yes. Use a virtual repository so one registry URL resolves your private modules and public modules from the Bazel Central Registry. For more information, see Resolve Bazel Modules.

Related Topics


Did this page help you?