AWS Lambda java.lang.ClassNotFoundException:com.opencsv.exceptions.CsvException

AWS Lambda java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException

我在 AWS lambda 上 运行 代码时收到以下错误:

    START RequestId: cd42e5ab-fbc8-4f50-8c4f-e8257525bd78 Version: $LATEST
Error loading method handler on class com.test.myapp: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/opencsv/exceptions/CsvException
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.base/java.lang.Class.privateGetPublicMethods(Unknown Source)
    at java.base/java.lang.Class.getMethods(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException. Current classpath: file:/var/task/

我正在使用 IntelliJ 创建一个 JAR 并使用控制台将其上传到 AWS Lambda。

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testerLambda2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.12.204</version>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.2</version>
        </dependency>

    </dependencies>


    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

不确定我应该做什么。此错误仅在我添加 openCSV 时发生,否则我的 lambda 工作正常。

将 FAT jar 或 jar-with-dependencies jar 上传到 AWS,然后 运行 lambda。

使用 maven 程序集或 shade 插件。
阴影插件:

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

使用 mvn package 生成 FAT jar 并部署。