-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use maven assembly plugin instead of shade plugin #133
Comments
Thanks for the feedback @eduramiba - I've been wondering whether it's time to build a Lambda-specific maven plugin to package an application and slim down the output jar as much as possible. I'll keep this open and think it through. I'll definitely look into adding the assembly plugin as an alternative to our samples and archetypes. |
Good to know this way to pack it. Thanks @eduramiba ! |
@eduramiba Thank you! How would you exclude the embedded tomcat in this case? |
@Juchar I guess you can use the goal options, https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html But if your dependency has |
Also, the jar manifest entries are not actually necessary in lambda runtime since it will load all jars in lib folder. |
@eduramiba Ok, thank you. I changed you approach a little bit and it is also working (you only need to configure the assembly plugin this way): <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>zip-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin> And the <assembly>
<id>aws-lambda-package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}${file.separator}classes</directory>
<outputDirectory/>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<excludes>
<exclude>org.springframework.boot:spring-boot-devtools</exclude>
<exclude>org.apache.tomcat.embed:*</exclude>
</excludes>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly> |
I've tried both the suggested examples provided. The second example puts all the dependencies in lib/ again, but at the root of the zip the directory structure; Either way, I get I've tried both ways to define the handler:
What am I missing? Background There are suggestions to remove the signatures:
but surely these will be rejected. |
Hi @eugenevd - I'm on the road right now but I'll try to replicate your issue with the Shade plugin and see if we can find a fix on Thursday. As for the assembly plugin questions, I'll let @eduramiba and @Juchar chime in since they clearly know more than I do on the topic. |
Hi @eugenevd - the assembly configuration I mention above will produce a zip file following these conventions. For me it worked out of the box. Maybe you could show us the This is the relevant part from mine: AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My Description
Resources:
MyServiceFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.my.function.StreamLambdaHandler::handleRequest
Runtime: java8
CodeUri: target/my-function-aws-lambda-package.zip
MemorySize: 768
Policies: AWSLambdaBasicExecutionRole
Timeout: 30
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: GET
Outputs:
MyFunctionAPI:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/entity'
Export:
Name: MyFunctionAPI |
per the example given by @Juchar in comment ↵aws#133 (comment) Follow the instructions to build/package/deploy/etc as per: https://github.com/awslabs/aws-serverless-java-container/wiki#deploying-the-sample-applications Doing the 'curl' results with error: "message": "Internal server error" Looking at Cloudwatch logs: "Class not found: com.amazonaws.serverless.sample.springboot.StreamLambdaHandler: java.lang.ClassNotFoundException"
per the example posted by eduramiba (the first post) aws#133 (comment)
To illustrate my situation, I've forked this project to https://github.com/eugenevd/aws-serverless-java-container and used the springboot pet-store sample as a base. Therein two branches, one each for each of the examples posted by @eduramiba (branch issue133-comment302648748) and @Juchar (branch issue133-comment399079415) Branch: master Branch: issue133-comment302648748 Branch: issue133-comment399079415 Comments packagedeployrun.sh - executes all the steps as outlined by https://github.com/awslabs/aws-serverless-java-container/wiki#deploying-the-sample-applications To double check, after a deployment, I downloaded the zip files uploaded to the bucket and compared their contents with the "mvn package" produced zip files. These were the same as expected. -- @sapessi No worries. Just to reiterate though; I'm trying to NOT use the shade plugin. If you have a signed dependency jar, these are invalidated when re-arranging that content the way shade does. Which is why I'm after what this issues' examples is about ... |
@eugenevd Puh, I checked out the branch and created the zip. After having a look everything looks fine to me. I have to admit that I ran out of ideas, from my point of view it should just work out. |
@Juchar So you're also getting errors? The examples posted here by yourself and @eduramiba ; is there a sample where they're working somewhere? |
@eugenevd I did just package it, not deploy it. For my lambda in my project the configuration I provided here works fine. |
I was hoping to find a working example that I can look at, to compare with, to see where it is that I'm going wrong... |
Did you get this resolved? I've got an issue where if I build it on my local machine, everything works. But if I build it on CodeBuild, it doesn't work. |
@eugenevd , I had a similar issue and solved it with the below settings:
bin.xml:
This works for me using |
Resolving in preparation for 1.3 release. |
Since using maven shade plugin to create a fat jar is known to be problematic because it can overwrite files by mixing jars (https://product.hubspot.com/blog/the-fault-in-our-jars-why-we-stopped-building-fat-jars), we currently use with success a safer way to deploy lambdas with dependencies: a single zip with all jars in a lib folder (as documented in https://docs.aws.amazon.com/en_en/lambda/latest/dg/create-deployment-pkg-zip-java.html)
Example code of how we do it in maven:
And
bin.xml
file:Thanks,
Eduardo
The text was updated successfully, but these errors were encountered: