Bazel rules for building Spring applications with Ahead-of-Time (AOT) compilation support. These rules enable you to leverage Spring's AOT processing capabilities within your Bazel build, resulting in faster startup times and reduced memory footprint for your Spring applications.
Add to your MODULE.bazel:
bazel_dep(name = "rules_spring_aot", version = "0.1.0")
From the release you wish to use:
https://github.com/tharakadesilva/rules_spring_aot/releases
copy the WORKSPACE snippet into your WORKSPACE file.
To use a commit rather than a release, you can point at any SHA of the repo.
For example to use commit abc123:
url = "https://github.com/tharakadesilva/rules_spring_aot/releases/download/v0.1.0/rules_spring_aot-v0.1.0.tar.gz" with a GitHub-provided source archive like url = "https://github.com/tharakadesilva/rules_spring_aot/archive/abc123.tar.gz"strip_prefix = "rules_spring_aot-0.1.0" with strip_prefix = "rules_spring_aot-abc123"sha256. The easiest way to do this is to comment out the line, then Bazel will
print a message with the correct value.load("@rules_spring_aot//spring_aot:defs.bzl", "spring_aot_library")
load("@rules_java//java:defs.bzl", "java_library", "java_binary")
# Define your Spring Boot application
java_library(
name = "my-app-lib",
srcs = glob(["src/main/java/**/*.java"]),
runtime_deps = [
"@maven//:org_springframework_boot_spring_boot_starter_web",
],
deps = [
# ... other dependencies
],
)
# Apply AOT processing
# Creates a java_library containing all AOT-generated classes and resources
spring_aot_library(
name = "my-app-aot",
target = ":my-app-lib",
main_class = "com.example.MyApplication",
group_id = "com.example",
artifact_id = "my-app",
)
# Create executable JAR
# Produces a standard JAR with improved startup time (faster than regular Spring Boot, slower than native)
java_binary(
name = "my-app",
main_class = "com.example.MyApplication",
runtime_deps = [":my-app-aot"],
jvm_flags = ["-Dspring.aot.enabled=true"],
)
See rules_graalvm for setup instructions.
load("@rules_spring_aot//spring_aot:defs.bzl", "spring_aot_library")
load("@rules_graalvm//graalvm:defs.bzl", "native_image")
# ... (previous definitions)
# Build native executable
native_image(
name = "my-app-native",
main_class = "com.example.MyApplication",
native_image_tool = "@graalvm//:native-image",
deps = [":my-app-aot"],
)
org.springframework.boot/spring-boot must be present in the target java_library's classpath