Switch layering on by default

Closes gh-20983
This commit is contained in:
Madhura Bhave
2020-07-27 18:45:00 -07:00
parent eaca13cb01
commit 41f5ba9077
24 changed files with 268 additions and 61 deletions

View File

@@ -180,16 +180,14 @@ class PackagingDocumentationTests {
}
@TestTemplate
void bootJarLayered() throws IOException {
this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-layered").build("bootJar");
void bootJarLayeredDisabled() throws IOException {
this.gradleBuild.script("src/docs/gradle/packaging/boot-jar-layered-disabled").build("bootJar");
File file = new File(this.gradleBuild.getProjectDir(),
"build/libs/" + this.gradleBuild.getProjectDir().getName() + ".jar");
assertThat(file).isFile();
try (JarFile jar = new JarFile(file)) {
JarEntry entry = jar.getJarEntry("BOOT-INF/layers.idx");
assertThat(entry).isNotNull();
assertThat(Collections.list(jar.entries()).stream().map(JarEntry::getName)
.filter((name) -> name.startsWith("BOOT-INF/lib/spring-boot"))).isNotEmpty();
assertThat(entry).isNull();
}
}

View File

@@ -63,26 +63,34 @@ class BootJarIntegrationTests extends AbstractBootArchiveIntegrationTests {
@TestTemplate
void upToDateWhenBuiltTwiceWithLayers() throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("-Playered=true", "bootJar").task(":bootJar").getOutcome())
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-Playered=true", "bootJar").task(":bootJar").getOutcome())
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.UP_TO_DATE);
}
@TestTemplate
void upToDateWhenBuiltWithDefaultLayeredAndThenWithExplicitLayered()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.UP_TO_DATE);
}
@TestTemplate
void notUpToDateWhenBuiltWithoutLayersAndThenWithLayers()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-Playered=true", "bootJar").task(":bootJar").getOutcome())
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "-PdisableLayers=true", "bootJar").task(":bootJar")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
}
@TestTemplate
void notUpToDateWhenBuiltWithLayersAndToolsAndThenWithLayersAndWithoutTools()
void notUpToDateWhenBuiltWithLayerToolsAndThenWithoutLayerTools()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure {
assertThat(this.gradleBuild.build("-Playered=true", "bootJar").task(":bootJar").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-Playered=true", "-PexcludeTools=true", "bootJar").task(":bootJar")
assertThat(this.gradleBuild.build("bootJar").task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(this.gradleBuild.build("-PcustomizeLayered=true", "-PexcludeTools=true", "bootJar").task(":bootJar")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}

View File

@@ -83,6 +83,30 @@ class BootJarTests extends AbstractBootArchiveTests<TestBootJar> {
}
}
@Test
void jarShouldBeLayeredByDefault() throws IOException {
addContent();
executeTask();
BootJar bootJar = getTask();
try (JarFile jarFile = new JarFile(bootJar.getArchiveFile().get().getAsFile())) {
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Classes"))
.isEqualTo("BOOT-INF/classes/");
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Lib"))
.isEqualTo("BOOT-INF/lib/");
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Classpath-Index"))
.isEqualTo("BOOT-INF/classpath.idx");
assertThat(jarFile.getManifest().getMainAttributes().getValue("Spring-Boot-Layers-Index"))
.isEqualTo("BOOT-INF/layers.idx");
assertThat(getEntryNames(jarFile)).contains("BOOT-INF/lib/" + JarModeLibrary.LAYER_TOOLS.getName());
}
}
@Test
void jarWhenLayersDisabledShouldNotContainLayersIndex() throws IOException {
List<String> entryNames = getEntryNames(createLayeredJar((configuration) -> configuration.setEnabled(false)));
assertThat(entryNames).doesNotContain("BOOT-INF/layers.idx");
}
@Test
void whenJarIsLayeredThenManifestContainsEntryForLayersIndexInPlaceOfClassesAndLib() throws IOException {
try (JarFile jarFile = new JarFile(createLayeredJar())) {