rules_jvm_external
rules_jvm_external copied to clipboard
Use Bazel's JVM to run Coursier
It is possible to run Bazel and even build Java projects without installing Java in the system or defining JAVA_HOME
. For example, consider this dockerfile:
FROM ubuntu:latest
RUN <<EOF
apt-get -y update
apt-get -y install curl git gcc
curl -LsO https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64
mv bazelisk-linux-amd64 /bin/bazel
chmod +x /bin/bazel
EOF
ENTRYPOINT /bin/bash
This container has everything that's necessary to run Bazel, because Bazelisk is able to download a suitable JVM. To build a Java target, you'd need to provide additional Bazel flags:
bazel build --javabase=@bazel_tools//tools/jdk:remote_jdk11 --java_runtime_version=remotejdk_11 //src/main/...
It this environment, the above command is able to compile Java sources. (I have been experimenting with https://github.com/odisseus/java-ipfs-http-client/commit/4de98315e4b1267110db990493eaad643047e0a2.)
However, if there's a dependency on a @maven//
target, the build fails with an error like this:
bazel build --javabase=@bazel_tools//tools/jdk:remote_jdk11 --java_runtime_version=remotejdk_11 //src/test/...
// snip...
ERROR: /build/java-ipfs-http-client/BUILD.bazel:1:14:
//:maven_test_deps depends on @maven//:junit_junit in repository @maven which failed to fetch. no such package '@maven//':
Unable to run coursier:
/root/.cache/bazel/_bazel_root/3f57bb81dd178514a6db80432bcb53b1/external/maven/coursier: 26: exec: java: not found
Apparently Coursier expects Java to be installed (i.e. present in PATH
). However, I don't see why this should be necessary.
Consider that there is already some JVM present in the system (the one which Bazel uses to run its own code), and we know were to find it. For the purpose of running Coursier, it does not matter at all what version it is, as long as it is able to launch the Coursier JAR and have the dependencies downloaded.
By the way, this will probably solve #770.
Yes, this is a reasonable FR. Contributions are welcome.
Maybe it is better to use the resolved runtime toolchain ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
?
As commented in https://github.com/bazelbuild/rules_jvm_external/issues/445#issuecomment-674437363, the embedded jdk may not be suitable.
Maybe it is better to use the resolved runtime toolchain
ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime
?As commented in #445 (comment), the embedded jdk may not be suitable.
NVM this seems to be a repository_rule.