rules_jvm_external
rules_jvm_external copied to clipboard
[bzlmod] How to declare artifacts outside MODULE.bazel
In my projects I like to define the artifacts outside the WORKSPACE file, so that my WORKSPACE file looks cleaner:
WORKSPACE file:
...
load("//bazel:repositories.bzl", "rules_jvm_external")
# Import additional Java rules.
rules_jvm_external()
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
rules_jvm_external_deps()
load("//bazel:maven.bzl", "install_maven_artifacts")
install_maven_artifacts()
load("@maven//:defs.bzl", "pinned_maven_install")
pinned_maven_install()
...
bazel/maven.bzl:
load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@rules_jvm_external//:specs.bzl", "maven")
_ARTIFACTS = [
maven.artifact("com.google.guava", "guava", "31.1-jre"),
]
_REPOSITORIES = [
"https://repo.maven.apache.org/maven2/",
]
def install_maven_artifacts():
maven_install(
artifacts = _ARTIFACTS,
repositories = _REPOSITORIES,
duplicate_version_warning = "error",
fail_if_repin_required = True,
fail_on_missing_checksum = False,
fetch_sources = True,
maven_install_json = "//bazel:maven.json",
version_conflict_policy = "pinned",
)
See demo.zip for an complete example.
I am having trouble figuring out how to do this with bzlmod. I'd like to leave the artifacts out of this file and have them in another one like before, since that file has a few hundred lines.
MODULE.bazel:
...
# https://github.com/bazelbuild/rules_jvm_external/
bazel_dep(name = "rules_jvm_external", version = "4.5")
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
# load bazel/maven.bzl and import macro for maven.install()
# run macro
use_repo(maven, "maven", "unpinned_maven")
...
What would be the way to achieve this? The only thing I can load from MODULE.bazel would be an extension but I am not able to achieve what I need that way.
The MODULE.bazel file must contain everything needed by the project within it, and cannot call load statements to help make it easier to navigate. If you'd like to use load statements in your module definitions, please raise an issue in https://github.com/bazelbuild/bazel
I agree that it's not particularly pleasant right now. One thing that we could consider is following rules_go's lead and providing a tag that allows us to read a file kept outside of the module definition that could be shared by the traditional WORKSPACE-based approach
Can we please add an extra tag to the rules_jvm_external extension to load a list of artifacts from a file? It's not possible to load a bzl file in MODULE.bazel but a module extension can load and read a file https://bazel.build/rules/lib/builtins/module_ctx#read
It seems like it would be possible to read a file that lists the dependencies, it is also possible to execute a program to parse that file (https://bazel.build/rules/lib/builtins/module_ctx#execute)
The file or the execution result of running the program could be used to build up a list of artifacts, overrides, and exclusions.
A feature like this would be immensely useful, especially on large mono repo products with a large list of dependencies.
The discussion in https://github.com/bazelbuild/bazel/issues/17880 seems to be stale. There are a few suggestions on how to do this in the extensions themselves. @shs96c would rules_jvm_external be interested in doing any of the suggestions?