Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Bazel's JVM to run Coursier #1046

Open
odisseus opened this issue Jan 22, 2024 · 3 comments
Open

Use Bazel's JVM to run Coursier #1046

odisseus opened this issue Jan 22, 2024 · 3 comments

Comments

@odisseus
Copy link

odisseus commented Jan 22, 2024

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 odisseus/java-ipfs-http-client@4de9831.)

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.

@jin
Copy link
Member

jin commented Jan 25, 2024

Yes, this is a reasonable FR. Contributions are welcome.

@honnix
Copy link
Contributor

honnix commented Feb 27, 2024

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.

@honnix
Copy link
Contributor

honnix commented Feb 27, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants