Saturday, November 14, 2020

Java - Copy project dependency (jars) to a folder using Maven Plugin

If one have to execute a Java program from Command prompt then it would be cumbersome to specify all the required Jar files. It would be very easy to specify the jars if all of them are in a single folder. Using a Maven Plugin we can easily copy all the dependency jars to a specific folder and then execute a Java class easily

  • Use the following Maven plug-in, mention the target folder in <outputDirectory> element, currently the folder is target/jars
 <plugin>  
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-dependency-plugin</artifactId>  
      <version>3.1.2</version>  
      <executions>  
           <execution>  
                <id>copy-dependencies</id>  
                <phase>package</phase>  
                <goals>  
                     <goal>copy-dependencies</goal>  
                </goals>  
                <configuration>  
                     <outputDirectory>${project.build.directory}/jars</outputDirectory>  
                     <overWriteReleases>false</overWriteReleases>  
                     <overWriteSnapshots>false</overWriteSnapshots>  
                     <overWriteIfNewer>true</overWriteIfNewer>  
                </configuration>  
           </execution>  
      </executions>  
 </plugin>  
  • Execute the command mvn clean package
  • Above command should have copied all the dependency jars under target/jars folder


  • To run a Java program from the jar use the following command
 java -cp target/<YOUR_PROJECT_JAR>:target/jars/* <class_to_be_run>  
  • For example to execute Java Class DynamoDBDisableStream.java issue this command
 java -cp target/aws-java-0.0.1-SNAPSHOT.jar:target/jars/* com.balatamilmani.awsdemo.dynamodb.DynamoDBDisableStream  

Reference: https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html

Wednesday, November 11, 2020

AWS - Invoke Lambda using CLI

Invoking Lambda using CLI

Let's see how to invoke a Lambda from Command line, the CLI version used in this demonstration is "aws-cli/2.0.57"

  • Create simple Lambda, this is written using NodeJS
 exports.handler = async (event, context) => {  
   const response = {  
     statusCode: 200,  
     body: JSON.stringify('Hello from Lambda!'),  
   };  
   return response;  
 };  
  • Make sure you have setup Client Credential to run CLI commands from your local machine, refer this Blog to setup the same
  • Make sure the Credential you have setup has "AWSLambdaRole" policy assigned which entitle the Credential user to invoke Lambda
  • Execute the following command from Command Prompt
 aws lambda invoke --function-name <LAMBDA_NAME> --payload "{\"name\": \"balatamilmani\"}" --invocation-type RequestResponse --cli-binary-format raw-in-base64-out response.json  
    • --invocation-type -> Value should be "RequestResponse" for Synchronize call
    • --cli-binary-format -> Value should be "raw-in-base64-out" for AWS CLI V2