问题描述
我需要将静态 UI 文件注入现有的 Spring Boot 可执行 jar。为此,我将 jar 作为依赖项,并使用 maven antrun 插件:
-
将 jar 解压到临时目录中
-
复制静态 UI 文件到资源目录
-
重新压缩罐子
<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 时,出现以下错误:
引起:java.lang.IllegalStateException:无法打开嵌套条目“BOOT-INF/lib/HdrHistogram-2.1.12.jar”。它已被压缩,嵌套的 jar 文件必须不压缩存储。请检查用于创建可执行 jar 文件的机制
当我检查 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>