Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring bazel to build/test using a specific JVM version

Tags:

java

bazel

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.

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)

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.

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:

  • 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?
  • What are the settings above overriding exactly, and is there a simpler way to achieve the same result?
like image 903
Andrea Fiore Avatar asked Mar 04 '23 03:03

Andrea Fiore


1 Answers

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?

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

The IntelliJ plugin will pick these up automatically.

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 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 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 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.

like image 123
Jin Avatar answered May 16 '23 07:05

Jin