bazelbazel-java

How to extract resources from jar in bazel


I have different modules in my project which are generating config files as a JSON which are part of java_libarary. I need to copy this generated JSON files to a new module using bazel build. I am thinking algorithm to do that as :

  1. Read all dependency from bazel (assumption all modules which generate json will be added as dependency).
  2. Extract JAR files once by one in dependency
  3. Copy json to new location
  4. Package copied json to new tar as output

I am not sure, how i can do this in bazel, let me know if any one has similar example available.

Thanks in Advance...


Solution

  • I am using genrule to achieve same as :

    In java_library BUILD file

    genrule(
        name = "core-search-registry-gen-rule",
        srcs = [
            ":core-alarm",
        ],
        outs = ["search-registry.zip"],
        cmd = """
        mkdir -p $(@D)/search_registry && \
        $(JAVABASE)/bin/jar xf $(location :core-alarm)  && \
        for x in `find . -type f -name "*_registry.json"`; \
            do cp $$x $(@D)/search_registry/; done && \
        (cd $(@D) && zip -rq search-registry.zip search_registry/)
        """,
        message = "Concatenation of all json files one zip file",
        toolchains = [
            "@bazel_tools//tools/jdk:current_java_runtime",
        ],
        visibility = ["//mp:__subpackages__"],
    )
    

    In packaging BUILD file :

    pkg_tar(
        name = "etc-search-registry",
        files = [
            "//mp/alarms/core:core-search-registry-gen-rule",
        ],
        mode = "0644",
        package_dir = "/etc/conf",
    )
    

    Thanks