使用spring boot创建fat jar APP
2023-09-11 14:14:33 时间
介绍
在很久很很久以前,我们部署web程序的方式是怎么样的呢?配置好服务器,将自己写的应用程序打包成war包,扔进服务器中指定的目录里面。当然免不了要配置一些负责的xml和自定义一些servlet。
现在有了spring boot,一切都变了,我们可以将web应用程序打包成fat jar包,直接运行就行了。
本文将会关注于怎么使用Spring Boot创建一个fat jar包。
所有你需要做的就是添加如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
build和run
有了上面的配置,只需要使用
mvn clean install
就可以生成相应的jar包了。
如果要运行它,使用:
java -jar <artifact-name>
即可。非常简洁。
如果你要在服务器上面永久运行该服务,即使登录的用户退出服务器,则可以使用nohup命令:
nohup java -jar <artifact-name>
fat jar和 fat war
在上面的例子中,所有的依赖jar包都会被打包进入这一个fat jar中,如果你使用了tomcat,那么tomcat也会被打包进去。
但有时候我们还是需要打包成war包,部署在服务器中,这种情况只需要将pom.xml中的packaging属性修改为war即可。
更多配置
大多情况下,我们不需要额外的配置,如果我们有多个main class,我们需要指定具体的哪个类:
<properties>
<start-class>com.flydean.FatJarApp</start-class>
</properties>
如果你没有从spring-boot-starter-parent继承,那么你需要将main class添加到maven plugin中:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.flydean.FatJarApp</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
有些情况下,你需要告诉maven来unpack一些依赖:
<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>
本文的代码请参考https://github.com/ddean2009/learn-springboot2/tree/master/springboot-fatjar
更多精彩内容且看:
- 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新
- java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程
更多教程请参考 flydean的博客
相关文章
- 面试(4)-spring-Spring面试题和答案
- Spring Boot国际化支持
- 解决IDEA使用Spring Initializr创建项目时,无法连接到https://start.spring.io的问题
- 基于Struts2、Spring、Hibernate实现的包括多条件查询分页的基础Dao层帮助jar包实现
- 《深入实践Spring Boot》一1.3 使用Spring Boot
- [转]Spring——jar包详解
- spring注解之@Import注解的三种使用方式(转载)
- MAVEN 打 JAR包 注意地方(怎么把依赖打到Jar)
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
- Spring Boot 2.x 中的 Actuator
- 使用spring boot创建fat jar APP
- Spring Cloud Netflix Zuul 重试会自动跳过经常超时的服务实例的简单说明和分析
- 通过spring,在项目的任意位置获取当前Request
- Spring MVC 学习笔记 spring mvc Schema-based configuration
- spring框架漏洞整理(Spring Cloud Config路径穿越导致的信息泄露)
- 【Spring常见错误】Initialization failed for ‘https://start.spring.io‘
- Spring MVC 的xml一些配置
- 【spring-bean】浅谈Spring的依赖注入(基础)
- 【spring mvc】Spring MVC 的参数解析器ArgumentResolver阐述
- 曹工说Spring Boot源码(25)-- Spring注解扫描的瑞士军刀,ASM + Java Instrumentation,顺便提提Jar包破解
- 曹工杂谈:Linux服务器上,Spring Boot 原地修改 jar 包配置文件/替换class文件,免去重复上传的麻烦
- spring-cloud-gateway获取post请求body参数以及响应数据