Create a Fat Jar App with Spring Boot – 用Spring Boot创建一个胖罐子应用程序

最后修改: 2016年 12月 8日


1. Introduction


One of the more heart-warming developments of recent years has been an ongoing simplification of how web applications are deployed.


Skipping all the boring intermediate historical steps, we arrive at today – when we can dispense with not only cumbersome servlets and XML boilerplate, but mostly the servers themselves.


This article will concentrate on creating a “fat jar” out of a Spring Boot application – basically to create a single artifact that is easy to deploy and run.

本文将集中讨论从Spring Boot应用程序中创建一个”fat jar”–基本上是为了创建一个易于部署和运行的单一构件。

Boot provides capabilities for container-less deployments right out of the box: all we need to do is to add a couple of configurations in the pom.xml:




2. Build and Run


With this configuration, we can now simply build the project with the standard mvn clean install – nothing unusual here.

有了这个配置,我们现在可以简单地用标准的mvn clean install构建项目–这里没有什么不寻常的。

And we run it with the following command: java -jar <artifact-name> – very simple and intuitive.

而我们用下面的命令来运行它。java -jar <artifact-name> – 非常简单和直观。

Proper process management is beyond the scope of this article, but one simple way to keep the process running even when we logoff the server is to use the nohup command: nohup java -jar <artifact-name>.

正确的进程管理超出了本文的范围,但有一个简单的方法可以让进程在我们注销服务器时也能继续运行,那就是使用nohup命令。nohup java -jar <artifact-name> .

Stopping spring-boot projects is also no different than stopping a regular process, whether we simply cntrl+c or kill <pid>.

停止spring-boot项目也与停止普通进程没有区别,无论我们是简单地cntrl+c还是kill <pid>.

3. Fat Jar / Fat War


Behind the scenes, spring-boot packages all the project dependencies inside the final artifact along side project classes (hence the “fat” jar). An embedded Tomcat server is also built-in.

在幕后,spring-boot将所有的项目依赖性与项目类一起打包在最终的工件中(因此称为 “胖 “jar)。一个嵌入式的Tomcat服务器也被内置。

And thus, the resulting artifact is completely self-contained, easy to deploy using standard Unix tools (scp, sftp…etc) and can be run on any server with a JVM.


By default, Boot creates a jar file – but if we change the packaging property in pom.xml to war, Maven will instead naturally build a war.


This will of course be both executable as stand-alone, and deployed to a web container.


4. Further Config


Most of the time no additional configuration is needed, everything “just works”, but in some specific cases, we may need to tell spring-boot explicitly what the main class is. One way to do it would be to add a property:

大多数时候不需要额外的配置,一切都 “刚刚好”,但在某些特殊情况下,我们可能需要明确告诉spring-boot主类是什么。一种方法是添加一个属性。


In case we’re not inheriting spring-boot-starter-parent we’ll need to do it in the Maven plugin:



Another thing we might need to do in some rare cases is to instruct Maven to unpack some dependencies:

在一些罕见的情况下,我们可能需要做的另一件事是指示Mavenunpack 一些依赖。


5. Conclusion


In this article, we looked at server-less deployment using “fat” jars built by spring-boot.

在这篇文章中,我们研究了使用由spring-boot.构建的 “胖 “罐子进行无服务器部署。

As always, the code in this writeup is all available over on Github.