Quick revision
The short answer
A layered jar is a Spring Boot jar that records, in BOOT-INF/layers.idx, how its contents split into layers: dependencies, spring-boot-loader, snapshot-dependencies and application. When we build a Docker image, each of those becomes a separate image layer, and because dependencies rarely change, their large layer stays cached and only the small application layer is rebuilt and pushed on most changes. On recent Boot versions we extract the layers with java -Djarmode=tools -jar app.jar extract --layers; older versions used the layertools jar mode.
What to remember
In this answer, we saw that Docker caches images layer by layer, so a jar copied as one file produces a new full-size layer on every commit. A layered jar records in BOOT-INF/layers.idx a split into dependencies, spring-boot-loader, snapshot-dependencies and application, from least to most often changed. Extracted with java -Djarmode=tools -jar app.jar extract --layers, or layertools on older versions, each becomes its own image layer, so most changes rebuild and push only the small application layer.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A layered jar is a Spring Boot jar that records, in BOOT-INF/layers.idx, how its contents split into layers: dependencies, spring-boot-loader, snapshot-dependencies and application. When we build a Docker image, each of those becomes a separate image layer, and because dependencies rarely change, their large layer stays cached and only the small application layer is rebuilt and pushed on most changes. On recent Boot versions we extract the layers with java -Djarmode=tools -jar app.jar extract --layers; older versions used the layertools jar mode.
Why this question is asked
A typical Spring Boot jar is 60 to 120 MB, and almost all of it is libraries that did not change since the last build. If the Dockerfile copies the jar as one file, every commit produces a new 100 MB layer that the CI has to build, the registry has to store and every node has to pull. Interviewers ask about layered jars because they are a simple idea that makes builds and deployments noticeably faster. In this answer, we will recap how Docker layers are cached, see why a single fat jar defeats the cache, look at the four layers Boot defines, and extract them.
1A Quick Recap of Docker Layers
A Docker image is a stack of layers. Each COPY or RUN instruction in a Dockerfile creates one layer, a set of file changes identified by a hash of its contents. When we build again, Docker reuses a cached layer if the instruction and its input files are unchanged, and it must rebuild that layer and every layer after it once something changes. Registries and nodes work the same way: a push uploads only layers the registry does not have, and a pull downloads only layers the node does not have.
So the cost of a change depends on how big the layer is that the change touches, and on its position: changing an early layer invalidates everything after it.
2Why One Fat Jar Defeats the Cache
The simplest Dockerfile copies the jar as one file.
The jar is a single file, so any change to any class inside it changes the file's hash, and Docker treats it as a completely new layer. Of those 95 MB, perhaps 94 MB are Spring, Hibernate, Jackson and the other libraries, which were identical in the last build. We push and pull them again anyway, on every commit and to every node.
3The Four Layers Boot Defines
A layered jar solves this by telling the build which parts change at which rate. Since Spring Boot 2.4 the Maven and Gradle plugins write a layers index into every jar by default, and it groups the contents into four layers, ordered from least to most often changed.
| Layer | Typical size | Changes when |
|---|---|---|
| dependencies | 90 MB | a library version is upgraded |
| spring-boot-loader | 0.5 MB | Spring Boot is upgraded |
| snapshot-dependencies | 2 MB | a SNAPSHOT library is rebuilt |
| application | 0.4 MB | almost every commit |
dependencies holds released third-party and internal libraries, the biggest and most stable part. spring-boot-loader holds Boot's launcher classes, which change only when we upgrade Boot. snapshot-dependencies holds libraries with a SNAPSHOT version, which by definition can change between builds, so they get their own layer instead of invalidating the big one. application holds our own classes, resources and the index files, which change on nearly every commit but are usually only a few hundred kilobytes.
4Extracting the Layers for a Dockerfile
To turn the index into image layers, we unpack the jar into one directory per layer and copy each directory with its own COPY instruction. Boot ships a jar mode for that, which runs a small tool built into the jar instead of the application.
In a Dockerfile, we copy dependencies first and application last, so that a code change invalidates only the last COPY. The tools mode also produces an application jar that can be started with java -jar on the extracted layout, and layertools was deprecated in favour of it, so new Dockerfiles should use tools. If our project needs a different split, for example a separate layer for company libraries that change weekly, we can define custom layers in the build plugin's configuration.
The effect is easy to measure. For a service with 90 MB of dependencies, a typical commit pushes about 0.5 MB instead of 95 MB, and nodes that already run the previous version pull almost nothing. Builds get faster too, because Docker reuses the cached dependency layer. Buildpacks, through mvn spring-boot:build-image, use the same layers index automatically.
5Follow-up questions
Do we need to enable layering in the build?
Not on current versions. Since Spring Boot 2.4 the layers index is written by default; in 2.3 it had to be enabled in the plugin. We can switch it off or define custom layers in the Maven or Gradle plugin configuration if the default split does not suit the project.
Why is snapshot-dependencies a separate layer?
A SNAPSHOT dependency can change between builds without a version change, for example an internal library under active development. If it sat in the dependencies layer, every rebuild of that library would invalidate the big layer. Keeping it separate means only that small layer is rebuilt.
Does layering help if we change a library version?
Only partly. Upgrading any dependency changes the dependencies layer, so that 90 MB layer is rebuilt and pushed once. Since upgrades are much rarer than code commits, the cache still saves most of the transfer over time. Teams with many internal libraries sometimes add a custom layer for them for this reason.
6Conclusion
In this answer, we saw that Docker caches images layer by layer, so a jar copied as one file produces a new full-size layer on every commit. A layered jar records in BOOT-INF/layers.idx a split into dependencies, spring-boot-loader, snapshot-dependencies and application, from least to most often changed. Extracted with java -Djarmode=tools -jar app.jar extract --layers, or layertools on older versions, each becomes its own image layer, so most changes rebuild and push only the small application layer.
Open with
A layered jar is a normal Boot jar with an extra index file, BOOT-INF/layers.idx, which groups its contents by how often they change. By default there are four layers: dependencies, which are the released libraries and usually most of the jar; spring-boot-loader, the launcher classes; snapshot-dependencies, libraries with a SNAPSHOT version that can change between builds; and application, our own classes and resources.
Then, if they want more
The reason this speeds up Docker builds is the layer cache. Docker caches each COPY as a layer keyed by its content. If we copy the jar as one file, any code change gives a new hash, so the whole 100 megabyte layer is rebuilt, pushed and pulled again.
If we extract the jar into the four directories and copy each one separately, dependencies first and application last, then a typical commit changes only the application layer, often under a megabyte, and the big dependency layer is reused from cache, in the registry and on the nodes.
To extract it, on Boot 3.3 and later I run java -Djarmode=tools -jar app.jar extract --layers; older versions used the layertools jar mode with the extract command. The index is written by default since Boot 2.4, and buildpacks use it automatically.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 2.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are layered jars and why do they speed up Docker image builds?": https://scrivio-interview-library.netlify.app/spring-boot/packaging-and-deployment/f8cc7dd3f1/