如何在Bazel v0.19 +中使用JAR工具?

时间:2018-10-30 14:48:40

标签: bazel

从Bazel v0.19开始,如果您有引用@bazel_tools//tools/jdk:jar的Starlark(以前称为“ Skylark”)代码,则在构建时会看到如下消息:

WARNING: <trimmed-path>/external/bazel_tools/tools/jdk/BUILD:79:1: in alias rule @bazel_tools//tools/jdk:jar: target '@bazel_tools//tools/jdk:jar' depends on deprecated target '@local_jdk//:jar': Don't depend on targets in the JDK workspace; use @bazel_tools//tools/jdk:current_java_runtime instead (see https://github.com/bazelbuild/bazel/issues/5594)

如果我想访问@bazel_tools//tools/jdk:current_java_runtime命令,我想可以使java正常工作,但是我不确定要获得jar所需做什么。工作工具。 linked GitHub issue的内容似乎没有解决这个特定问题。

2 个答案:

答案 0 :(得分:1)

我偶然发现a commit to Bazel,它对Starlark Java规则进行了类似的调整。它使用以下模式:(我对代码进行了一些编辑)

# in the rule attrs:
    "_jdk": attr.label(
        default = Label("//tools/jdk:current_java_runtime"),
        providers = [java_common.JavaRuntimeInfo],
    ),

# then in the rule implementation:
    java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo]
    jar_path = "%s/bin/jar" % java_runtime.java_home
     ctx.action(
         inputs = ctx.files._jdk + other inputs,
         outputs = [deploy_jar],
         command = "%s cmf %s" % (jar_path, input_files),
     )

另外,java处的str(java_runtime.java_executable_exec_path)javac处的"%s/bin/javac" % java_runtime.java_home处。

另请参见a pull request with a simpler example

答案 1 :(得分:0)

因为我对jar工具的引用位于顶级宏的某个规则中,而不是规则,所以我无法使用Rodrigo的回答中的方法。相反,我明确引用了current_java_runtime工具链,然后能够使用JAVABASE make variable作为jar工具的基本路径。

native.genrule(
    name = genjar_rule,
    srcs = [<rules that create files being jar'd>],
    cmd = "some_script.sh $(JAVABASE)/bin/jar $@ $(SRCS)",
    tools = ["some_script.sh", "@bazel_tools//tools/jdk:current_java_runtime"],
    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
    outs = [<some outputs>]
)