Concept Explanation
Compiling produces .class files. Shipping requires a single artifact someone can actually run. There are several formats, and the differences matter at deployment time.
The Plain Jar
mvn package # → target/app-1.0.jar
This contains only your classes. Running it fails immediately with NoClassDefFoundError because none of your dependencies are inside. A plain jar is right for a library that others will depend on — their build tool resolves the dependencies.
The Fat (Executable) Jar
For an application you want to run with java -jar, everything must be inside. Spring Boot does this by default:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
mvn package
java -jar target/app-1.0.jar # runs, dependencies included
The structure is nested rather than flattened:
app.jar
├── META-INF/MANIFEST.MF (Main-Class: JarLauncher)
├── BOOT-INF/classes/ (your code)
├── BOOT-INF/lib/ (dependency jars, intact)
└── org/springframework/boot/loader/ (the launcher)
Keeping dependency jars intact avoids the classic shade-plugin problem where two libraries ship the same resource file and one silently overwrites the other.
Without Spring Boot, use the Shade plugin:
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="...ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Layered Jars for Docker
A fat jar is one giant layer, so changing one line re-uploads 60 MB. Layering splits it by change frequency:
<configuration>
<layers><enabled>true</enabled></layers>
</configuration>
FROM eclipse-temurin:21-jre AS builder
WORKDIR /app
COPY target/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=builder /app/dependencies/ ./
COPY --from=builder /app/spring-boot-loader/ ./
COPY --from=builder /app/snapshot-dependencies/ ./
COPY --from=builder /app/application/ ./ # changes most often, last
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
Dependencies change rarely and stay cached; only the small application layer is rebuilt and re-pushed.
Custom Runtimes with jlink
For non-Spring applications, jlink builds a JRE containing only the modules you use — often 40 MB instead of 300 MB:
jlink --add-modules java.base,java.sql \
--strip-debug --no-header-files --no-man-pages \
--output custom-jre
Reproducible Builds
Two builds of the same commit should produce byte-identical jars:
<properties>
<project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>
</properties>
Without this, embedded timestamps make every build differ, which defeats Docker layer caching and makes artifact verification impossible.
Common Mistakes
- Shipping a plain jar as an application:
NoClassDefFoundErroron first run. - Shade-plugin resource collisions: Two libraries with the same
META-INF/servicesfile, one silently lost. Configure a services transformer. - Secrets inside the jar:
unzipreveals everything. Configuration belongs outside the artifact. - One fat-jar Docker layer: Every deploy pushes the full image. Use layertools.
- A JDK in the runtime image: A JRE is smaller and has less attack surface.
Best Practices
- Fat jar for applications, plain jar for libraries.
- Layered jars whenever you build Docker images.
- Version in the filename and the manifest so a running artifact can be identified.
- Make builds reproducible with a fixed output timestamp.
- Never bake configuration or secrets in — inject them at runtime.
Practice Exercises
- The Failing Jar: Build a plain jar for an app with dependencies, run it, and read the exact error.
- The Layer Extract: Run
java -Djarmode=layertools -jar app.jar listand describe what each layer holds. - The Size Comparison: Compare a fat jar, a layered Docker image, and a
jlinkruntime for the same application.