Skip to content

Commit e2610cd

Browse files
authored
Merge pull request #172 from salesforce/plaird/bazelruntoolchain
add bazelrun_java_toolchain attribute to launch service with the java toolchain JVM
2 parents 63f0053 + d333d96 commit e2610cd

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

springboot/default_bazelrun_script.sh

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

25-
# soon we will use one of the jdk locations already known to Bazel, see Issue #16
26-
if [ -z ${JAVA_HOME} ]; then
27-
java_cmd="$(which java)"
28-
else
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
31+
echo "Selected the JVM using the BAZEL_RUN_JAVA environment variable."
32+
java_cmd=$BAZEL_RUN_JAVA
33+
elif [ -f "$JAVA_TOOLCHAIN" ]; then
34+
echo "Selected the JVM using the bazelrun_java_toolchain attribute on the springboot rule."
35+
java_cmd=$JAVA_TOOLCHAIN
36+
elif [ -d "${JAVA_HOME}" ]; then
37+
echo "Selected the JVM using the JAVA_HOME environment variable."
2938
java_cmd="${JAVA_HOME}/bin/java"
39+
else
40+
echo "Selected the JVM by executing 'which java'"
41+
java_cmd="$(which java)"
3042
fi
3143

3244
if [ -z "${java_cmd}" ]; then

springboot/springboot.bzl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,22 @@ set -e
225225
# SCRIPT_DIR is the directory in which bazel run executes
226226
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
227227
228+
# Detect Java Toolchain (optional)
229+
# If the bazelrun_java_toolchain attribute on the springboot rule is set, the springboot
230+
# rule impl below will replace the java_toolchain_attr tag with the relative path
231+
# from exec root to the JVM. We have to do some Bash hijinks to resolve the
232+
# absolute path.
233+
JAVA_TOOLCHAIN_RELATIVE=%java_toolchain_attr%
234+
if [ -z ${JAVA_TOOLCHAIN_RELATIVE+x} ]; then
235+
# the springboot rule did not set the bazelrun_java_toolchain attribute
236+
unset JAVA_TOOLCHAIN
237+
else
238+
# the springboot rule did set the bazelrun_java_toolchain attribute, convert
239+
# it into an absolute path using exec_root
240+
exec_root=${SCRIPT_DIR%%bazel-out*}
241+
JAVA_TOOLCHAIN="${exec_root}${JAVA_TOOLCHAIN_RELATIVE}"
242+
fi
243+
228244
# the env variables file is found in SCRIPT_DIR
229245
source $SCRIPT_DIR/bazelrun_env.sh
230246
@@ -248,9 +264,23 @@ def _springboot_rule_impl(ctx):
248264
])
249265

250266
# resolve the full path of the launcher script that runs "java -jar <springboot.jar>" when calling
251-
# "bazel run" with the springboot target (bazel run //examples/helloworld)
267+
# "bazel run" with the springboot target (bazel run //examples/helloworld) and string sub it
268+
# into the _bazelrun_script_template text defined above
252269
outer_bazelrun_script_contents = _bazelrun_script_template \
253270
.replace("%bazelrun_script%", ctx.attr.bazelrun_script.files.to_list()[0].path)
271+
272+
# the bazelrun_java_toolchain optional, if set, we use it as the jvm for bazel run
273+
if ctx.attr.bazelrun_java_toolchain != None:
274+
# lookup the path to selected java toolchain, and string sub it into the bazel run script
275+
# text _bazelrun_script_template defined above
276+
java_runtime = ctx.attr.bazelrun_java_toolchain[java_common.JavaToolchainInfo].java_runtime
277+
java_bin = [f for f in java_runtime.files.to_list() if f.path.endswith("bin/java")][0]
278+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
279+
.replace("%java_toolchain_attr%", java_bin.path)
280+
else:
281+
outer_bazelrun_script_contents = outer_bazelrun_script_contents \
282+
.replace("%java_toolchain_attr%", "")
283+
254284
outer_bazelrun_script_file = ctx.actions.declare_file("%s" % ctx.label.name)
255285
ctx.actions.write(outer_bazelrun_script_file, outer_bazelrun_script_contents, is_executable = True)
256286

