Skip to content

[bzlmod] How to declare artifacts outside MODULE.bazel #854

Closed as not planned
@joca-bt

Description

@joca-bt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions