一、简介
在本文中,我们将介绍如何从 Spring Boot 应用程序中创建可运行的 JAR 文件。Spring Boot 的最佳特性之一是嵌入式 Web 容器,它允许我们将具有依赖关系的整个应用程序打包到一个 jar 文件中,以便部署和运行。
2.Maven配置
要fat jar
使用 Spring Boot 创建一个,我们需要spring-boot-maven-plugin
在我们的pom.xml
文件中添加一个:
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/>
</parent>
<groupId>com.frontbackend.springboot</groupId>
<artifactId>getting-started</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>getting-started</name>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
使用该 Maven 配置,我们可以使用mvn clean install
. 要运行我们的应用程序,我们只需在目标目录中执行生成的 jar 文件。
复制
java -jar target/getting-started-0.0.1-SNAPSHOT.jar
我们应该在控制台上看到 Spring Boot 开始输出日志:
复制
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.0)
2021-05-27 22:20:05.735 INFO 567 --- [ main] c.f.s.GettingStartedApplication : Starting GettingStartedApplication v0.0.1-SNAPSHOT using Java 1.8.0_231 on frontbackend with PID 567 (/home/tutorials/spring-boot/getting-started/target/getting-started-0.0.1-SNAPSHOT.jar started by marcinw in /home/tutorials/spring-boot/getting-started/target)
2021-05-27 22:20:05.737 INFO 567 --- [ main] c.f.s.GettingStartedApplication : No active profile set, falling back to default profiles: default
2021-05-27 22:20:06.768 INFO 567 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-05-27 22:20:06.781 INFO 567 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-05-27 22:20:06.781 INFO 567 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-05-27 22:20:06.839 INFO 567 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-05-27 22:20:06.839 INFO 567 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1033 ms
2021-05-27 22:20:07.232 INFO 567 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-27 22:20:07.246 INFO 567 --- [ main] c.f.s.GettingStartedApplication : Started GettingStartedApplication in 1.872 seconds (JVM running for 2.291)
2021-05-27 22:20:07.247 INFO 567 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-05-27 22:20:07.249 INFO 567 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
要停止应用程序,我们需要找到该进程并终止它:
复制
> ps aux | grep java
frontbackend 567 11.6 0.8 12252152 289796 pts/14 Sl+ 22:20 0:09 java -jar getting-started-0.0.1-SNAPSHOT.jar
> sudo kill -9 567
3.附加的Maven配置
如果我们没有spring-boot-starter-parent
在pom.xml
文件中继承,我们仍然可以创建可执行的 Spring Boot jar,但在 Maven 插件中添加一个主类:
复制
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.frontbackend.springboot.GettingStartedApplication</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
我们也可以在属性部分指出该类:
复制
<properties>
<start-class>com.frontbackend.springboot.GettingStartedApplication</start-class>
</properties>
请注意,如果我们使用正确的注释并且在pom.xml
我们继承自的文件中,spring-boot-starter-parent
我们不需要添加与主类相关的值。
4。结论
在本文中,我们介绍了一种在可运行 JAR 文件中构建 Spring Boot 的简单方法。