This section shows how to package your Java code into a deployment package using Eclipse IDE and Maven plugin for Eclipse.
Before You Begin
Install the Maven Plugin for Eclipse.
- Start Eclipse. From the Help menu in Eclipse, choose Install New Software.
- In the Install window, typehttp://download.eclipse.org/technology/m2e/releasesin the Work with: box, and choose Add.
- Follow the steps to complete the setup.
Step 1: Create and Build a Project
In this step, you start Eclipse and create a Maven project. You will add the necessary dependencies, and build the project. The build will produce a .jar, which is your deployment package.
- Create a new Maven project in Eclipse.- From the File menu, choose New, and then choose Project.
- In the New Project window, choose Maven Project.
- In the New Maven Project window, choose Create a simple project, and leave other default selections.
- In the New Maven Project, Configure project windows, type the following Artifact information:- Group Id: doc-examples
- Artifact Id: lambda-java-example
- Version: 0.0.1-SNAPSHOT
- Packaging: jar
- Name: lambda-java-example
 
 
- Add theaws-lambda-java-coredependency to thepom.xmlfile.It provides definitions of theRequestHandler,RequestStreamHandler, andContextinterfaces. This allows you to compile code that you can use with AWS Lambda.- Open the context (right-click) menu for thepom.xmlfile, choose Maven, and then choose Add Dependency.
- In the Add Dependency windows, type the following values:Group Id: com.amazonawsArtifact Id: aws-lambda-java-coreVersion: 1.1.0NoteIf you are following other tutorial topics in this guide, the specific tutorials might require you to add more dependencies. Make sure to add those dependencies as required.
 
- Add Java class to the project.- Open the context (right-click) menu for thesrc/main/javasubdirectory in the project, choose New, and then choose Class.
- In the New Java Class window, type the following values:- Package:example
- Name:HelloNoteIf you are following other tutorial topics in this guide, the specific tutorials might recommend different package name or class name.
 
- Add your Java code. If you are following other tutorial topics in this guide, add the provided code.
 
- Build the project.Open the context (right-click) menu for the project in Package Explorer, choose Run As, and then choose Maven Build .... In the Edit Configuration window, typepackagein the Goals box.NoteThe resulting .jar,lambda-java-example-0.0.1-SNAPSHOT.jar, is not the final standalone .jar that you can use as your deployment package. In the next step, you add the Apachemaven-shade-pluginto create the standalone .jar. For more information, go to Apache Maven Shade Plugin.
- Add themaven-shade-pluginplugin and rebuild.The maven-shade-plugin will take artifacts (jars) produced by the package goal (produces customer code .jar), and created a standalone .jar that contains the compiled customer code, and the resolved dependencies from thepom.xml.- Open the context (right-click) menu for thepom.xmlfile, choose Maven, and then choose Add Plugin.
- In the Add Plugin window, type the following values:- Group Id: org.apache.maven.plugins
- Artifact Id: maven-shade-plugin
- Version: 2.3
 
- Now build again.This time we will create the jar as before, and then use themaven-shade-pluginto pull in dependencies to make the standalone .jar.- Open the context (right-click) menu for the project, choose Run As, and then choose Maven build ....
- In the Edit Configuration windows, typepackage shade:shadein the Goals box.
- ChooseRun.You can find the resulting standalone .jar (that is, your deployment package), in the/targetsubdirectory.Open the context (right-click) menu for the/targetsubdirectory, choose Show In, choose System Explorer, and you will find thelambda-java-example-0.0.1-SNAPSHOT.jar.
 
 
 
No comments:
Post a Comment