Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rules_jvm_external

> 包管理
Open source

Bazel rules to resolve, fetch and export Maven artifacts

373 stars0 likes6 views
WebsiteGitHub

About

Bazel rules to resolve, fetch and export Maven artifacts

rules_jvm_external

Transitive Maven artifact resolution and publishing rules for Bazel.

  • Main build:
  • Examples build:

Features

  • MODULE.bazel bzlmod configuration
  • Artifact version resolution with Coursier, Maven or Gradle
  • Import downloaded JAR, AAR, source JARs
  • Export built JARs to Maven repositories
  • Pin resolved artifacts with their SHA-256 checksums into a version-controllable JSON file
  • Custom Maven repositories
  • Private Maven repositories using netrc files
  • Integration with Bazel's downloader and caching mechanisms for sharing artifacts across Bazel workspaces
  • Versionless target labels for simpler dependency management
  • Ability to declare multiple sets of versioned artifacts
  • Supported on Windows, macOS, Linux

Get the latest release here.

Examples

You can find examples in the examples/ directory.

Projects using rules_jvm_external

Find other GitHub projects using rules_jvm_external with this search query.

Prerequisites

Compatibility guideline: This project aims to be backwards compatible with the (current LTS - 2) version. If the current LTS version is 9, then we aim to support versions 7, 8 and 9.

The project will only support the last release of non-LTS releases. That is, if the current LTS version is 9, then only Bazel 7.7.1 would be supported of the 7.x versions.

  • Support for Bazel versions between 7.x and 9.x is only available on releases 7.x
  • Support for Bazel versions between 5.x and 7.x is only available on releases 6.x.
  • Support for Bazel versions between 4.x and 5.4 is only available on releases 5.x.
  • Support for Bazel versions before 4.0.0 is only available on releases 4.2 or earlier.

API Reference

You can find the complete API reference for rules at docs/api.md and for the bzlmod tag classes in docs/bzlmod.md

Usage

See bzlmod.md for complete usage instructions.

…

Credentials for private repositories can also be specified using a property file or environment variables. See the Coursier documentation for more information. You may also use a .netrc file.

Pinning and lock files

In order to support repeatable builds, and to avoid projects needing to perform their own dependency resolution at build time, it is strongly recommend that projects "pin" their dependencies. This is a three step process:

  1. Add a lock_file attribute to the install tag. The lock file is traditionally called maven_install.json.
  2. touch maven_install.json BUILD.bazel The lock file must exist before pinning but may be empty. It must also be a valid target. This command creates both of the required files.
  3. REPIN=1 bazel run @maven//:pin This will generate the lock file.

You may now add the build and lock file to source control.

Subsequent repins are required every time you modify the artifacts or BOMs in used in the dependency resolution. rules_jvm_external will fail the build and notify you if a repin is ever required.

Next, reference the artifacts in the BUILD file with their versionless label:

python
java_library(
    name = "java_test_deps",
    exports = [
        "@maven//:junit_junit",
        "@maven//:org_hamcrest_hamcrest_library",
    ],
)

android_library(
    name = "android_test_deps",
    exports = [
        "@maven//:junit_junit",
        "@maven//:androidx_test_espresso_espresso_core",
    ],
)

