Search code examples
protocol-buffersbazelbazel-rulesbazel-java

How to use protobuf from remote Artifactory?


There is a protobuf that my project needs. The protobuf itself is in another repo altogether owned by a completely different team. However they do publish their artifacts on my company's internal Artifactory.

How can I use that (non Bazel) protobuf in my Bazel project?

This is the direction I was trying to go before I hit a deadend.

WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

http_archive(
    name = "rules_proto",
    sha256 = "80d3a4ec17354cccc898bfe32118edd934f851b03029d63ef3fc7c8663a7415c",
    strip_prefix = "rules_proto-5.3.0-21.5",
    urls = [
        "https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.5.tar.gz",
    ],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

http_file(
    name = "some-service-protobuf",
    url = "https://artifactory.xyz.com/artifactory/x/y/some-service-protobuf.dbgrel.zip",
)

BUILD file:

load("@rules_proto//proto:defs.bzl", "proto_library")
proto_library(
    name = "SomeService.proto",
)

The resources I'm trying to use:

  1. https://bazel.build/reference/be/java#java_proto_library
  2. https://github.com/bazelbuild/rules_proto

Is fetching proto files from remote URL possible? How show I do it after copying those files in my repo.


Solution

  • You might try using http_archive instead:

    https://bazel.build/rules/lib/repo/http#http_archive

    And then use the build_file or build_file_content attributes to add the build file with the proto_library definition to the external workspace created from the zip file:

    http_archive(
        name = "some-service-protobuf",
        url = "https://artifactory.xyz.com/artifactory/x/y/some-service-protobuf.dbgrel.zip",
        build_file = "BUILD.some_service",
    )
    

    BUILD.some_service:

    load("@rules_proto//proto:defs.bzl", "proto_library")
    proto_library(
        name = "some_service_proto",
        srcs = ["SomeService.proto"],
    )
    

    Then in another build file you can do something like:

    java_proto_library(
      name = "some_service_java_protobuf",
      deps = ["@some-service-protobuf//:some_service_proto"],
    )