配置bazel以使用特定的JVM版本进行构建/测试 [英] Configuring bazel to build/test using a specific JVM version

查看:480
本文介绍了配置bazel以使用特定的JVM版本进行构建/测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估Bazel并尝试对其进行配置,以使其使用与构建时所用的JDK不同的JDK来构建和测试我的Scala项目.

I am evaluating Bazel and trying to configure it so that it builds and tests my scala project using a different JDK than the one it has been built with.

这些是我bazel info输出中的相关行:

These are the relevant lines from the output of my bazel info:

java-home: /nix/store/09x4mnxfzppwq1yjaakrfa6aj3rp7sw8-openjdk-11.0.4-ga/lib/openjdk
java-runtime: OpenJDK Runtime Environment (build 11.0.3-internal+0-adhoc..jdk11u-jdk-11.0.3-ga) by Oracle Corporation
java-vm: OpenJDK 64-Bit Server VM (build 11.0.3-internal+0-adhoc..jdk11u-jdk-11.0.3-ga, mixed mode) by Oracle Corporation
max-heap-size: 8325MB
package_path: %workspace%
release: release 1.1.0- (@non-git)

虽然我的软件包管理器似乎是使用openjdk的较新版本构建的Bazel,但我要构建/测试的项目应该使用我安装在/usr/lib/jvm/java-8-openjdk下的JDK.

While my package manager seems to have built Bazel using a fairly recent version of openjdk, the project I want to build/test should instead use a JDK I have installed under: /usr/lib/jvm/java-8-openjdk.

通过研究各种github问题,最终得到了以下解决方案,该解决方案使我可以获得绿色版本:

By digging into various github issues, I ended up with the following solution, which allows me to get a green build:

bazel test \
    --define=ABSOLUTE_JAVABASE=/usr/lib/jvm/java-8-openjdk \
    --host_javabase=@bazel_tools//tools/jdk:absolute_javabase \
    --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
    --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla \
    --test_timeout=10 --test_output=all //...

但是,我仍然对以下内容感到困惑:

However, I am still confused on the following:

  • 我如何才能基于项目永久设置此选项,这样我就不必一直提供它们?
  • 我可以通过intellij Bazel插件自动获取它的方式来执行此操作吗?
  • 上面的设置到底是什么?要覆盖相同的结果,有没有更简单的方法?

推荐答案

我如何才能基于项目永久设置此选项,这样我就不必一直提供它们? 我可以通过intellij Bazel插件自动选择它的方式来做到这一点吗?

How can I permanently set this option on a project basis, so that I don't have to supply them all the times? Can I do so in such a way that the intellij Bazel plugin will automatically pick it up?

是的.将此添加到<project root>/.bazelrc中,以使所有构建都使用本地JDK:

Yes. Add this to <project root>/.bazelrc for all builds to use the local JDK:

build --define=ABSOLUTE_JAVABASE=/usr/lib/jvm/java-8-openjdk
build --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
build --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla

IntelliJ插件将自动提取这些信息.

The IntelliJ plugin will pick these up automatically.

或者,将它们分组为.bazelrc配置,例如local_jdk,您可以使用bazel build //:target --config=local_jdk进行选择,因此默认的无配置版本不会受到影响.

Alternatively, group these under a .bazelrc configuration like local_jdk that you can select with bazel build //:target --config=local_jdk, so default config-less builds aren't affected.

build:local_jdk --define=ABSOLUTE_JAVABASE=/usr/lib/jvm/java-8-openjdk
build:local_jdk --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
build:local_jdk --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla
build:local_jdk --java_toolchain=@bazel_tools//tools/jdk:toolchain_vanilla

--host_javabase定义Java规则用于宿主工具编译的JDK的位置.

--host_javabase defines the location of the JDK used by Java rules for host tools compilation.

$ bazel query --output=build @bazel_tools//tools/jdk:absolute_javabase

java_runtime(
  name = "absolute_javabase",
  tags = ["__JAVA_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"],
  generator_name = "absolute_javabase",
  generator_function = "java_runtime",
  generator_location = "tools/jdk/BUILD:75",
  java_home = "$(ABSOLUTE_JAVABASE)",
)

--host_java_toolchain定义了用于宿主工具编译的Java工具集.

--host_java_toolchain defines the set of Java tools used for host tools compilation.

$ bazel query --output=build @bazel_tools//tools/jdk:toolchain_vanilla

java_toolchain(
  name = "toolchain_vanilla",
  tags = ["__JAVA_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"],
  generator_name = "toolchain_vanilla",
  generator_function = "default_java_toolchain",
  generator_location = "tools/jdk/BUILD:367",
  source_version = "",
  target_version = "",
  bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"],
  misc = ["-XDskipDuplicateBridges=true", "-g", "-parameters"],
  jvm_opts = [],
  javac_supports_workers = True,
  javac = ["@bazel_tools//tools/jdk:javac_jar"],
  tools = ["@bazel_tools//tools/jdk:java_compiler_jar", "@bazel_tools//tools/jdk:jdk_compiler_jar"],
  javabuilder = ["@bazel_tools//tools/jdk:vanillajavabuilder"],
  singlejar = ["@bazel_tools//tools/jdk:singlejar"],
  genclass = ["@bazel_tools//tools/jdk:genclass"],
  ijar = ["@bazel_tools//tools/jdk:ijar"],
  header_compiler = ["@bazel_tools//tools/jdk:turbine"],
  header_compiler_direct = ["@bazel_tools//tools/jdk:turbine_direct"],
  forcibly_disable_header_compilation = True,
)

--java_toolchain定义了用于目标Java编译的Java工具集.它独立于--host_java_toolchain来分离编译宿主工具和实际源代码编译.

--java_toolchain defines the set of Java tools used for Java compilation for your target. This is independent of --host_java_toolchain to decouple compiling host tools and actual source compilation.

这篇关于配置bazel以使用特定的JVM版本进行构建/测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