Search code examples
javaspring-bootmavenmaven-assembly-pluginmaven-antrun-plugin

How to fix compression error when repackaging Spring Boot Jar


I need to inject static UI files into an existing Spring Boot executable jar. To do so, I'm pulling the jar as a dependency, and using the maven antrun plugin to:

  • Unzip the jar in a temp dir

  • Add a resources dir to the unzipped jar

  • Copy static UI files to the resources dir

  • Re-zip the 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>
    

This all seems to work; however, when I run the Jar, I get the following error:

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

When I inspect the contents of the Jar, everything looks fine (the libs do not seem to be compressed). Any idea of what's going on here or suggestions for a better repackaging solution? Thanks!


Solution

  • I ended up using the exec-maven-plugin and Java jar utility to add the UI files. See code below (the ui files are in the /tmp/resources directory):

    <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>