Force Bazel to use local java only

531 views
Skip to first unread message

Benedek Thaler

unread,
May 1, 2019, 3:18:41 AM5/1/19
to bazel-...@googlegroups.com
Hi,

I'd like to use bazel in a locked-down environment, one without uncontrolled internet access. I'd like to force bazel to use the locally installed java, instead of downloading one. I could not figure out the required combination of flags - this is what I tried:

$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
$ javac -version
javac 1.8.0_171
$ bazel clean --expunge
$ bazel info release
release 0.24.1
$ bazel build //hello
# This builds a 'hello world' cpp file.
# This rule also downloads some rules, would be nice to avoid it as
INFO: Build completed successfully, 6 total actions well
$ bazel test --nofetch //hello:hello_test
ERROR: /home/erenon/.cache/bazel/_bazel_erenon/afacf41c7e3fc3f4ea7510d344b4de38/external/bazel_tools/tools/jdk/BUILD:218:1: no such package '@remote_java_tools//': to fix, run
        bazel fetch //...
External repository @remote_java_tools not found and fetching repositories is disabled. and referenced by '@bazel_tools//tools/jdk:JacocoCoverageRunner'
ERROR: Analysis of target '//hello:hello_test' failed; build aborted: no such package '@remote_java_tools//': to fix, run
        bazel fetch //...
External repository @remote_java_tools not found and fetching repositories is disabled.
INFO: Elapsed time: 0.160s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (3 packages loaded, 27 targets configured)
FAILED: Build did NOT complete successfully (3 packages loaded, 27 targets configured)

I get the same results with a more intricate command line:

$ bazel test --nofetch --define=ABSOLUTE_JAVABASE=/usr/lib/jvm/java-8-openjdk-amd64 --host_javabase=@local_jdk//:jdk --javabase=@local_jdk//:jdk --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 //hello:hello_test
INFO: Build options --define, --host_java_toolchain, --host_javabase, and 2 more have changed, discarding analysis cache.
ERROR: /home/erenon/.cache/bazel/_bazel_erenon/afacf41c7e3fc3f4ea7510d344b4de38/external/bazel_tools/tools/jdk/BUILD:218:1: no such package '@remote_java_tools//': to fix, run
        bazel fetch //...
External repository @remote_java_tools not found and fetching repositories is disabled. and referenced by '@bazel_tools//tools/jdk:JacocoCoverageRunner'
ERROR: Analysis of target '//hello:hello_test' failed; build aborted: no such package '@remote_java_tools//': to fix, run
        bazel fetch //...
External repository @remote_java_tools not found and fetching repositories is disabled.
INFO: Elapsed time: 0.215s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 50 targets configured)
FAILED: Build did NOT complete successfully (0 packages loaded, 50 targets configured)

I tried every combination of the java flags, no luck. I also tried to enable every incompatible change (--all_incompatible_changes), which yields to a different result:

INFO: Build options --incompatible_allow_python_version_transitions, --incompatible_auto_configure_host_platform, --incompatible_cc_coverage, and 24 more have changed, discarding analysis cache.
ERROR: While resolving toolchains for target //hello:hello_test: com.google.devtools.build.lib.packages.BuildFileNotFoundException: no such package '@local_config_platform//': to fix, run
        bazel fetch //...
External repository @local_config_platform not found and fetching repositories is disabled.
ERROR: Analysis of target '//hello:hello_test' failed; build aborted: com.google.devtools.build.lib.packages.BuildFileNotFoundException: no such package '@local_config_platform//': to fix, run
        bazel fetch //...
External repository @local_config_platform not found and fetching repositories is disabled.
INFO: Elapsed time: 0.228s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)

It now looks for @local_config_platform.

 - Is there a bazel target which I can build from source and includes these dependencies?
 - Is there a different command line, which enables bazel test --nofetch?


Thanks.

László Csomor

unread,
May 2, 2019, 2:55:14 AM5/2/19
to Benedek Thaler, Tobias Werth, bazel-discuss
/cc +Tobias Werth 


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAP1yG%3Drs%2B8K_wTWo1EMPgpS8iJ5Z5u7RMRzh0Mp6gKSLsikfow%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Werth

