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

最后修改: 2016年 12月 8日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

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.

跳过所有无聊的中间历史步骤,我们来到了今天–当我们不仅可以免除繁琐的Servlet和XML模板,而且主要是免除服务器本身。

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:

Boot提供了开箱即用的无容器部署功能:我们所要做的就是在pom.xml中添加几个配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.4.0</version>
    </plugin>
</plugins>

2. Build and Run

2.建立和运行

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

3.脂肪罐/脂肪战争

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.

因此,所产生的工件是完全独立的,易于使用标准的Unix工具(scp、sftp…等)进行部署,并且可以在任何有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.

默认情况下,Boot会创建一个jar文件–但如果我们把pom.xml中的packaging属性改为war,Maven就会自然地构建一个war

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

当然,这既可以作为独立的执行,也可以部署到一个网络容器。

4. Further Config

4.进一步配置

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主类是什么。一种方法是添加一个属性。

<properties>
    <start-class>org.baeldung.boot.Application</start-class>
</properties>

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

如果我们没有继承spring-boot-starter-parent,我们就需要在Maven插件中进行。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.4.0</version>
    <configuration>
        <mainClass>org.baeldung.boot.Application</mainClass>
        <layout>ZIP</layout>
    </configuration>
</plugin>

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

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

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <requiresUnpack>
            <dependency>
                <groupId>org.jruby</groupId>
                <artifactId>jruby-complete</artifactId>
            </dependency>
        </requiresUnpack>
    </configuration>
</plugin>

5. Conclusion

5.结论

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.

像往常一样,本报告中的代码都可以在Github上找到,