Spring Boot 无法为使用maven-shaded-plugin创建的jar获取条目BOOT-INF/lib的嵌套归档文件

8gsdolmq  于 2022-12-26  发布在  Spring
关注(0)|答案(3)|浏览(265)

我正在使用spring-boot-maven-plugin:2.1.0.RELEASE打包我的主线应用程序。这个应用程序有所有常见的依赖项等打包
在一个单独的项目中,我使用maven-shade-plugin:2.4.3构建了一个shaded jar,并使用org.codehaus.mojo:truezip-maven-plugin:1.1注入到BOOT-INF/lib目录下的spring Boot 应用程序中。

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

我试着直接复制BOOT-INF/lib里面的jar文件,把maven-truezip-plugin取出来,但是还是一样。有什么办法可以解决这个问题吗?或者是一个变通方案?

biswetbf

biswetbf1#

可以在我的maven包构建中解决这个问题;

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

我的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>
    • 内部版本. 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>
    • 注意:不要使用7Zip/Winrar或任何其他压缩工具打开您计划部署的Spring Boot应用程序。它会更新压缩偏移量(或其他内容),应用程序会失败。只是不要触摸工件,复制一份并检查!**
7fhtutme

7fhtutme2#

对我们来说,同样的失败消息,修复是只使用spring-boot-maven-plugin(为您创建一个可运行的jar),而不是使用shade插件并从该jar运行它。

zwghvu4y

zwghvu4y3#

在构建jar时将其添加到插件配置中。

<archiverConfig>
                <compress>false</compress>
            </archiverConfig>

相关问题