Spring boot maven plugin - BOOT-INF directory causing AWS Lambda application to fail
Asked Answered
C

2

3

There has been a change in the generated package structure (if you extract the uber jar file) in SpringBoot 2.1.0.RELEASE.

The 1.5.9.RELEASE jar file has com, lib, META-INF, and org directories

2.1.0.RELEASE has a BOOT-INF, META-INF and org directories

Basically from 2.0.0.RELEASE onwards - all the classes and libs are in the BOOT-INF directory.

Due to this - when you try to run a Spring Boot project on Amazon Lambda - it says that there is a jar not found as it cannot read the new Spring Boot Uber jar structure

My question:

  • Is it possible in the newer versions of the Spring Boot Maven Plugin, to generate the uber jar to be the same structure as in version 1.5.9.RELEASE?

I tried the maven-shade-plugin - but that leads to other issues. Any help is appreciated.

Reference link from StackOverflow: Spring Boot Maven Plugin - No BOOT-INF directory

Carbide answered 30/9, 2019 at 23:39 Comment(0)
S
1

This snippet of maven plugin XML did the trick for me:

            <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>

In addition, you will need an assembly/bin.xml file with the following contents:

<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>

Note that you will have to use the generated .zip file rather than the .jar file.

A full working example of using Spring Boot 2 with Lambda is available from awslabs: https://github.com/awslabs/aws-serverless-java-container/tree/master/samples/springboot2/pet-store

Swill answered 9/10, 2019 at 7:51 Comment(0)
P
0

In addition to @balster answer:

For my case I do not need maven-dependency-plugin.

My pom.xml plugins section contains:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.4.2</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/bin.xml</descriptor>
                    </descriptors>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

This is my bin.xml content

    <id>lambda</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>${artifact}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}${file.separator}classes</directory>
            <includes>
                <include>**</include>
            </includes>
            <outputDirectory>${file.separator}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

The only missing part is the maven folder with pom files (xml/properties). But it runs without it.

Peggypegma answered 30/3, 2023 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.