Post

Spring Boot Executable Jar and Fat Jar

沒有框架時,常見做法是把 class 與所有相依函式庫打成一個 jar-with-dependencies.jar

原因很單純:一般 java -jar 只看 manifest 的 Main-Class,classpath 要自己處理;把相依函式庫攤進同一包,可以少帶一堆 lib/*.jar

但 Spring Boot 不需要這樣做。

Spring Boot Maven plugin 會產生可執行 jar,manifest 會指向 Boot loader:

1
2
Main-Class: org.springframework.boot.loader.launch.JarLauncher
Start-Class: com.example.Main

相依函式庫會放在:

1
BOOT-INF/lib/

所以交付 Spring Boot CLI batch 時,應交付 spring-boot:repackage 產生的 jar,而不是額外再做 jar-with-dependencies

後者可能變成「Boot 目錄結構」加上「一般 Java Main-Class」的混合包,結果 java -jar 反而找不到主程式。

This post is licensed under CC BY 4.0 by the author.