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

@ -275,19 +275,6 @@ By default, the `bootJar` task builds an archive that contains the application's
For cases where a docker image needs to be built from the contents of the jar, it's useful to be able to separate these directories further so that they can be written into distinct layers.
Layered jars use the same layout as regular boot packaged jars, but include an additional meta-data file that describes each layer.
To use this feature, the layering feature must be enabled:
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Groovy
----
include::../gradle/packaging/boot-jar-layered.gradle[tags=layered]
----
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
.Kotlin
----
include::../gradle/packaging/boot-jar-layered.gradle.kts[tags=layered]
----
By default, the following layers are defined:
@ -300,7 +287,21 @@ The layers order is important as it determines how likely previous layers can be
The default order is `dependencies`, `spring-boot-loader`, `snapshot-dependencies`, `application`.
Content that is least likely to change should be added first, followed by layers that are more likely to change.
When you create a layered jar, the `spring-boot-jarmode-layertools` jar will be added as a dependency to your jar.
To disable this feature, you can do so in the following manner:
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Groovy
----
include::../gradle/packaging/boot-jar-layered-disabled.gradle[tags=layered]
----
[source,kotlin,indent=0,subs="verbatim,attributes",role="secondary"]
.Kotlin
----
include::../gradle/packaging/boot-jar-layered-disabled.gradle.kts[tags=layered]
----
When a layered jar is created, the `spring-boot-jarmode-layertools` jar will be added as a dependency to your jar.
With this jar on the classpath, you can launch your application in a special mode which allows the bootstrap code to run something entirely different from your application, for example, something that extracts the layers.
If you wish to exclude this dependency, you can do so in the following manner:

View File

