首页 > 解决方案 > Spring Boot maven 插件 - 导致 AWS Lambda 应用程序失败的 BOOT-INF 目录

问题描述

SpringBoot 2.1.0.RELEASE中生成的包结构(如果您提取 uber jar文件)发生了变化。

1.5.9.RELEASE jar 文件comlibMETA-INForg目录

2.1.0.RELEASE有一个BOOT-INF,META-INForg目录

基本上从2.0.0.RELEASE开始——所有的类和库都在BOOT-INF目录中。

因此 - 当您尝试在Amazon Lambda上运行 Spring Boot 项目时- 它说找不到jar,因为它无法读取新的 Spring Boot Uber jar 结构

我的问题:

我尝试了maven-shade-plugin- 但这会导致其他问题。任何帮助表示赞赏。

来自 StackOverflow 的参考链接:Spring Boot Maven Plugin - No BOOT-INF directory

标签: mavenspring-bootaws-lambdaspring-boot-maven-plugin

解决方案


这段 Maven 插件 XML 片段对我有用:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>zip-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <descriptors>
                                <descriptor>src${file.separator}assembly${file.separator}bin.xml</descriptor>
                            </descriptors>
                            <attach>false</attach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

此外,您将需要一个assembly/bin.xml包含以下内容的文件:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>lambda-package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- copy runtime dependencies with some exclusions -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}lib</directory>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>tomcat-embed*</exclude>
            </excludes>
        </fileSet>
        <!-- copy all classes -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}classes</directory>
            <includes>
                <include>**</include>
            </includes>
            <outputDirectory>${file.separator}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

请注意,您必须使用生成的.zip文件而不是.jar文件。

awslabs 提供了将 Spring Boot 2 与 Lambda 结合使用的完整工作示例:https ://github.com/awslabs/aws-serverless-java-container/tree/master/samples/springboot2/pet-store


推荐阅读