Spring Framework打包RPM后Jar无法执行:嵌套归档压缩异常
Looks like you've hit a common pitfall with packaging Spring Boot executables as RPMs—rpmbuild's default behavior of repacking Jars breaks the nested archive structure that Spring Boot relies on. Let's break down the problem and fix it step by step.
The Root Cause
Your error message spells out the issue clearly:
Unable to open nested entry 'BOOT-INF/lib/commons-io-2.4.jar'. It has been compressed and nested jar files must be stored without compression.
Spring Boot's executable Jar requires all nested Jars under BOOT-INF/lib to be stored without compression (using the STORED entry type in the Jar archive). By default, rpmbuild automatically repacks Jar files to compress their contents, which re-compresses those nested Jars and makes them unreadable to the Spring Boot loader.
Step 1: Disable Jar Repacking in Your Spec File
Add this line at the top of your .spec file (right after the Name/Version definitions) to tell rpmbuild not to modify your Spring Boot Jar:
%global __jar_repack %{nil}
This disables the default macro that triggers Jar repacking, preserving the original structure of your executable Jar.
Step 2: Clean Up Your Spec File for Reliability
Your current .spec has some redundant and potentially problematic steps—let's fix those to avoid other issues:
Simplify the
%prepsection:
The%setup -qcommand already extracts your source tarball to the correct build directory. Remove the manual copy command, since it's redundant and could cause directory conflicts:%prep %setup -qFix the
%installpathing:
Use RPM's built-in macros (like%{_builddir}) instead of hardcoded paths to make your spec file more portable. Update the%installsection to:%install rm -rf $RPM_BUILD_ROOT # Create the target directory structure mkdir -p $RPM_BUILD_ROOT/opt/sample/sample-tool-1.0/ # Copy the Jar from the extracted source directory cp %{_builddir}/%{name}-%{version}/%{name}-sample-0.0.10.BUILD-SNAPSHOT.jar $RPM_BUILD_ROOT/opt/sample/sample-tool-1.0/sample-tool.jar # Ensure executable permissions (optional but good practice) chmod 0755 $RPM_BUILD_ROOT/opt/sample/sample-tool-1.0/sample-tool.jarClean up the
%filessection:
Ensure%defattris the first entry under%files(standard practice) and keep your directory structure intact:%files %defattr(-,root,root,-) /opt/sample/ %doc
Step 3: Verify the Fix
After updating your spec file:
- Rebuild the RPM with
rpmbuild -ba your.spec - Install the RPM locally
- Run your Jar again—this time it should load the nested Jars correctly without the compression error.
This works because disabling __jar_repack tells rpmbuild to treat your Spring Boot Jar as a binary file instead of modifying its internal structure. This preserves the STORED entry type for all nested Jars under BOOT-INF/lib, which the Spring Boot loader expects.
内容的提问来源于stack exchange,提问作者JeanP

