3

I am using spring-boot-maven-plugin:2.1.0.RELEASE to package my main line application. This application has all common dependencies etc packaged

In a separate project, I am building a shaded jar using maven-shade-plugin:2.4.3 and injecting into my spring boot application under BOOT-INF/lib directory using org.codehaus.mojo:truezip-maven-plugin:1.1. Now when I start my spring-boot application I am getting following exception;

ingester_1         | Listening for transport dt_socket at address: 40500
ingester_1         | Exception in thread "main" java.lang.IllegalStateException: Failed to get nested archive for entry BOOT-INF/lib/ms-holdings-package-docker-DEV.0.0-SNAPSHOT.jar
ingester_1         |    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:108)
ingester_1         |    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:86)
ingester_1         |    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
ingester_1         |    at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
ingester_1         |    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
ingester_1         | Caused by: java.io.IOException: Unable to open nested jar file 'BOOT-INF/lib/ms-holdings-package-docker-DEV.0.0-SNAPSHOT.jar'
ingester_1         |    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:256)
ingester_1         |    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:241)
ingester_1         |    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:103)
ingester_1         |    ... 4 more
ingester_1         | Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/ms-holdings-package-docker-DEV.0.0-SNAPSHOT.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
ingester_1         |    at org.springframework.boot.loader.jar.JarFile.createJarFileFromFileEntry(JarFile.java:284)
ingester_1         |    at org.springframework.boot.loader.jar.JarFile.createJarFileFromEntry(JarFile.java:264)
ingester_1         |    at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:252)
ingester_1         |    ... 6 more

I tried to copy the jar inside BOOT-INF/lib directly taking maven-truezip-plugin out but still the same. Is there anyway I can solve this? or a workaround?

SJunejo
  • 1,316
  • 4
  • 23
  • 39
  • Just for the information, I unpack the problamatic jar using `jar -xvf` and repackaged again using `jar -cfm0M...`, put the jar back into spring-boot app, but no use. I am now suspecting spring-boot has an issue! – SJunejo Mar 19 '19 at 23:04
  • 1
    As already [discussed on GitHub](https://github.com/spring-projects/spring-boot/issues/16264), the jar you are adding is being stored incorrectly. Have you tried to configure things so that its entry in your app’s jar is not compressed? You can read more about this requirement [in the documentation](https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#executable-jar-restrictions). – Andy Wilkinson Mar 20 '19 at 07:22
  • @AndyWilkinsonas thanks for the link. This means I can not use Spring Boot Loader and have to work with alternatives! It would be helpful for others if documentation shows one alternative packaging style example and how it would compare with Spring Boot packaging to really make it simple and easy. – SJunejo Mar 20 '19 at 10:08

3 Answers3

0

OK to resolve the issue, in my maven package build;

1) Invoked ANT to unzip the released template SPRING-BOOT-app.jar
2) Unzip and repackage additional shaded artefact from the project
3) Copy repackaged artefact under BOOT-INF/lib
4) Repackage SPRING-BOOT-app.jar

My pom now have the following additional step;

pom.xml

    <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>repackage-app</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target name="repack-app">
                    <property name="workDirectory" value="${project.build.directory}/app-repack" />
                    <property name="sourceArtifact" value="${project.build.directory}/SPRING-BOOT-app.jar" />
                    <property name="appendWith" value="${project.build.directory}/${project.artifactId}-${project.version}.jar" /> 
                    <ant antfile="${packager.utilities.directory}/build.xml"> 
                        <target name="append-and-repack-spring-boot-artiafact" />
                    </ant>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

build.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project name="utilities" basedir=".">

    <!-- Repack and append artifact to Spring Boot App -->
    <target name="append-and-repack-spring-boot-artiafact">

        <!-- Clean -->
        <delete dir="${workDirectory}" failonerror="false"/>
        <mkdir dir="${workDirectory}" />

        <!-- Copy the original files -->
        <copy file="${sourceArtifact}" todir="${workDirectory}"/>
        <copy file="${appendWith}" todir="${workDirectory}" />

        <!-- Get basename -->
        <basename property="sourceArtifactName" file="${sourceArtifact}" />
        <basename property="appendArtifactName" file="${appendWith}" />

        <!-- repackage and append -->
        <repack-jar artifact="${appendArtifactName}" reworkOutputDir="${workDirectory}/repackaged" />
        <append artifact="${sourceArtifactName}" appendFrom="${workDirectory}/repackaged" location="BOOT-INF/lib" />

    </target>

    <!-- Repackage a single jar -->
    <macrodef name="repack-jar">
        <attribute name="artifact" />
        <attribute name="reworkOutputDir" />
        <sequential>
            <echo
                message="-------------- Repackaging @{artifact} --------------" />
            <!-- Unpack and delete jar -->
            <unzip dest="${workDirectory}/tmp" src="${workDirectory}/@{artifact}" />
            <delete file="${workDirectory}/@{artifact}" />

            <!-- Package jar -->
            <jar manifest="${workDirectory}/tmp/META-INF/MANIFEST.MF"
                compress="false" basedir="${workDirectory}/tmp"
                destfile="@{reworkOutputDir}/@{artifact}" />
            <delete dir="${workDirectory}/tmp" />
        </sequential>
    </macrodef>

    <!-- Append artifact to jar -->
    <macrodef name="append">
        <attribute name="artifact" />
        <attribute name="appendFrom" />
        <attribute name="location" />
        <sequential>
            <echo
                message="-------------- Unpacking @{artifact} --------------" />
            <!-- Unpack and delete jar -->
            <unzip dest="${workDirectory}/tmp" src="${workDirectory}/@{artifact}" />
            <delete file="${workDirectory}/@{artifact}" />

            <copy todir="${workDirectory}/tmp/@{location}" overwrite="true" force="true">
                <fileset dir="@{appendFrom}" includes="*.jar" />
            </copy>

            <!-- Package jar -->
            <jar manifest="${workDirectory}/tmp/META-INF/MANIFEST.MF"
                compress="false" basedir="${workDirectory}/tmp"
                destfile="${workDirectory}/@{artifact}" />
            <delete dir="${workDirectory}/tmp" />
        </sequential>
    </macrodef>
</project>

NOTE: Do NOT USE 7Zip/Winrar or any other compression tools to open the Spring Boot App which you are planning to deploy. It would update the compression offset (or something) and app would fail. Just don't touch the artefact, take a copy and inspect!

SJunejo
  • 1,316
  • 4
  • 23
  • 39
0

Fix for us, same failure message, was to use just the spring-boot-maven-plugin (which creates a runnable jar for you) instead of using the shade plugin and running it from that jar.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
0

Add this in plugin configuration while building the jar.

           <archiverConfig>
                <compress>false</compress>
            </archiverConfig>
  • Please refrain from posting the same answer multiple times, as you have [here](https://stackoverflow.com/a/68410377/10871073), [here](https://stackoverflow.com/a/68410932/10871073) and [here](https://stackoverflow.com/a/68410947/10871073). – Adrian Mole Jul 17 '21 at 19:14