Skip to content

Commit 648b510

Browse files
authored
Merge pull request #173 from salesforce/plaird/improve_bazelrun_toolchain
improve default behavior of bazel run jvm
2 parents e2610cd + 97498dd commit 648b510

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

springboot/bazelrun.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ But you may wish to customize the launch.
1010
The *springboot* rule supports several features for customization.
1111
Note that these features do **not** apply when running the application directly using ```java -jar [file]```.
1212

13+
### Launcher JVM
14+
15+
By default, the service will be started using the JVM from the current Java toolchain in your
16+
Bazel workspace - `@bazel_tools//tools/jdk:current_java_toolchain`
17+
See the [Bazel Java docs](https://bazel.build/docs/bazel-and-java) on how toolchains are defined.
18+
However, there are multiple ways to override this.
19+
20+
First, you can set the `BAZEL_RUN_JAVA` environment variable to the Java executable of your choice.
21+
This works well for local overrides, when you want to test new Java distributions.
22+
This variable, when set, takes priority over the toolchain configurations.
23+
24+
```
25+
export BAZEL_RUN_JAVA=/opt/my_jdk/bin/java
26+
```
27+
28+
Second, you can use the `bazelrun_java_toolchain` to pass the label to a Java toolchain
29+
defined in your Bazel workspace.
30+
The bazel run script will use the JVM from the toolchain.
31+
32+
```
33+
springboot(
34+
...
35+
bazelrun_java_toolchain = "//tools/jdk:my_default_toolchain",
36+
)
37+
```
38+
39+
Finally, you can provide a custom launcher script (see below) that can tailor JVM selection as needed.
40+
This is the most flexible option, but it may cause compatibility issues with newer versions of rules_spring.
41+
42+
1343
### Java Startup Options
1444

1545
You may wish to customize the bazel run launcher with JVM options.

springboot/default_bazelrun_script.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ set -e
2222
# versions of Bazel because they are documented:
2323
# https://docs.bazel.build/versions/master/user-manual.html#run
2424

25-
# Picking the Java VM to run is a bit of an ordeal.
26-
# 1. honor any environmental variable BAZEL_RUN_JAVA (optional)
27-
# 2. use the java executable from the java toolchain passed into the rule (optional)
28-
# 3. use non-hermetic JAVA_HOME
29-
# 4. as a last resort, use 'which java'
30-
if [ -d "${BAZEL_RUN_JAVA}" ]; then
25+
# Picking the Java VM to run is a bit of an ordeal. We now default to using the
26+
# JVM from @bazel_tools//tools/jdk:current_java_toolchain. But you can override that.
27+
# 1. honor any environmental variable BAZEL_RUN_JAVA (optional)
28+
# 2. use the java executable from the java toolchain passed into the rule (default)
29+
# 3. use non-hermetic JAVA_HOME (dead code, we no longer allow this)
30+
# 4. as a last resort, use 'which java' (dead code, we no longer allow this)
31+
if [ -f "${BAZEL_RUN_JAVA}" ]; then
3132
echo "Selected the JVM using the BAZEL_RUN_JAVA environment variable."
3233
java_cmd=$BAZEL_RUN_JAVA
3334
elif [ -f "$JAVA_TOOLCHAIN" ]; then
34-
echo "Selected the JVM using the bazelrun_java_toolchain attribute on the springboot rule."
35+
echo "Selected the JVM using the Bazel Java toolchain: $JAVA_TOOLCHAIN_NAME"
3536
java_cmd=$JAVA_TOOLCHAIN
3637
elif [ -d "${JAVA_HOME}" ]; then
3738
echo "Selected the JVM using the JAVA_HOME environment variable."
@@ -42,7 +43,7 @@ else
4243
fi
4344

4445
if [ -z "${java_cmd}" ]; then
45-
echo "ERROR: no java found, either set JAVA_HOME or add the java executable to your PATH"
46+
echo "ERROR: no java found. See the Bazel Run docs in rules_spring for details."
4647
exit 1
4748
fi
4849
echo "Using Java at ${java_cmd}"

springboot/springboot.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ else
239239
# it into an absolute path using exec_root
240240
exec_root=${SCRIPT_DIR%%bazel-out*}
241241
JAVA_TOOLCHAIN="${exec_root}${JAVA_TOOLCHAIN_RELATIVE}"
242+
JAVA_TOOLCHAIN_NAME=%java_toolchain_name_attr%
242243
fi
243244
244245
# the env variables file is found in SCRIPT_DIR
@@ -277,6 +278,8 @@ def _springboot_rule_impl(ctx):
277278
java_bin = [f for f in java_runtime.files.to_list() if f.path.endswith("bin/java")][0]
278279
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
279280
.replace("%java_toolchain_attr%", java_bin.path)
281+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
282+
.replace("%java_toolchain_name_attr%", ctx.attr.bazelrun_java_toolchain.label.name)
280283
else:
281284
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
282285
.replace("%java_toolchain_attr%", "")
@@ -319,6 +322,7 @@ _springboot_rule = rule(
319322

320323
"bazelrun_java_toolchain": attr.label(
321324
mandatory = False,
325+
default = "@bazel_tools//tools/jdk:current_java_toolchain",
322326
providers = [java_common.JavaToolchainInfo],
323327
),
324328
},

0 commit comments

Comments
 (0)