@ -11,6 +11,8 @@ tasks.getByName<BootJar>("bootJar") {
// tag::layered[]
tasks.getByName<BootJar>("bootJar") {
layered()
layered {
isEnabled = false
}
}
// end::layered[]

View File

@ -64,7 +64,7 @@ public class BootJar extends Jar implements BootArchive {
private FileCollection classpath;
private LayeredSpec layered;
private LayeredSpec layered = new LayeredSpec();
/**
* Creates a new {@code BootJar} task.
@ -98,13 +98,17 @@ public class BootJar extends Jar implements BootArchive {
@Override
public void copy() {
this.support.configureManifest(getManifest(), getMainClassName(), CLASSES_DIRECTORY, LIB_DIRECTORY,
CLASSPATH_INDEX, (this.layered != null) ? LAYERS_INDEX : null);
CLASSPATH_INDEX, (isLayeredDisabled()) ? null : LAYERS_INDEX);
super.copy();
}
private boolean isLayeredDisabled() {
return this.layered != null && !this.layered.isEnabled();
}
@Override
protected CopyAction createCopyAction() {
if (this.layered != null) {
if (!isLayeredDisabled()) {
JavaPluginConvention javaPluginConvention = getProject().getConvention()
.findPlugin(JavaPluginConvention.class);
Iterable<SourceSet> sourceSets = (javaPluginConvention != null) ? javaPluginConvention.getSourceSets()

View File

@ -52,6 +52,8 @@ public class LayeredSpec {
private boolean includeLayerTools = true;
private boolean enabled = true;
private ApplicationSpec application = new ApplicationSpec();
private DependenciesSpec dependencies = new DependenciesSpec();
@ -80,6 +82,24 @@ public class LayeredSpec {
this.includeLayerTools = includeLayerTools;
}
/**
* Returns whether the layers.idx should be included in the jar.
* @return whether the layers.idx should be included
*/
@Input
public boolean isEnabled() {
return this.enabled;
}
/**
* Sets whether the layers.idx should be included in the jar.
* @param enabled {@code true} layers.idx should be included in the jar, otherwise
* {@code false}
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Returns the {@link ApplicationSpec} that controls the layers to which application
* classes and resources belong.

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())) {

View File

@ -15,3 +15,9 @@ dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.9")
implementation("commons-io:commons-io:2.6")
}
bootJar {
layered {
enabled = false
}
}

View File

@ -16,3 +16,9 @@ dependencies {
developmentOnly("commons-io:commons-io:2.6")
implementation("commons-io:commons-io:2.6")
}
bootJar {
layered {
enabled = false
}
}

View File

@ -10,9 +10,10 @@ bootJar {
properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default'
}
}
if (project.hasProperty('layered') && project.getProperty('layered')) {
if (project.hasProperty('customizeLayered') && project.getProperty('customizeLayered')) {
layered {
includeLayerTools = project.hasProperty('excludeTools') && project.getProperty('excludeTools') ? false : true
enabled = project.hasProperty('disableLayers') && project.getProperty('disableLayers') ? false : true
}
}
}

View File

@ -168,18 +168,25 @@ public abstract class Packager {
protected final void write(JarFile sourceJar, Libraries libraries, AbstractJarWriter writer) throws IOException {
Assert.notNull(libraries, "Libraries must not be null");
WritableLibraries writeableLibraries = new WritableLibraries(libraries);
if (this.layers != null) {
if (isLayersEnabled()) {
writer = new LayerTrackingEntryWriter(writer);
}
writer.writeManifest(buildManifest(sourceJar));
writeLoaderClasses(writer);
writer.writeEntries(sourceJar, getEntityTransformer(), writeableLibraries);
writeableLibraries.write(writer);
if (this.layers != null) {
if (isLayersEnabled()) {
writeLayerIndex(writer);
}
}
private boolean isLayersEnabled() {
if (!(getLayout() instanceof Layouts.Jar)) {
return false;
}
return this.layers != null;
}
private void writeLoaderClasses(AbstractJarWriter writer) throws IOException {
Layout layout = getLayout();
if (layout instanceof CustomLoaderLayout) {
@ -332,7 +339,7 @@ public abstract class Packager {
attributes.putValue(BOOT_CLASSES_ATTRIBUTE, layout.getRepackagedClassesLocation());
putIfHasLength(attributes, BOOT_LIB_ATTRIBUTE, getLayout().getLibraryLocation("", LibraryScope.COMPILE));
putIfHasLength(attributes, BOOT_CLASSPATH_INDEX_ATTRIBUTE, layout.getClasspathIndexFileLocation());
if (this.layers != null) {
if (isLayersEnabled()) {
putIfHasLength(attributes, BOOT_LAYERS_INDEX_ATTRIBUTE, layout.getLayersIndexFileLocation());
}
}
@ -465,7 +472,7 @@ public abstract class Packager {
addLibrary(library);
}
});
if (Packager.this.layers != null && Packager.this.includeRelevantJarModeJars) {
if (isLayersEnabled() && Packager.this.includeRelevantJarModeJars) {
addLibrary(JarModeLibrary.LAYER_TOOLS);
}
}

View File

@ -80,7 +80,20 @@ A repackaged jar contains the application's classes and dependencies in `BOOT-IN
For cases where a docker image needs to be built from the contents of the jar, it's useful to be able to separate these directories further so that they can be written into distinct layers.
Layered jars use the same layout as regular repackaged jars, but include an additional meta-data file that describes each layer.
To use this feature, the layering feature must be enabled:
By default, the following layers are defined:
* `dependencies` for any dependency whose version does not contain `SNAPSHOT`.
* `spring-boot-loader` for the jar loader classes.
* `snapshot-dependencies` for any dependency whose version contains `SNAPSHOT`.
* `application` for application classes and resources.
The layers order is important as it determines how likely previous layers can be cached when part of the application changes.
The default order is `dependencies`, `spring-boot-loader`, `snapshot-dependencies`, `application`.
Content that is least likely to change should be added first, followed by layers that are more likely to change.
The repackaged jar includes the `layers.idx` file by default.
To disable this feature, you can do so in the following manner:
[source,xml,indent=0,subs="verbatim,attributes"]
----
@ -93,7 +106,7 @@ To use this feature, the layering feature must be enabled:
<version>{gradle-project-version}</version>
<configuration>
<layers>
<enabled>true</enabled>
<enabled>false</enabled>
</layers>
</configuration>
</plugin>
@ -102,17 +115,6 @@ To use this feature, the layering feature must be enabled:
</project>
----
By default, the following layers are defined:
* `dependencies` for any dependency whose version does not contain `SNAPSHOT`.
* `spring-boot-loader` for the jar loader classes.
* `snapshot-dependencies` for any dependency whose version contains `SNAPSHOT`.
* `application` for application classes and resources.
The layers order is important as it determines how likely previous layers can be cached when part of the application changes.
The default order is `dependencies`, `spring-boot-loader`, `snapshot-dependencies`, `application`.
Content that is least likely to change should be added first, followed by layers that are more likely to change.
[[repackage-layers-configuration]]
@ -531,7 +533,7 @@ This example excludes any artifact belonging to the `com.foo` group:
[[repackage-layered-jars-tools]]
==== Layered Jar Tools
When you create a layered jar, the `spring-boot-jarmode-layertools` jar will be added as a dependency to your jar.
When a layered jar is created, the `spring-boot-jarmode-layertools` jar will be added as a dependency to your jar.
With this jar on the classpath, you can launch your application in a special mode which allows the bootstrap code to run something entirely different from your application, for example, something that extracts the layers.
If you wish to exclude this dependency, you can do so in the following manner:
@ -546,7 +548,6 @@ If you wish to exclude this dependency, you can do so in the following manner:
<version>{gradle-project-version}</version>
<configuration>
<layers>
<enabled>true</enabled>
<includeLayerTools>false</includeLayerTools>
</layers>
</configuration>

View File

@ -297,7 +297,7 @@ class JarIntegrationTests extends AbstractArchiveIntegrationTests {
}
@TestTemplate
void whenJarIsRepackagedWithLayersEnabledTheJarContainsTheLayersIndex(MavenBuild mavenBuild) {
void repackagedJarContainsTheLayersIndexByDefault(MavenBuild mavenBuild) {
mavenBuild.project("jar-layered").execute((project) -> {
File repackaged = new File(project, "jar/target/jar-layered-0.0.1.BUILD-SNAPSHOT.jar");
assertThat(jar(repackaged)).hasEntryWithNameStartingWith("BOOT-INF/classes/")
@ -316,6 +316,18 @@ class JarIntegrationTests extends AbstractArchiveIntegrationTests {
});
}
@TestTemplate
void whenJarIsRepackagedWithTheLayersDisabledDoesNotContainLayersIndex(MavenBuild mavenBuild) {
mavenBuild.project("jar-layered-disabled").execute((project) -> {
File repackaged = new File(project, "jar/target/jar-layered-0.0.1.BUILD-SNAPSHOT.jar");
assertThat(jar(repackaged)).hasEntryWithNameStartingWith("BOOT-INF/classes/")
.hasEntryWithNameStartingWith("BOOT-INF/lib/jar-release")
.hasEntryWithNameStartingWith("BOOT-INF/lib/jar-snapshot")
.doesNotHaveEntryWithName("BOOT-INF/layers.idx")
.doesNotHaveEntryWithNameStartingWith("BOOT-INF/lib/" + JarModeLibrary.LAYER_TOOLS.getName());
});
}
@TestTemplate
void whenJarIsRepackagedWithTheLayersEnabledAndLayerToolsExcluded(MavenBuild mavenBuild) {
mavenBuild.project("jar-layered-no-layer-tools").execute((project) -> {
@ -323,6 +335,7 @@ class JarIntegrationTests extends AbstractArchiveIntegrationTests {
assertThat(jar(repackaged)).hasEntryWithNameStartingWith("BOOT-INF/classes/")
.hasEntryWithNameStartingWith("BOOT-INF/lib/jar-release")
.hasEntryWithNameStartingWith("BOOT-INF/lib/jar-snapshot")
.hasEntryWithNameStartingWith("BOOT-INF/layers.idx")
.doesNotHaveEntryWithNameStartingWith("BOOT-INF/lib/" + JarModeLibrary.LAYER_TOOLS.getName());
});
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-release</artifactId>
<version>0.0.1.RELEASE</version>
<packaging>jar</packaging>
<name>jar</name>
<description>Release Jar dependency</description>
</project>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-snapshot</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jar</name>
<description>Snapshot Jar dependency</description>
</project>

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-layered</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<layers>
<enabled>false</enabled>
</layers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-snapshot</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>jar-release</artifactId>
<version>0.0.1.RELEASE</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
public class SampleApplication {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>aggregator</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<modules>
<module>jar-snapshot</module>
<module>jar-release</module>
<module>jar</module>
</modules>
</project>

View File

@ -23,7 +23,6 @@
</goals>
<configuration>
<layers>
<enabled>true</enabled>
<includeLayerTools>false</includeLayerTools>
</layers>
</configuration>

View File

@ -21,11 +21,6 @@
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
</configuration>
</execution>
</executions>
</plugin>

View File

@ -135,7 +135,10 @@ public abstract class AbstractPackagerMojo extends AbstractDependencyFilterMojo
getLog().info("Layout: " + this.layout);
packager.setLayout(this.layout.layout());
}
if (this.layers != null && this.layers.isEnabled()) {
if (this.layers == null) {
packager.setLayers(IMPLICIT_LAYERS);
}
else if (this.layers.isEnabled()) {
packager.setLayers((this.layers.getConfiguration() != null)
? getCustomLayers(this.layers.getConfiguration()) : IMPLICIT_LAYERS);
packager.setIncludeRelevantJarModeJars(this.layers.isIncludeLayerTools());

View File

@ -26,7 +26,7 @@ import java.io.File;
*/
public class Layers {
private boolean enabled;
private boolean enabled = true;
private boolean includeLayerTools = true;