@@ -286,6 +316,11 @@ _springboot_rule = rule(
286316

287317
"bazelrun_script": attr.label(allow_files=True),
288318
"bazelrun_data": attr.label_list(allow_files=True),
319+
320+
"bazelrun_java_toolchain": attr.label(
321+
mandatory = False,
322+
providers = [java_common.JavaToolchainInfo],
323+
),
289324
},
290325
)
291326

@@ -306,6 +341,7 @@ def springboot(
306341
javaxdetect_enable = None,
307342
javaxdetect_ignorelist = None,
308343
include_git_properties_file=True,
344+
bazelrun_java_toolchain = None,
309345
bazelrun_script = None,
310346
bazelrun_jvm_flags = None,
311347
bazelrun_data = None,
@@ -357,6 +393,7 @@ def springboot(
357393
javaxdetect_ignorelist: Optional. When using the javax detect check, this attribute provides a file
358394
that contains a list of libraries excluded from the analysis. Ex: *javaxdetect_ignorelist.txt*
359395
include_git_properties_file: If *True*, will include a git.properties file in the resulting jar.
396+
bazelrun_java_toolchain: Optional. Label to the Java toolchain to use when launching the application using 'bazel run'
360397
bazelrun_script: Optional. When launching the application using 'bazel run', a default launcher script is used.
361398
This attribute can be used to provide a customized launcher script. Ex: *my_custom_script.sh*
362399
bazelrun_jvm_flags: Optional. When launching the application using 'bazel run', an optional set of JVM flags
@@ -573,6 +610,7 @@ def springboot(
573610
_springboot_rule(
574611
name = name,
575612
app_compile_rule = java_library,
613+
bazelrun_java_toolchain = bazelrun_java_toolchain,
576614
dep_aggregator_rule = ":" + dep_aggregator_rule,
577615
genmanifest_rule = ":" + genmanifest_rule,
578616
genbazelrunenv_rule = ":" + genbazelrunenv_rule,

springboot/springboot_doc.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Note that the rule README has more detailed usage instructions for each attribut
3434
| dupeclassescheck_enable | If *True*, will analyze the list of dependencies looking for any class that appears more than once, but with a different hash. This indicates that your dependency tree has conflicting libraries. | <code>None</code> |
3535
| dupeclassescheck_ignorelist | Optional. When using the duplicate class check, this attribute provides a file that contains a list of libraries excluded from the analysis. Ex: *dupeclass_libs.txt* | <code>None</code> |
3636
| include_git_properties_file | If *True*, will include a git.properties file in the resulting jar. | <code>True</code> |
37+
| bazelrun_java_toolchain | Optional. When launching the application using 'bazel run', this attribute can identify the label of the Java toolchain used to launch the JVM. Ex: *//tools/jdk:my_default_toolchain*. See *default_java_toolchain* in the Bazel documentation. | <code>None</code> |
3738
| bazelrun_script | Optional. When launching the application using 'bazel run', a default launcher script is used. This attribute can be used to provide a customized launcher script. Ex: *my_custom_script.sh* | <code>None</code> |
3839
| bazelrun_jvm_flags | Optional. When launching the application using 'bazel run', an optional set of JVM flags to pass to the JVM at startup. Ex: *-Dcustomprop=gold -DcustomProp2=silver* | <code>None</code> |
3940
| bazelrun_data | Uncommon option to add data files to runfiles. Behaves like the *data* attribute defined for *java_binary*. | <code>None</code> |
@@ -49,5 +50,3 @@ Note that the rule README has more detailed usage instructions for each attribut
4950
| duplicate_class_allowlist | Deprecated synonym of *dupeclassescheck_ignorelist* | <code>None</code> |
5051
| jvm_flags | Deprecated synonym of *bazelrun_jvm_flags* | <code>""</code> |
5152
| data | Deprecated synonym of *bazelrun_data* | <code>[]</code> |
52-
53-

0 commit comments

Comments
 (0)