{"id":"3917783216fc3251","repo":"spring-projects/spring-boot","slug":"error-packaging-archive-for-image","errorCode":null,"errorMessage":"Error packaging archive for image","messagePattern":"Error packaging archive for image","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java","lineNumber":441,"sourceCode":"\n\t\tprivate final ImagePackager packager;\n\n\t\tPackagedTarArchive(Owner owner, Libraries libraries, ImagePackager packager) {\n\t\t\tthis.owner = owner;\n\t\t\tthis.libraries = libraries;\n\t\t\tthis.packager = packager;\n\t\t}\n\n\t\t@Override\n\t\tpublic void writeTo(OutputStream outputStream) throws IOException {\n\t\t\tTarArchiveOutputStream tar = new TarArchiveOutputStream(outputStream);\n\t\t\ttar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);\n\t\t\ttry {\n\t\t\t\tthis.packager.packageImage(this.libraries, (entry, entryWriter) -> write(entry, entryWriter, tar));\n\t\t\t}\n\t\t\tcatch (RuntimeException ex) {\n\t\t\t\toutputStream.close();\n\t\t\t\tthrow new RuntimeException(\"Error packaging archive for image\", ex);\n\t\t\t}\n\t\t}\n\n\t\tprivate void write(ZipEntry jarEntry, @Nullable EntryWriter entryWriter, TarArchiveOutputStream tar) {\n\t\t\ttry {\n\t\t\t\tTarArchiveEntry tarEntry = convert(jarEntry);\n\t\t\t\ttar.putArchiveEntry(tarEntry);\n\t\t\t\tif (tarEntry.isFile()) {\n\t\t\t\t\tAssert.state(entryWriter != null, \"'entryWriter' must not be null\");\n\t\t\t\t\tentryWriter.write(tar);\n\t\t\t\t}\n\t\t\t\ttar.closeArchiveEntry();\n\t\t\t}\n\t\t\tcatch (IOException ex) {\n\t\t\t\tthrow new IllegalStateException(ex);\n\t\t\t}\n\t\t}\n","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/spring-projects/spring-boot/blob/5b2dbdbb8be64415eb6552f81ff7c449c8d251e6/build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java#L423-L459","documentation":"Thrown by BuildImageMojo.PackagedTarArchive.writeTo as a RuntimeException wrapping a RuntimeException raised while the ImagePackager lays out the application into the tar stream sent to the builder. This is the boundary where the repackaged archive contents are converted into layered tar entries; any failure in packager.packageImage (entry writer, layout, library handling) is caught, the output stream is closed, and the error is re-thrown with this message. Note it also closes outputStream before re-throwing.","triggerScenarios":"An EntryWriter returning null for a file entry (caught downstream as IllegalStateException in BuildImageMojo.write, then propagated here); a corrupted or truncated source jar that the ImagePackager cannot read; an IOException during writing that is wrapped into IllegalStateException by the inner write method; incompatible layout vs packaging type.","commonSituations":"Source archive was partially written by a previous failed build; mixing layout=WAR with a jar packaging; custom layers configuration that produces no layers yet selectors reference content; concurrent build writing to the same target directory.","solutions":["Run mvn clean package spring-boot:build-image to guarantee a coherent source archive.","Inspect the wrapped cause — if it is an IllegalStateException about 'entryWriter', the layout/packaging combination is likely wrong.","Remove or simplify any custom <layers> configuration to determine if it is the source of the packaging failure.","Ensure the <layout> matches the project packaging (JAR for jar, WAR for war)."],"exampleFix":"// before — mismatched layout\n<layout>WAR</layout> <!-- on a jar-packaged project -->\n// after — let the plugin guess, or match packaging\n<layout>JAR</layout>","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// If you invoke BuildImage programmatically / in a test\ntry {\n    // trigger build-image packaging\n} catch (RuntimeException ex) {\n    if (ex.getMessage() != null && ex.getMessage().contains(\"Error packaging archive for image\")) {\n        Throwable cause = ex.getCause();\n        // inspect cause: entryWriter null -> layout/packaging mismatch\n    }\n    throw ex;\n}","preventionTips":["Keep <layout> consistent with project packaging (JAR for jar, WAR for war).","Run mvn clean package spring-boot:build-image to avoid partial-archive state.","Simplify custom layers configuration when diagnosing; restore once packaging succeeds."],"tags":["maven","spring-boot","build-image","packaging","tar"],"analyzedSha":"5b2dbdbb8be64415eb6552f81ff7c449c8d251e6","analyzedAt":"2026-08-04T18:53:14.967Z","schemaVersion":2}