Polish "Add jarmode tools command to print the SBOM"

See gh-51505
This commit is contained in:
Moritz Halbritter
2026-09-02 16:34:49 +02:00
parent 962ca36126
commit e5a24bafc7
2 changed files with 27 additions and 7 deletions
@@ -73,14 +73,15 @@ class SbomCommand extends Command {
private String getSbomLocation(JarFile jarFile) throws IOException {
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
String location = manifest.getMainAttributes().getValue(SBOM_LOCATION_ATTRIBUTE);
if (location != null) {
return location;
}
if (manifest == null) {
throw new JarModeErrorException("No manifest found in the jar");
}
throw new JarModeErrorException(
"No SBOM found in the jar; the manifest has no '%s' attribute".formatted(SBOM_LOCATION_ATTRIBUTE));
String location = manifest.getMainAttributes().getValue(SBOM_LOCATION_ATTRIBUTE);
if (location == null) {
throw new JarModeErrorException(
"No SBOM found in the jar; the manifest has no '%s' attribute".formatted(SBOM_LOCATION_ATTRIBUTE));
}
return location;
}
private void writeSbom(InputStream in, PrintStream out, Map<Option, @Nullable String> options) throws IOException {
@@ -17,6 +17,7 @@
package org.springframework.boot.jarmode.tools;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -24,6 +25,8 @@ import java.io.PrintStream;
import java.nio.file.Files;
import java.util.ArrayDeque;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.junit.jupiter.api.Test;
@@ -63,6 +66,13 @@ class SbomCommandTests extends AbstractJarModeTests {
.withMessage("No SBOM found in the jar; the manifest has no 'Sbom-Location' attribute");
}
@Test
void shouldFailWhenManifestIsMissing() throws IOException {
File archive = createArchiveWithoutManifest();
assertThatExceptionOfType(JarModeErrorException.class).isThrownBy(() -> run(archive))
.withMessage("No manifest found in the jar");
}
@Test
void shouldFailWhenSbomEntryIsMissing() throws IOException {
Manifest manifest = createManifest("Sbom-Location: " + SBOM_LOCATION);
@@ -133,6 +143,15 @@ class SbomCommandTests extends AbstractJarModeTests {
return createArchive(manifest, SBOM_LOCATION, SBOM_RESOURCE);
}
private File createArchiveWithoutManifest() throws IOException {
File file = new File(this.tempDir, "no-manifest.jar");
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file))) {
zip.putNextEntry(new ZipEntry("some-file.txt"));
zip.closeEntry();
}
return file;
}
private byte[] getResourceContent(String resource) throws IOException {
try (InputStream stream = getClass().getResourceAsStream(resource)) {
assertThat(stream).as("Resource '%s'", resource).isNotNull();