salesforce / rules_spring

Bazel rule for building Spring Boot apps as a deployable jar
BSD 3-Clause "New" or "Revised" License
226 stars 49 forks source link

Could not find or load main class org.springframework.boot.loader.JarLauncher #184

Open donkahlero opened 8 months ago

donkahlero commented 8 months ago

Hello dear maintanance team,

currently, I am trying to move away from a gradle-base Spring Cloud Functions build towards a bazel build.

My configuration looks as follows:

MODULES.bzl

[...]
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
    artifacts = [
        "com.amazonaws:aws-lambda-java-events:3.11.1",
        "com.fasterxml.jackson.core:jackson-annotations:2.16.1",
        "com.google.code.gson:gson:2.10.1",
        "io.swagger.core.v3:swagger-annotations:2.2.18",
        "jakarta.validation:jakarta.validation-api:3.0.2",
        "org.apache.logging.log4j:log4j-core:2.20.0",
        "org.mapstruct:mapstruct-processor:1.5.5.Final",
        "org.mapstruct:mapstruct:1.5.5.Final",
        "org.projectlombok:lombok:1.18.30",
        "org.springframework.boot:spring-boot-starter-web:3.1.2",
        "org.springframework.cloud:spring-cloud-function-adapter-aws:4.0.4",
        "org.springframework.cloud:spring-cloud-function-context:4.0.4",
        "software.amazon.awssdk:lambda:2.23.2",
    ],
    fetch_sources = True,
    repositories = [
        "https://some_internal_maven_mirror",
    ],
)
use_repo(maven, "maven")
[...]

BUILD

load("@rules_spring//springboot:springboot.bzl", "springboot")
[...]
deps = [
    "@maven//:com_amazonaws_aws_lambda_java_events",
    "@maven//:com_google_code_gson_gson",
    "@maven//:jakarta_validation_jakarta_validation_api",
    "@maven//:org_apache_logging_log4j_log4j_api",
    "@maven//:org_apache_logging_log4j_log4j_core",
    "@maven//:org_springframework_boot_spring_boot",
    "@maven//:org_springframework_boot_spring_boot_autoconfigure",
    "@maven//:org_springframework_boot_spring_boot_starter_web",
    "@maven//:org_springframework_cloud_spring_cloud_function_adapter_aws",
    "@maven//:org_springframework_cloud_spring_cloud_function_context",
    "@maven//:org_springframework_spring_context",
    "@maven//:org_springframework_spring_core",
    "@maven//:org_springframework_spring_web",
    "@maven//:software_amazon_awssdk_regions",
]

java_library(
    name = "_function",
    srcs = [...],
    deps = [SOME_INTERNAL_DEPS] + deps,
    resources = glob(["src/main/resources/**"]),
)

springboot(
    name = "function",
    boot_app_class = "path.to.my.main.Class",
    java_library = ":_function",
)

However, if try to execute my spring application, I am getting this result:

Error: Could not find or load main class org.springframework.boot.loader.JarLauncher
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.loader.JarLauncher

I saw that there has been an issue in the past (https://github.com/salesforce/rules_spring/issues/13) but that has been marked resolved after some PRs.

What I am doing wrong here? What am I missing? Are Spring Cloud Functions even supported?

Cheers and thanks for your help! Jonas

plaird commented 8 months ago

rules_spring took a BYOSD (bring your own spring dependencies) approach because we didn't want to lock you into a particular version of anything. We require you to add all the required Spring/SpringBoot dependencies to the deps list.

This includes the foundation Spring Boot "loader" jar which knows how to launch a Spring Boot app. The problem is you are missing that.

You are missing this lib from your deps list: org.springframework.boot:spring-boot-loader

Or, for convenience, we have an "import bundle" that defines the min set of dependencies: Spring Boot import bundle example

Please use one of the above solution and let us know how it goes.

plaird commented 8 months ago

(I revised the title of this Issue to reflect the actual error. I don't think this is related to Spring Cloud at all)

donkahlero commented 8 months ago

Hi @plaird, thanks for getting back to me. I actually found that out myself, but you were slightly faster than me in answering. Sorry for that!

However, there is some other issue with the spring cloud functions deployment on AWS that I am investigating at the moment and I will update this issue for anyone that is interested in this as soon as I know more.

plaird commented 8 months ago

FYI: If you arrive here by looking at the title, and are using Spring Boot 3, the issue you are facing might be: https://github.com/salesforce/rules_spring/issues/177