{"id":"c4630792c581be80","repo":"spring-projects/spring-boot","slug":"a-jar-or-war-file-is-required-for-building-image","errorCode":null,"errorMessage":"A jar or war file is required for building image","messagePattern":"A jar or war file is required for building image","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/BuildImageMojo.java","lineNumber":325,"sourceCode":"\t\t\timage.setImagePlatform(this.imagePlatform);\n\t\t}\n\t\treturn customize(image.getBuildRequest(this.project.getArtifact(), content));\n\t}\n\n\tprivate TarArchive getApplicationContent(Owner owner, Libraries libraries, ImagePackager imagePackager) {\n\t\tImagePackager packager = getConfiguredPackager(() -> imagePackager);\n\t\treturn new PackagedTarArchive(owner, libraries, packager);\n\t}\n\n\tprivate File getArchiveFile() {\n\t\t// We can't use 'project.getArtifact().getFile()' because package can be done in a\n\t\t// forked lifecycle and will be null\n\t\tFile archiveFile = getTargetFile(this.finalName, this.classifier, this.sourceDirectory);\n\t\tif (!archiveFile.exists()) {\n\t\t\tarchiveFile = getSourceArtifact(this.classifier).getFile();\n\t\t}\n\t\tif (!archiveFile.exists()) {\n\t\t\tthrow new IllegalStateException(\"A jar or war file is required for building image\");\n\t\t}\n\t\treturn archiveFile;\n\t}\n\n\t/**\n\t * Return the {@link File} to use to back up the original source.\n\t * @return the file to use to back up the original source\n\t */\n\tprivate @Nullable File getBackupFile() {\n\t\t// We can't use 'project.getAttachedArtifacts()' because package can be done in a\n\t\t// forked lifecycle and will be null\n\t\tif (this.classifier != null) {\n\t\t\tFile backupFile = getTargetFile(this.finalName, null, this.sourceDirectory);\n\t\t\tif (backupFile.exists()) {\n\t\t\t\treturn backupFile;\n\t\t\t}\n\t\t\tArtifact source = getSourceArtifact(null);\n\t\t\tif (!this.classifier.equals(source.getClassifier())) {","sourceCodeStart":307,"sourceCodeEnd":343,"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#L307-L343","documentation":"Thrown by BuildImageMojo.getArchiveFile as an IllegalStateException when neither the expected target archive (target/<finalName>.<ext>) nor the source artifact file exists. spring-boot:build-image packages an existing jar/war into an OCI image, so it requires the artifact to have been built first. The code deliberately avoids project.getArtifact().getFile() because package may run in a forked lifecycle, so it falls back to the source artifact — if both are absent, the build cannot proceed.","triggerScenarios":"Invoking spring-boot:build-image directly without first running package; running build-image in a phase before package; configuring a classifier whose repackaged artifact has not been produced; a multi-module reactor where the application module's package step was skipped or failed silently.","commonSituations":"Running `mvn spring-boot:build-image` instead of `mvn package spring-boot:build-image`; binding build-image to a phase earlier than package; using a <classifier> that doesn't match any produced artifact; clean having removed target/ before build-image runs standalone.","solutions":["Always run package before build-image: `mvn package spring-boot:build-image`.","Bind spring-boot:build-image to the package phase or later in the <executions> block.","If using a classifier, confirm the repackaged artifact with that classifier exists under target/.","Run mvn clean package spring-boot:build-image to ensure a fresh, complete artifact."],"exampleFix":"// before\nmvn spring-boot:build-image\n// after\nmvn package spring-boot:build-image","handlingStrategy":"validation","validationCode":"// Confirm the archive exists before invoking build-image\nimport java.io.File;\n\nFile target = new File(projectBuildDir, finalName + \".\" + extension);\nif (!target.exists()) {\n    throw new IllegalStateException(\"Run `mvn package` first; missing: \" + target);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bind spring-boot:build-image to the package phase or later.","Always invoke build-image as part of `mvn package spring-boot:build-image`.","In multi-module builds, ensure the application module's package step runs before build-image."],"tags":["maven","spring-boot","build-image","lifecycle","configuration"],"analyzedSha":"5b2dbdbb8be64415eb6552f81ff7c449c8d251e6","analyzedAt":"2026-08-04T18:53:14.967Z","schemaVersion":2}