无法获得使用maven-shaded-plugin

时间:2019-03-19 22:07:34

标签: spring-boot maven-shade-plugin spring-boot-maven-plugin

我正在使用spring-boot-maven-plugin:2.1.0.RELEASE打包我的主线应用程序。此应用程序已打包了所有常见的依赖项

在一个单独的项目中,我正在使用maven-shade-plugin:2.4.3构建一个有阴影的jar,并使用org.codehaus.mojo:truezip-maven-plugin注入到BOOT-INF / lib目录下的spring boot应用程序中:1.1。现在,当我启动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

我试图直接将maven-truezip-plugin复制出来,复制到BOOT-INF / lib内的jar,但还是一样。反正有什么我可以解决的?或解决方法?

3 个答案:

答案 0 :(得分:0)

好,在我的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现在需要执行以下附加步骤;

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>

注意:请勿使用7Zip / Winrar或任何其他压缩工具打开您打算部署的Spring Boot App。它将更新压缩偏移量(或其他值),并且应用程序将失败。只是不要触摸文物,复制并检查!

答案 1 :(得分:0)

为我们解决的错误消息是,仅使用spring-boot-maven-plugin(为您创建一个可运行的jar),而不是使用shade插件并从该jar运行它。

答案 2 :(得分:0)

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

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