# Packaging and Running — Spring Boot

Source: https://www.skillbyai.com/en/spring-boot/o-package

> Executable jars, containers, native images.

## java -jar and beyond

The Spring Boot Maven or Gradle plugin builds an **executable jar** containing the app and its dependencies, run with `java -jar`. For containers, `mvn spring-boot:build-image` creates an OCI image using Cloud Native Buildpacks, or you can write a Dockerfile using the layered jar for efficient caching. **GraalVM native images** start in milliseconds with low memory, at the cost of longer builds and some reflection constraints. Give the JVM sensible memory limits in containers.

## Build and run commands

mvn package and java -jar were run for this course; the other commands need Docker or GraalVM and were not run.

```bash
mvn package                                   # builds target/shop-0.0.1-SNAPSHOT.jar (run)
java -jar target/shop-0.0.1-SNAPSHOT.jar      # starts the embedded server (run)
mvn spring-boot:build-image                   # OCI image via buildpacks (not run)
mvn -Pnative native:compile                   # GraalVM native executable (not run)
```

## Use layered jars in Dockerfiles

Copying dependencies and application classes as separate layers makes image rebuilds much faster.

**Quiz:** What does a Spring Boot executable jar contain?

- [ ] A database dump
- [ ] Only your source code
- [ ] Only configuration files
- [x] The application plus its dependencies and an embedded server

*Answer:* The application plus its dependencies and an embedded server. Self-contained deployment unit.
