David Lee
David Lee

Reputation: 41

bazel run works, but building deploy JAR fails due to missing dependencies

Java/Bazel noobie here. I’ve got a project using Spring Boot (and managing external dependencies with rules_jvm_external).

I’m able to successfully run bazel run, but when I try building a _deploy.jar, the application fails with the following error:

11:48:11.913 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.flexport.originops.OriginOpsApplication.main(OriginOpsApplication.java:18)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
    ... 8 common frames omitted  

It seems as if some dependencies aren't being picked up with the deployable JAR, but are getting picked up when I run bazel run.

Here's my (truncated) BUILD file:

filegroup(
    name = "java_files",
    srcs = glob(["src/main/java/**/*.java"]),
)
filegroup(
    name = "java_test_files",
    srcs = glob(["src/test/java/com/**/*.java"]),
)
java_library(
    name = "lib",
    srcs = [
        ":java_files",
    ],
    resources = glob(["src/main/resources/**"]),
    deps = [
        # Plugins and internal dependencies
        "//build-utils/lombok",
        "//build-utils/mapstruct",
        ...
        # External dependencies
        "@com_google_protobuf//:protobuf_java",
        "@maven//:com_baomidou_mybatis_plus_annotation",
        "@maven//:com_baomidou_mybatis_plus_boot_starter",
        "@maven//:com_baomidou_mybatis_plus_core",
        "@maven//:com_baomidou_mybatis_plus_extension",
        "@maven//:com_fasterxml_jackson_core_jackson_annotations",
        "@maven//:com_fasterxml_jackson_core_jackson_core",
        "@maven//:com_fasterxml_jackson_core_jackson_databind",
        "@maven//:com_fasterxml_jackson_module_jackson_module_jsonSchema",
        "@maven//:com_github_rholder_guava_retrying",
        "@maven//:com_google_code_findbugs_jsr305",
        "@maven//:com_google_guava_guava",
        "@maven//:com_hubspot_HorizonCore",
        "@maven//:com_hubspot_algebra",
        "@maven//:com_hubspot_slack_slack_base",
        "@maven//:com_hubspot_slack_slack_java_client",
        "@maven//:io_springfox_springfox_bean_validators",
        "@maven//:io_springfox_springfox_core",
        "@maven//:io_springfox_springfox_spi",
        "@maven//:io_springfox_springfox_spring_web",
        "@maven//:io_springfox_springfox_swagger2",
        "@maven//:io_springfox_springfox_swagger_ui",
        "@maven//:io_swagger_swagger_annotations",
        "@maven//:jakarta_validation_jakarta_validation_api",
        "@maven//:javax_validation_validation_api",
        "@maven//:org_apache_tomcat_tomcat_annotations_api",
        "@maven//:org_aspectj_aspectjweaver",
        "@maven//:org_flywaydb_flyway_core",
        "@maven//:org_javassist_javassist",
        "@maven//:org_mapstruct_mapstruct",
        "@maven//:org_mapstruct_mapstruct_processor",
        "@maven//:org_mybatis_mybatis",
        "@maven//:org_mybatis_mybatis_spring",
        "@maven//:org_postgresql_postgresql",
        "@maven//:org_reflections_reflections",
        "@maven//:org_slf4j_slf4j_api",
        "@maven//:org_springframework_boot_spring_boot",
        "@maven//:org_springframework_boot_spring_boot_autoconfigure",
        "@maven//:org_springframework_boot_spring_boot_configuration_processor",
        "@maven//:org_springframework_boot_spring_boot_devtools",
        "@maven//:org_springframework_boot_spring_boot_starter_actuator",
        "@maven//:org_springframework_boot_spring_boot_starter_aop",
        "@maven//:org_springframework_boot_spring_boot_starter_web",
        "@maven//:org_springframework_spring_beans",
        "@maven//:org_springframework_spring_context",
        "@maven//:org_springframework_spring_tx",
        "@maven//:org_springframework_spring_web",
        "@maven//:org_springframework_spring_webmvc",
    ],
)
java_binary(
    # not the real names
    name = "app",
    main_class = "com.my.org.Application",
    runtime_deps = [
        ":lib",
    ],
)

Taking a wild stab in the dark, I think it might be related to the Java plugins my app depends on, but I'm not sure why that would cause Spring Boot to fail to start up. Anyone know what might be going on?

Upvotes: 1

Views: 1053

Answers (1)

thundergolfer
thundergolfer

Reputation: 547

Have you tried https://github.com/salesforce/bazel-springboot-rule/ ?

I was able to get a 'hello world' SpringBoot app going with rules_jvm_external here: https://github.com/thundergolfer/the-one-true-bazel-monorepo/commits/jonathon/spring-boot-backend

I posted an issue about rules_jvm_external compatibility with github.com/salesforce/bazel-springboot-rule here: https://github.com/salesforce/bazel-springboot-rule/issues

Upvotes: 2

Related Questions