重新打包 Spring Boot Jar 时如何修复压缩错误
How to fix compression error when repackaging Spring Boot Jar
我需要将静态 UI 文件注入现有的 Spring 启动可执行 jar。为此,我将 jar 作为依赖项,并使用 maven ant运行 插件来:
将 jar 解压到临时目录
将资源目录添加到解压缩的 jar
复制静态UI文件到资源目录
重新压缩 jar
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>build-ui-kit</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>ui-assembly-config.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>repack</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- unzip jar -->
<unzip src="${com.xxx:xxx:jar}" dest="${project.build.directory}/tmp"/>
<!-- make resources dir in unzipped jar dir -->
<mkdir dir="${project.build.directory}/tmp/resources"/>
<!-- copy ui files to resources dir -->
<copy todir="${project.build.directory}/tmp/resources">
<fileset dir="${basedir}/../../src/ui/dist">
<include name="*"/>
</fileset>
</copy>
<!-- zip tmp dir to create txapps jar -->
<zip basedir="${project.build.directory}/tmp" destfile="${project.build.directory}/yyyy.jar"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
这一切似乎都有效;但是,当我 运行 Jar 时,出现以下错误:
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/HdrHistogram-2.1.12.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
当我检查 Jar 的内容时,一切看起来都很好(库似乎没有被压缩)。知道这里发生了什么或有更好的重新打包解决方案的建议吗?谢谢!
我最终使用 exec-maven-plugin 和 Java jar 实用程序来添加 UI 文件。请参见下面的代码(ui 文件位于 /tmp/resources 目录中):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>update-jar</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>jar</executable>
<workingDirectory>${project.build.directory}/tmp</workingDirectory>
<arguments>
<argument>uf</argument>
<argument>dependencies/myjar-${version}.jar</argument>
<argument>resources</argument>
</arguments>
</configuration>
</plugin>
我需要将静态 UI 文件注入现有的 Spring 启动可执行 jar。为此,我将 jar 作为依赖项,并使用 maven ant运行 插件来:
将 jar 解压到临时目录
将资源目录添加到解压缩的 jar
复制静态UI文件到资源目录
重新压缩 jar
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>build-ui-kit</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>ui-assembly-config.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>repack</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <!-- unzip jar --> <unzip src="${com.xxx:xxx:jar}" dest="${project.build.directory}/tmp"/> <!-- make resources dir in unzipped jar dir --> <mkdir dir="${project.build.directory}/tmp/resources"/> <!-- copy ui files to resources dir --> <copy todir="${project.build.directory}/tmp/resources"> <fileset dir="${basedir}/../../src/ui/dist"> <include name="*"/> </fileset> </copy> <!-- zip tmp dir to create txapps jar --> <zip basedir="${project.build.directory}/tmp" destfile="${project.build.directory}/yyyy.jar"/> </target> </configuration> </execution> </executions> </plugin>
这一切似乎都有效;但是,当我 运行 Jar 时,出现以下错误:
Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/HdrHistogram-2.1.12.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
当我检查 Jar 的内容时,一切看起来都很好(库似乎没有被压缩)。知道这里发生了什么或有更好的重新打包解决方案的建议吗?谢谢!
我最终使用 exec-maven-plugin 和 Java jar 实用程序来添加 UI 文件。请参见下面的代码(ui 文件位于 /tmp/resources 目录中):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>update-jar</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>jar</executable>
<workingDirectory>${project.build.directory}/tmp</workingDirectory>
<arguments>
<argument>uf</argument>
<argument>dependencies/myjar-${version}.jar</argument>
<argument>resources</argument>
</arguments>
</configuration>
</plugin>