mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge pull request #24597 from edwardsre
* pr/24597: Polish "Handle missing manifest files in JarTypeFilter" Handle missing manifest files in JarTypeFilter Closes gh-24597
This commit is contained in:
commit
5fa5b6242a
@ -20,8 +20,10 @@ import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
|
||||
@ -43,8 +45,9 @@ class JarTypeFilter extends DependencyFilter {
|
||||
@Override
|
||||
protected boolean filter(Artifact artifact) {
|
||||
try (JarFile jarFile = new JarFile(artifact.getFile())) {
|
||||
String jarType = jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Jar-Type");
|
||||
return jarType != null && EXCLUDED_JAR_TYPES.contains(jarType);
|
||||
return Optional.ofNullable(jarFile.getManifest()).map(Manifest::getMainAttributes)
|
||||
.map((attributes) -> attributes.getValue("Spring-Boot-Jar-Type")).map(EXCLUDED_JAR_TYPES::contains)
|
||||
.orElse(Boolean.FALSE);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return false;
|
||||
|
@ -60,12 +60,17 @@ class JarTypeFilterTests {
|
||||
assertThat(new JarTypeFilter().filter(createArtifact("annotation-processor"))).isTrue();
|
||||
}
|
||||
|
||||
private Artifact createArtifact(String jarType) {
|
||||
@Test
|
||||
void whenArtifactHasNoManifestFileThenItIsIncluded() {
|
||||
assertThat(new JarTypeFilter().filter(createArtifactWithNoManifest())).isFalse();
|
||||
}
|
||||
|
||||
private Artifact createArtifact(String springBootJarType) {
|
||||
Path jarPath = this.temp.resolve("test.jar");
|
||||
Manifest manifest = new Manifest();
|
||||
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||
if (jarType != null) {
|
||||
manifest.getMainAttributes().putValue("Spring-Boot-Jar-Type", jarType);
|
||||
if (springBootJarType != null) {
|
||||
manifest.getMainAttributes().putValue("Spring-Boot-Jar-Type", springBootJarType);
|
||||
}
|
||||
try {
|
||||
new JarOutputStream(new FileOutputStream(jarPath.toFile()), manifest).close();
|
||||
@ -73,6 +78,21 @@ class JarTypeFilterTests {
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
return mockArtifact(jarPath);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return mockArtifact(jarPath);
|
||||
}
|
||||
|
||||
private Artifact mockArtifact(Path jarPath) {
|
||||
Artifact artifact = mock(Artifact.class);
|
||||
given(artifact.getFile()).willReturn(jarPath.toFile());
|
||||
return artifact;
|
||||
|
Loading…
Reference in New Issue
Block a user