unread,
May 2, 2019, 3:51:40 AM5/2/19
to László Csomor, Irina Iancu, Benedek Thaler, bazel-discuss
+Irina Iancu is working on the java tools.

Patrick

unread,
Dec 15, 2020, 11:46:13 AM12/15/20
to bazel-discuss
Is there any progress on this? I could use help with this as well in bazel version3.7.1 to not only be able to use local java runtime during tests but also to use a different version of java other than java 11.

Thanks in advance

Tobias Werth

unread,
Dec 15, 2020, 2:55:27 PM12/15/20
to Patrick, Ivo List, bazel-discuss

Ivo List

unread,
Dec 17, 2020, 11:43:59 AM12/17/20
to Tobias Werth, Patrick, bazel-discuss
Hey Patrick,

If I understand correctly your use case is: "always use remote JDK (not local)"
For bazel 3.7.1, command line options should give you that:
bazel build --javabase=@bazel_tools//tools/jdk:remote_jdk11 --host_javabase=@bazel_tools//tools/jdk:remote_jdk11 --java_toolchain=@bazel_tools//tools/jdk:toolchain --host_java_toolchain=@bazel_tools//tools/jdk:toolchain

I verified that it works.

For bazel after 4.0.0, the interface is going to be:
bazel build --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_11

Second use case you mention: use a different version of Java
Bazel 3.7.1 (has definitions for JDK14) and Bazel 4.0.0 also has definitions for JDK15.
bazel build --javabase=@remotejdk14_linux//:jdk --host_javabase=@remotejdk14_linux//:jdk --java_toolchain=@remote_java_tools_linux//:toolchain_jdk_14 --host_java_toolchain=@remote_java_tools_linux//:toolchain_jdk_14

For bazel after 4.0.0, the interface is going to be:
bazel build --java_runtime_version=remotejdk_15 --tool_java_runtime_version=remotejdk_15

For other Java versions you need to import your own JDK repos. 

Use case that was mentioned previously in the thread: only use local JDK never download remote JDKs.

Bazel 3.7.1: 
bazel build --javabase=@local_jdk//:jdk --host_javabase=@local_jdk//:jdk --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8  --|grep remote
host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
(or remove "_hostjdk8" if you have JDK>=8)
will compile using local JDK, but it will still download @remote_java_tools repository, because Bazel needs those tools.

Bazel >4.0.0 will need a custom toolchain definition for such usecase.
More on: 

Does this answer your questions?

Regards,
Ivo
--

Ivo List | Software Engineer | il...@google.com | 

Patrick

unread,
Dec 21, 2020, 3:05:42 PM12/21/20
to bazel-discuss
Unfortunately no, thank you for the documentation though it will be really helpfully. While those options work during a java build the issue actually comes in when I tried to run tests in a cpp application. an example can be found at https://gitlab.com/phamod/bazel-example the command `bazel test //hello_world:testbar` it then proceeds to download remote jdk 11 instead of my local java version to run coverage tool.

Right now I'm trying to see how bazel works in an environment that's not connected to the internet and the environment I don't have control over some the infrastructure but I do have a local gitlab server that I can use. You may need to mess with the work space removing the local repositories to make it build those will be replaced with http in the real thing. I'll make a simple example with gtest instead qtest so that using the example is easier without having to install qt.

Respectfully,
Patrick

Patrick

unread,
Dec 21, 2020, 3:05:50 PM12/21/20
to bazel-discuss
I'm trying to run bazel from an offline environment and the issue comes when I try to run bazel test or bazel coverage it tries to pull down remote_jdk11 when I try to test my C++ code. I have an example located on gitlab to show issue: https://gitlab.com/phamod/bazel-example then I run the command 'bazel test //hello_world:testbar'. Sorry I tried to respond earlier but I don't have the best internet so my first response may or may not have gone through. my bazelrc file is also included that works on build commands.

Thanks,
Patrick

On Thursday, December 17, 2020 at 11:43:59 AM UTC-5 Ivo List wrote:
Reply all
Reply to author
Forward
0 new messages