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.