Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created October 4, 2024 02:59
Show Gist options
  • Save fzakaria/54ac80051f16b946f712d34d6db06be0 to your computer and use it in GitHub Desktop.
Save fzakaria/54ac80051f16b946f712d34d6db06be0 to your computer and use it in GitHub Desktop.
Creating a JAR natively in Starlark
load("@bazel_skylib//rules:write_file.bzl", "write_file")
genrule(
name = "example",
outs = ["example.jar"],
cmd = """
OLD_CWD=$$(pwd) && cd $(RULEDIR) && \\
$$OLD_CWD/$(JAVABASE)/bin/jar cmfv $(rootpath :manifest) \\
$$OLD_CWD/$@ $(rootpath :main_class)
""",
srcs = [
":main_class",
":manifest",
],
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
)
genrule(
name = "main_class",
outs = ["Main.class"],
cmd = "$(JAVABASE)/bin/javac $(SRCS) -d $$(dirname $@)",
srcs = [
"Main.java",
],
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
)
write_file(
name = "manifest",
out = "META-INF/MANIFEST.MF",
# extra line needed for line break
content = ["Manifest-Version: 1.0", "Main-Class: Main", ""],
)
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
bazel_dep(name = "bazel_skylib", version = "1.7.1")
@fzakaria
Copy link
Author

fzakaria commented Oct 4, 2024

The idea for this is to have a pkg_jar in rules_pkg that can natively create JAR rather than going through pkg_zip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment