简体   繁体   English

如何用 Bazel 指定 Java 版本

[英]How to specify Java version with Bazel

I am using Bazel to build and test a Java project.我正在使用 Bazel 构建和测试 Java 项目。 It seems to be compiling with JDK10 that it downloaded from somewhere.它似乎正在使用从某处下载的 JDK10 进行编译。 Would like to specify that it use JDK13.想指定它使用JDK13。

How do I do this?我该怎么做呢? is it something i can put in the.bazelrc or BUILD file?我可以把它放在 .bazelrc 或 BUILD 文件中吗? Really having trouble with this.真的有这方面的麻烦。

java_toolchain seems to do the trick: java_toolchain似乎可以解决问题:

For example:例如:

In your BUILD file add:在您的BUILD文件中添加:

java_toolchain(
    name = "bootstrap_toolchain",
    # javac -extdirs is implemented by appending the contents to the platform
    # class path after -bootclasspath. For convenience, we currently have a
    # single jar that contains the contents of both the bootclasspath and
    # extdirs.
    bootclasspath = ["//tools/jdk:platformclasspath.jar"],
    extclasspath = [],
    genclass = ["bootstrap_genclass_deploy.jar"],
    ijar = ["//third_party/ijar"],
    javabuilder = ["bootstrap_VanillaJavaBuilder_deploy.jar"],
    javac = ["//third_party/java/jdk/langtools:javac_jar"],
    jvm_opts = [
        # Prevent "Could not reserve enough space for object heap" errors on Windows.
        "-Xmx512m",
        # Using tiered compilation improves performance of Javac when not using the worker mode.
        "-XX:+TieredCompilation",
        "-XX:TieredStopAtLevel=1",
    ],
    singlejar = ["//src/java_tools/singlejar:bootstrap_deploy.jar"],
    source_version = "8",
    tags = ["manual"],
    target_version = "8",
    visibility = ["//visibility:public"],
)

.bazelrc .bazelrc

build --java_toolchain=//:bootstrap_toolchain

Bazel comes with some toolchains already defined, and they can specified on the command line or in your.bazlrc: Bazel 附带了一些已经定义好的工具链,它们可以在命令行或 your.bazlrc 中指定:

build --java_toolchain=@bazel_tools//tools/jdk:toolchain_java11 --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_java11

Unfortunately no standard JDK13 toolchain yet..不幸的是,还没有标准的 JDK13 工具链..

You can now Configure the JDK Toolchain to use a locally installed JDK.您现在可以将 JDK 工具链配置为使用本地安装的 JDK。

After installing OpenJDK 16, I created ~/.bazelrc with the following contents:安装 OpenJDK 16 后,我创建了~/.bazelrc ,其内容如下:

build --define=ABSOLUTE_JAVABASE=/home/guaporocco/.jdks/openjdk-16
build --javabase=@bazel_tools//tools/jdk:absolute_javabase
build --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla

I can confirm that this works with test projects using features introduced after version 11, like multi-line-strings.我可以确认这适用于使用版本 11 之后引入的功能的测试项目,例如多行字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM