'Failed to get nested archive for entry BOOT-INF/lib for jar created with maven-shaded-plugin
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?
Solution 1:[1]
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!
Solution 2:[2]
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.
Solution 3:[3]
Add this in plugin configuration while building the jar.
<archiverConfig>
<compress>false</compress>
</archiverConfig>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SJunejo |
Solution 2 | rogerdpack |
Solution 3 | Dilip Paudel |