spring-cloud-function icon indicating copy to clipboard operation
spring-cloud-function copied to clipboard

Spring Cloud Function AWS example works for Maven build but not Gradle

Open LowBudgetMan opened this issue 5 years ago • 3 comments

I pulled the sample projects locally and built the aws example project with maven. I uploaded the example to an AWS Lambda and found the application ran perfectly fine. When I built the example with gradle and uploaded it to the same function I get a NullPointerException.

Gradle Version: 4.8.1 Maven Version: 3.6.1 Java Version: 1.8.0_212

Steps to reproduce:

  1. pull the v3.0.0.M2 branch
  2. run mvn clean install on the function-sample-aws project
  3. upload the *-aws.jar file from /target to an AWS Lambda.
  4. See successful output when run
  5. run ./gradlew clean build -x test on the function-sample-aws project
  6. upload the *-aws.jar file from /build/libs to the same AWS Lambda.
  7. See below error

AWS Error Message:

{
  "errorMessage": "java.lang.NullPointerException",
  "errorType": "java.lang.NullPointerException",
  "stackTrace": [
    "org.springframework.cloud.function.context.FunctionalSpringApplication.postProcessApplicationContext(FunctionalSpringApplication.java:102)",
    "org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:372)",
    "org.springframework.boot.SpringApplication.run(SpringApplication.java:325)",
    "org.springframework.cloud.function.adapter.aws.SpringFunctionInitializer.initialize(SpringFunctionInitializer.java:86)",
    "org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:46)"
  ]
}

AWS Logs:

START RequestId: 7169b189-bb18-454a-8d7c-de2b1af12dc1 Version: $LATEST
21:09:25.778 [main] INFO org.springframework.cloud.function.adapter.aws.SpringFunctionInitializer - Initializing: null

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                        

2019-08-22 21:09:31.551 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.NullPointerException: null
	at org.springframework.cloud.function.context.FunctionalSpringApplication.postProcessApplicationContext(FunctionalSpringApplication.java:102) ~[task/:na]
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:372) ~[task/:na]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) ~[task/:na]
	at org.springframework.cloud.function.adapter.aws.SpringFunctionInitializer.initialize(SpringFunctionInitializer.java:86) [task/:na]
	at org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:46) [task/:na]
	at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64) [LambdaSandboxJava-1.0.jar:na]
	at java.lang.Class.forName0(Native Method) [na:1.8.0_201]
	at java.lang.Class.forName(Class.java:348) [na:1.8.0_201]
	at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:114) [LambdaJavaRTEntry-1.0.jar:na]

java.lang.NullPointerException: java.lang.NullPointerException
java.lang.NullPointerException
	at org.springframework.cloud.function.context.FunctionalSpringApplication.postProcessApplicationContext(FunctionalSpringApplication.java:102)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:372)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:325)
	at org.springframework.cloud.function.adapter.aws.SpringFunctionInitializer.initialize(SpringFunctionInitializer.java:86)
	at org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:46)

END RequestId: 7169b189-bb18-454a-8d7c-de2b1af12dc1
REPORT RequestId: 7169b189-bb18-454a-8d7c-de2b1af12dc1	Duration: 6087.57 ms	Billed Duration: 6100 ms 	Memory Size: 512 MB	Max Memory Used: 79 MB	

Unrelated but something else I noticed, MapTests.java fails from a NoSuchMethodError (see below). That's why I skipped the tests when building with gradle but did not when running maven.

Failed Test:

    org.springframework.beans.factory.BeanCreationException at MapTests.java:41
        Caused by: java.lang.NoSuchMethodError at MapTests.java:41

2 tests completed, 1 failed

LowBudgetMan avatar Aug 22 '19 21:08 LowBudgetMan

@thenewimagineer this may be related to a stale gradle file. See the PR i just merged - https://github.com/spring-cloud/spring-cloud-function/commit/971caf184d753c930c3a82d9c0577f866ff3e88d

In other words you have to until M3, you'd need to use snapshot

olegz avatar Aug 23 '19 03:08 olegz

@olegz Thanks for the quick response! Do you know what version has a working gradle example?

Also I pulled the commit and got a different error this time

Error

{
  "errorMessage": "Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).",
  "errorType": "java.lang.IllegalStateException"
}

Logs

START RequestId: 4ec62ec3-4888-4964-892c-2ea649605c17 Version: $LATEST
Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).: java.lang.IllegalStateException
java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).
	at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.getStartClass(AbstractSpringFunctionAdapterInitializer.java:288)
	at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.<init>(AbstractSpringFunctionAdapterInitializer.java:100)
	at org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.<init>(SpringBootRequestHandler.java:44)
	at example.Handler.<init>(Handler.java:25)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
Caused by: java.lang.IllegalArgumentException: Failed to locate main class
	at org.springframework.util.Assert.notNull(Assert.java:198)
	at org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.getStartClass(AbstractSpringFunctionAdapterInitializer.java:284)
	... 7 more

END RequestId: 4ec62ec3-4888-4964-892c-2ea649605c17
REPORT RequestId: 4ec62ec3-4888-4964-892c-2ea649605c17	Duration: 32.24 ms	Billed Duration: 100 ms 	Memory Size: 512 MB	Max Memory Used: 31 MB	

LowBudgetMan avatar Aug 23 '19 14:08 LowBudgetMan

@LowBudgetMan - i had a similar error...you can add an environment variable to indicate main class: main

jeffrey-kinesso avatar Sep 25 '20 20:09 jeffrey-kinesso

It's been fixed - https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-aws

olegz avatar Sep 26 '23 12:09 olegz