微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重新打包 Spring Boot Jar 时如何修复压缩错误

如何解决重新打包 Spring Boot Jar 时如何修复压缩错误

我需要将静态 UI 文件注入现有的 Spring Boot 可执行 jar。为此,我将 jar 作为依赖项,并使用 maven antrun 插件

  • 将 jar 解压到临时目录中

  • 在解压后的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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?