Commit 86eecb01 authored by Rob Edwards's avatar Rob Edwards Committed by Stephane Nicoll

Handle missing manifest files in JarTypeFilter

This commit makes sure that a jar file without a manifest is handled
properly (i.e. included in the repackaged archive).

See gh-24597
parent e8a936df
...@@ -20,8 +20,10 @@ import java.io.IOException; ...@@ -20,8 +20,10 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
...@@ -43,8 +45,9 @@ class JarTypeFilter extends DependencyFilter { ...@@ -43,8 +45,9 @@ class JarTypeFilter extends DependencyFilter {
@Override @Override
protected boolean filter(Artifact artifact) { protected boolean filter(Artifact artifact) {
try (JarFile jarFile = new JarFile(artifact.getFile())) { try (JarFile jarFile = new JarFile(artifact.getFile())) {
String jarType = jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Jar-Type"); return Optional.ofNullable(jarFile.getManifest()).map(Manifest::getMainAttributes)
return jarType != null && EXCLUDED_JAR_TYPES.contains(jarType); .map((attributes) -> attributes.getValue("Spring-Boot-Jar-Type")).map(EXCLUDED_JAR_TYPES::contains)
.orElse(Boolean.FALSE);
} }
catch (IOException ex) { catch (IOException ex) {
return false; return false;
......
...@@ -60,6 +60,11 @@ class JarTypeFilterTests { ...@@ -60,6 +60,11 @@ class JarTypeFilterTests {
assertThat(new JarTypeFilter().filter(createArtifact("annotation-processor"))).isTrue(); assertThat(new JarTypeFilter().filter(createArtifact("annotation-processor"))).isTrue();
} }
@Test
void whenArtifactHasNoManifestFileThenItIsIncluded() {
assertThat(new JarTypeFilter().filter(createArtifactWithNoManifest())).isFalse();
}
private Artifact createArtifact(String jarType) { private Artifact createArtifact(String jarType) {
Path jarPath = this.temp.resolve("test.jar"); Path jarPath = this.temp.resolve("test.jar");
Manifest manifest = new Manifest(); Manifest manifest = new Manifest();
...@@ -78,4 +83,17 @@ class JarTypeFilterTests { ...@@ -78,4 +83,17 @@ class JarTypeFilterTests {
return artifact; return artifact;
} }
private Artifact createArtifactWithNoManifest() {
Path jarPath = this.temp.resolve("test.jar");
try {
new JarOutputStream(new FileOutputStream(jarPath.toFile())).close();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
Artifact artifact = mock(Artifact.class);
given(artifact.getFile()).willReturn(jarPath.toFile());
return artifact;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment