Created
November 7, 2024 17:47
-
-
Save fzakaria/4e98f65be96cf7f8b13081e75d7a2bf8 to your computer and use it in GitHub Desktop.
Bazel Starlark rule to create a JarIndex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _jar_index_impl(ctx): | |
java_info = ctx.attr.src[JavaInfo] | |
java_runtime = ctx.attr._java_runtime[java_common.JavaRuntimeInfo] | |
java_home = java_runtime.java_home | |
jar_bin = "%s/bin/jar" % java_home | |
runtime_jars = " " | |
for jar in java_info.transitive_runtime_jars.to_list(): | |
runtime_jars += jar.path + " " | |
cmds = [ | |
"%s -i %s %s" % (jar_bin, java_info.java_outputs[0].class_jar.path, runtime_jars), | |
"cp %s %s" % (java_info.java_outputs[0].class_jar.path, ctx.outputs.index.path), | |
] | |
ctx.actions.run_shell( | |
inputs = [ java_info.java_outputs[0].class_jar] + java_info.transitive_runtime_jars.to_list(), | |
outputs = [ctx.outputs.index], | |
tools = java_runtime.files, | |
command = ";\n".join(cmds), | |
) | |
return [ | |
DefaultInfo(files = depset([ctx.outputs.index])), | |
] | |
jar_index = rule( | |
implementation = _jar_index_impl, | |
attrs = { | |
"src": attr.label( | |
mandatory = True, | |
providers = [JavaInfo], | |
), | |
"_java_runtime": attr.label( | |
default = "@bazel_tools//tools/jdk:current_java_runtime", | |
providers = [java_common.JavaRuntimeInfo], | |
), | |
}, | |
outputs = {"index": "%{name}_index.jar"}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces a
INDEX.LIST
file like so: