springboot打包部署到linux服务器

本文介绍了如何将SpringBoot应用打包成jar并部署到Linux服务器。首先,由于SpringBoot内置Tomcat,故使用jar打包。接着,启动类需继承SpringBootServletInitializer并重写configure方法。通过Maven进行clean和install操作生成jar。将jar复制到服务器,确保已安装JDK并开放相应端口。使用nohup命令后台运行应用,完成部署。若需关闭服务,可通过查询进程号并kill。

1.由于springboot集成了tomcat,所以打包的时候不再使用war,而是使用jar

<groupId>cn</groupId>
	<artifactId>back</artifactId>
	<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<!--忽略test打包  -->
<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.将springboot启动类添加继承SpringBootServletInitializer,并且重写configure方法

public class BackApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(BackApplication.class, args);
	}
	
	@Override//为了打包springboot项目
    protected SpringApplicationBuilder configure(
            Sprin
### 打包部署 Spring Boot 项目至 Linux 服务器 #### 准备工作 确保本地开发环境已安装 Maven 或 Gradle 构建工具,用于构建和打包应用程序。此外,在目标 Linux 服务器上需预先配置好 Java 运行环境。 #### 使用 IDE 打包 Spring Boot 应用程序 通过 IntelliJ IDEA 的图形界面操作来简化打包流程。打开项目后,导航到 `File` -> `Project Structure...` -> `Artifacts`,确认存在针对当前工程设置好的 JAR 文件生成方案[^3]。 对于基于 Maven 的项目,则可以在终端执行如下命令完成编译与打包过程: ```bash mvn clean package -DskipTests=true ``` 该指令会跳过单元测试环节以加快速度,并最终于 `/target/` 目录下产出可执行的 `.jar` 文件[^2]。 #### 将 Jar 文件传输至远程主机 利用 SCP (Secure Copy Protocol) 命令实现安全的数据迁移: ```bash scp /path/to/your-application.jar username@remotehost:/desired/path/ ``` 这里需要替换实际路径以及登录凭证信息。 #### 设置服务自启及后台运行 为了保证应用能在重启之后自动恢复运作状态,建议创建 Systemd service unit file 来管理进程生命周期。编辑器中输入以下内容保存为 `/etc/systemd/system/spring-boot-app.service`: ```ini [Unit] Description=My Spring Boot Application Service After=syslog.target [Service] User=<username> ExecStart=/usr/bin/java -jar /path-to-your-jar-file/application.jar SuccessExitStatus=143 Restart=on-failure [Install] WantedBy=multi-user.target ``` 记得赋予适当权限给指定用户,并启用此服务以便跟随系统一同启动: ```bash sudo chmod 664 /etc/systemd/system/spring-boot-app.service sudo systemctl daemon-reload sudo systemctl enable spring-boot-app.service sudo systemctl start spring-boot-app.service ``` 如果只是临时需求让其在后台持续运行而不必每次都手动开启的话,可以采用 nohup 方式启动: ```bash nohup java -jar application.jar & ``` 这将使得即使关闭 SSH 终端连接也不会影响正在运行的应用实例[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值