The default label syntax for an artifact foo.bar:baz-qux:1.2.3 is @maven//:foo_bar_baz_qux. That is,

  • All non-alphanumeric characters are substituted with underscores.
  • Only the group and artifact IDs are required.
  • The target is located in the @maven top level package (@maven//).

Pinning artifacts and integration with Bazel's downloader

rules_jvm_external supports pinning artifacts and their SHA-256 checksums into a maven_install.json file that can be checked into your repository.

Without artifact pinning, in a clean checkout of your project, rules_jvm_external executes the full artifact resolution and fetching steps (which can take a bit of time) and does not verify the integrity of the artifacts against their checksums. The downloaded artifacts also cannot be shared across Bazel workspaces.

By pinning artifact versions, you can get improved artifact resolution and build times, since using maven_install.json enables rules_jvm_external to integrate with Bazel's downloader that caches files on their sha256 checksums. It also improves resiliency and integrity by tracking the sha256 checksums and original artifact urls in the JSON file.

Since all artifacts are persisted locally in Bazel's cache, it means that fully offline builds are possible after the initial bazel fetch @maven//.... The artifacts are downloaded with http_file which supports netrc for authentication. Your ~/.netrc will be included automatically. To pass machine login credentials in the ~/.netrc file to coursier, specify use_credentials_from_home_netrc_file = True in your maven_install rule. For additional credentials, add them in the repository URLs passed to maven_install (so they will be included in the generated JSON). Alternatively, pass an array of additional_netrc_lines to maven_install for authentication with credentials from outside the workspace.

To get started with pinning artifacts, run the following command to generate the initial maven_install.json at the root of your Bazel workspace:

$ bazel run @maven//:pin

Then, specify lock_file in your maven.install in MODULE.bazel:

starlark
maven.install(
    # artifacts, repositories, ...
    lock_file = "//:maven_install.json",
)

Updating maven_install.json

Whenever you make a change to the list of artifacts or repositories and want to update maven_install.json, run this command to re-pin the unpinned @maven repository:

$ REPIN=1 bazel run @maven//:pin

Without re-pinning, maven_install will not pick up the changes made to MODULE.bazel, as maven_install.json is now the source of truth.

Requiring lock file repinning when the list of artifacts changes

It can be easy to forget to update the maven_install.json lock file when updating artifacts in a maven_install. Normally, rules_jvm_external will print a warning to the console and continue the build when this happens, but by setting the fail_if_repin_required attribute to True, this will be treated as a build error, causing the build to fail. When this attribute is set, it is possible to update the maven_install.json file using:

bash
# To repin everything:
REPIN=1 bazel run @maven//:pin

# To only repin rules_jvm_external:
RULES_JVM_EXTERNAL_REPIN=1 bazel run @maven//:pin

Alternatively, it is also possible to modify the fail_if_repin_required attribute in your MODULE.bazel file, run bazel run @maven//:pin and then reset the fail_if_repin_required attribute.

Custom location for maven_install.json

You can specify a custom location for maven_install.json by changing the lock_file attribute value to point to the new file label. For example:

starlark
maven.install(
    name = "maven_install_in_custom_location",
    artifacts = ["com.google.guava:guava:27.0-jre"],
    repositories = ["https://repo1.maven.org/maven2"],
    lock_file = "//tests/custom_maven_install:maven_install.json",
)

Note: The //:maven_install.json label assumes you have a BUILD file in your project's root directory. If you do not have one, create an empty BUILD file to fix issues you may see. See #242

Note: If you're using an older version of rules_jvm_external and haven't repinned your dependencies, you may see a warning that you lock file "does not contain a signature of the required artifacts" then don't worry: either ignore the warning or repin the dependencies.

Future artifact pinning updates to maven_install.json will overwrite the file at the specified path instead of creating a new one at the default root directory location.

Multiple maven_install.json files

If you have multiple maven.install declarations, each should have a unique name and its own lock_file:

starlark
maven.install(
    name = "foo",
    lock_file = "//:foo_maven_install.json",
    # ...
)

maven.install(
    name = "bar",
    lock_file = "//:bar_maven_install.json",
    # ...
)

Support for Maven BOM files

Maven BOMs can be used by using the boms attribute, for example:

starlark
maven.install(
    boms = [
        "org.seleniumhq.selenium:selenium-bom:4.18.1",
    ],
    artifacts = [
        # This dependency is included in the `selenium-bom`, so we can omit the version number
        "org.seleniumhq.selenium:selenium-java",
    ],
)

Generated targets

For the junit:junit example, using bazel query @maven//:all --output=build, we can see that the rule generated these targets:

…

These targets can be referenced by:

  • @maven//:junit_junit
  • @maven//:org_hamcrest_hamcrest_core

Transitive classes: To use a class from hamcrest-core in your test, it's not sufficient to just depend on @maven//:junit_junit even though JUnit depends on Hamcrest. The compile classes are not exported transitively, so your test should also depend on @maven//:org_hamcrest_hamcrest_core. However it is recommended that you add first-order dependencies directly to your build files.

Original coordinates: The generated tags attribute value also contains the original coordinates of the artifact, which integrates with rules like bazel-common's pom_file for generating POM files. See the pom_file_generation example for more information.

Outdated artifacts

To check for updates of artifacts, run the following command at the root of your Bazel workspace:

$ bazel run @maven//:outdated

This will list the coordinates of every dependency that might need to be updated in your repo.

Advanced usage

Fetch source JARs

To download the source JAR alongside the main artifact JAR, set fetch_sources = True in maven_install:

python
maven_install(
    artifacts = [
        # ...
    ],
    repositories = [
        # ...
    ],
    fetch_sources = True,
)

Checksum verification

Artifact resolution will fail if a SHA-1 or MD5 checksum file for the artifact is missing in the repository. To disable this behavior, set fail_on_missing_checksum = False in maven_install:

python
maven_install(
    artifacts = [
        # ...
    ],
    repositories = [
        # ...
    ],
    fail_on_missing_checksum = False,
)

Using a custom Coursier download url

By default bazel bootstraps Coursier via the urls specificed in versions.bzl. However in case they are not directly accessible in your environment, you can also specify a custom url to download Coursier. For example:

$ bazel build @maven_with_unsafe_shared_cache//... --repo_env=COURSIER_URL='https://my_secret_host.com/vXYZ/coursier.jar'

Please note it still requires the SHA to match.

To use a different Coursier version entirely, you can override both the URL and SHA256:

bash
$ bazel build @maven//... \
  --repo_env=COURSIER_URL='https://github.com/coursier/coursier/releases/download/v2.1.25-M23/coursier.jar' \
  --repo_env=COURSIER_SHA256=''

Note: rules_jvm_external is tested agains

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javabazelmaven

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image