简体   繁体   English

如何在Maven中使用适用于Java的AWS开发工具包?

[英]How to use AWS SDK for java with maven?

I want to fetch the messages in the SQS queue. 我想获取SQS队列中的消息。 I am using the maven for the first time. 我是第一次使用Maven。 Here are the steps I have did so far. 这是我到目前为止已执行的步骤。

1. Created maven project using this command: 1.使用以下命令创建maven项目:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=aws-try -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The above command created a aws-try directory with src folder and pom.xml. 上面的命令使用src文件夹和pom.xml创建了aws-try目录。

2. Added AWS-SDK dependency in pom.xml: 2.在pom.xml中添加了AWS-SDK依赖性:

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk</artifactId>
  <version>1.11.78</version>
</dependency>

3. Added the SQSTry.java file under src > main > java > com > mycompany > app > SQSTry.java 3.在src> main> java> com> mycompany> app> SQSTry.java下添加了SQSTry.java文件。

package com.mycompany.app;

import java.util.List;
import java.util.Map.Entry;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteQueueRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageRequest;


public class SQSTry {

public static void main (String args[]) {


System.out.println("SQSTry");

        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider().getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException(
                    "Cannot load the credentials from the credential profiles file. " +
                    "Please make sure that your credentials file is at the correct " +
                    "location (~/.aws/credentials), and is in valid format.",
                    e);
        }

        AmazonSQS sqs = new AmazonSQSClient(credentials);
        Region apNortheast1 = Region.getRegion(Regions.AP_NORTHEAST_1);
        sqs.setRegion(apNortheast1);

        System.out.println("===========================================");
        System.out.println("Getting Started with Amazon SQS");
        System.out.println("===========================================\n");



}
}

4. Now package command 4.现在打包命令

mvn package

The above command was run against the pom.xml in the root of aws-try directory. 上面的命令是在aws-try目录的根目录下的pom.xml上运行的。

This gives the following error: 这给出了以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/AmazonClientException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
    at java.lang.Class.getMethod0(Class.java:2866)
    at java.lang.Class.getMethod(Class.java:1676)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.AmazonClientException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

I have added the dependency correctly. 我已经正确添加了依赖项。 If you have noticed the above SQSTry.java file, the AWSCredentials was also a package from amazon, but id does not give any error. 如果您已经注意到上面的SQSTry.java文件,则AWSCredentials也是来自Amazon的软件包,但是id不会给出任何错误。

What am I missing ? 我想念什么?

You need to add maven-shade-plugin to the pom.xml which packages all AWS sdk jars to a standalone jar file. 您需要将maven-shade-plugin添加到pom.xml,它将所有AWS sdk jar打包到一个独立的jar文件中。

Adding the following worked for me: 添加以下内容对我有用:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

I found this solution from here . 我从这里找到了这个解决方案。

Can you try mvn clean install . 你可以尝试mvn clean install吗? Also verify if you are using the right version for the SDK 还要验证您是否为SDK使用了正确的版本

Try adding 尝试添加

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    <version>1.11.78</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM