Bazel rules to resolve, fetch and export Maven artifacts
Bazel rules to resolve, fetch and export Maven artifacts
Transitive Maven artifact resolution and publishing rules for Bazel.
netrc filesGet the latest release here.
You can find examples in the examples/ directory.
Find other GitHub projects using rules_jvm_external
with this search query.
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.
7.x and 9.x is only available on releases 7.x5.x and 7.x is only available on releases 6.x.4.x and 5.4 is only available on releases 5.x.4.0.0 is only available on releases 4.2 or earlier.You can find the complete API reference for rules at docs/api.md and for the bzlmod tag classes in docs/bzlmod.md
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.
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:
lock_file attribute to the install tag. The lock file is traditionally
called maven_install.json.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.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:
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,
@maven top level package (@maven//).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//:pinThen, specify lock_file in your maven.install in MODULE.bazel:
maven.install(
# artifacts, repositories, ...
lock_file = "//:maven_install.json",
)maven_install.jsonWhenever 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//:pinWithout re-pinning, maven_install will not pick up the changes made to
MODULE.bazel, as maven_install.json is now the source of truth.
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:
# To repin everything:
REPIN=1 bazel run @maven//:pin
# To only repin rules_jvm_external:
RULES_JVM_EXTERNAL_REPIN=1 bazel run @maven//:pinAlternatively, 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.
maven_install.jsonYou 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:
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.
maven_install.json filesIf you have multiple maven.install declarations, each should have a unique
name and its own lock_file:
maven.install(
name = "foo",
lock_file = "//:foo_maven_install.json",
# ...
)
maven.install(
name = "bar",
lock_file = "//:bar_maven_install.json",
# ...
)Maven BOMs can be used by using the boms attribute, for example:
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",
],
)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_coreTransitive 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.
To check for updates of artifacts, run the following command at the root of your Bazel workspace:
$ bazel run @maven//:outdatedThis will list the coordinates of every dependency that might need to be updated in your repo.
To download the source JAR alongside the main artifact JAR, set fetch_sources = True in maven_install:
maven_install(
artifacts = [
# ...
],
repositories = [
# ...
],
fetch_sources = True,
)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:
maven_install(
artifacts = [
# ...
],
repositories = [
# ...
],
fail_on_missing_checksum = False,
)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:
$ 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
No open issues yet, or sync has not completed.