AI Hints
Home›Spring Boot Tutorial›Actuator, Health and Info and Packaging a Jar
Spring Boot · Lesson 111 of 120

Packaging a Jar

One file to run anywhere.

⏱ 2 min read▶ Video at 2:27:55🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

So far, Maven has run our app for us. But a server doesn't have our source code. It needs one file, that it can simply run.

  • Maven ran it so far
  • Servers need one file
  • A runnable jar

An executable jar

Maven package compiles the code, runs the tests, and builds a jar. Spring Boot puts everything inside, your classes, every library, and Tomcat. So the jar runs anywhere Java is installed. With java dash jar.

keyvalue
Build./mvnw package
Insideyour code + libraries + Tomcat
Needsonly Java
Runjava -jar app.jar

Build it, then run it

Package builds the jar, into the target folder. Dash D skip tests skips the tests, just to be quick here. Then java dash jar starts the app, with no Maven at all. Run it. Maven builds the jar. It's about fifty four megabytes, because it has everything inside. The app starts from the jar. And health says UP.

terminalbash
./mvnw package -DskipTests
du -h target/*.jar
java -jar target/bookshop-0.0.1-SNAPSHOT.jar
curl localhost:8080/actuator/health
Output
[INFO] Building jar: target/bookshop-0.0.1-SNAPSHOT.jar
[INFO] BUILD SUCCESS
 54M    target/bookshop-0.0.1-SNAPSHOT.jar
INFO  BookshopApplication : Started BookshopApplication in 2.1 seconds
{"groups":["liveness","readiness"],"status":"UP"}
Also uses BookController.java
@RestController
public class BookController {

    @GetMapping("/books")
    public String books() { return "Book list"; }
}
Also uses _mvn
package -DskipTests
Also uses _after
du -h target/*.jar
java -jar target/bookshop-0.0.1-SNAPSHOT.jar > jar.log 2>&1 &
until grep -q "Started BookshopApplication" jar.log; do sleep 0.3; done
grep "Started BookshopApplication" jar.log | sed -E 's/ in [0-9.]+ seconds/ in 2.1 seconds/'
curl -s localhost:8080/actuator/health; echo
kill $!

Running it anywhere

Settings can change, without rebuilding. Dash dash server dot port picks the port. An environment variable can pick a profile. And the same jar goes into a Docker image, as you'll see in other courses.

keyvalue
Portjava -jar app.jar --server.port=9090
ProfileSPRING_PROFILES_ACTIVE=prod
Same jardev, test, production
Containerscopy the jar into an image

Summary

Maven package builds one executable jar, with everything inside. Java dash jar runs it anywhere. Next, our final project.

Key takeaways
  • ./mvnw package
  • target/*.jar
  • java -jar
Next up: Project: The Plan — what we will build