If you cannot remove a dependency, you can influence which version of a duplicate class 'wins' by controlling the classpath order. In a Spring Boot executable jar, the order of entries determines which class is loaded first by the JVM.
Method 1: BUILD file dependency order
Spring Boot writes dependencies into the jar based on Bazel's depset order, which is influenced by the order in your deps list. Earlier entries in the deps list are generally loaded first.
Note: If you encounter errors with very large dependency sets (command line length limits), you can disable this ordering behavior by setting deps_use_starlark_order = False in the springboot rule.
To verify the order in your jar, use: jar -tvf {springboot_jar_name}.
Method 2: Spring Boot Classpath Index
You can provide a deps_index_file to the springboot rule to instruct the Spring Boot loader to load specific jars in a specific order.
⚠️ Caveat: This only works if you explode the executable jar and invoke the JarLauncher manually:
jar -xf helloworld.jar
java org.springframework.boot.loader.JarLauncher
springboot(
name = "helloworld",
boot_app_class = "com.sample.SampleMain",
java_library = ":helloworld_lib",
# if you have conflicting classes in dependency jar files, you can define the order in which the jars are loaded
deps_index_file = "helloworld_classpath.idx",
)