shabunc
shabunc

Reputation: 24731

How to extract resource from downloaded jar

I want to extract some specific resource from jar (which, in turn, is downloaded as a part of http archive) and while I know how to achieve this in principle I don't know what is the most bazelish and minimal way to achieve this.

I've naïvely tried (after reading this answer) to do something like this:

new_http_archive(
    name="some_jar_contents",
    url="@some_archive//:lib/some_jar.jar",
    build_file_content="""
      filegroup(
          name = "srcs",
          srcs = glob(["*"]),
          visibility = ["//visibility:public"]
      )
    """
)

However, I'm predictably getting java.net.MalformedURLException: no protocol:

Upvotes: 0

Views: 787

Answers (1)

Xiao Liang
Xiao Liang

Reputation: 756

The problem you are getting is caused by the fact that Bazel does not understand the url @some_archive//:lib/some_jar.jar.
It cannot infer the protocol to use for fetching the Jar, and the exception is thrown.

The rule new_http_archive is intended to be used for fetching a compressed archive from certain remote location, then build & expose it as an external target to current repo.

You need to change the url parameter to the actual URL in order to let Bazel fetch the Jar with the resource you want to extract.
Then, in build_file_content parameter part, use genrule to move the desired resource file, and export it.

A working example can be found in this private gist.

The example does the following things:

  • Fetches the Jar for AutoValue in WORKSPACE
  • Uses genrule to extract the autovalue.vm file from the Jar
  • Have a Python Program that reads and prints content of autovalue.vm

Helpful resources:

Upvotes: 3

Related Questions