Created
October 4, 2024 02:59
-
-
Save fzakaria/54ac80051f16b946f712d34d6db06be0 to your computer and use it in GitHub Desktop.
Creating a JAR natively in Starlark
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
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", ""], | |
) |
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
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Hello World!"); | |
} | |
} |
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
bazel_dep(name = "bazel_skylib", version = "1.7.1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The idea for this is to have a pkg_jar in rules_pkg that can natively create JAR rather than going through pkg_zip.