以下是在服务器上部署Spring Boot项目jar包的步骤:
打包项目:
mvn clean package -DskipTests
这个命令会清除之前的构建产物,然后打包项目,并跳过测试。将jar文件上传到服务器:
/opt/myapp
。创建启动脚本(可选):
#!/bin/bash
APP_NAME=myapp
JAR_FILE=/opt/myapp/myapp.jar
PID_FILE=/var/run/$APP_NAME.pid
LOG_FILE=/var/log/$APP_NAME.log
start() {
if [ -f $PID_FILE ]; then
echo "Application is already running..."
exit 1
fi
java -jar $JAR_FILE > $LOG_FILE 2>&1 &
echo $! > $PID_FILE
echo "Application started."
}
stop() {
if [ ! -f $PID_FILE ]; then
echo "Application is not running..."
exit 1
fi
PID=$(cat $PID_FILE)
kill $PID
rm $PID_FILE
echo "Application stopped."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
将这个脚本保存为 myapp.sh
,并将其放在一个方便的位置,如 /etc/init.d/
。赋予脚本执行权限:
chmod
命令给启动脚本添加执行权限:chmod +x /etc/init.d/myapp.sh
配置系统服务(可选):
service
命令来管理应用,可以将启动脚本添加为系统服务。以下是在Ubuntu系统中使用Systemd的示例:
/etc/systemd/system/myapp.service
,内容如下:[Unit]
Description=My Spring Boot Application
After=network.target
[Service]
ExecStart=/etc/init.d/myapp.sh start
ExecStop=/etc/init.d/myapp.sh stop
User=<your_user>
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start myapp
systemctl enable myapp
启动应用:
/etc/init.d/myapp.sh start
systemctl
命令:systemctl start myapp
验证应用是否运行:
/var/log/myapp.log
来确认应用是否成功启动。以上就是将Spring Boot项目jar包部署到服务器上的基本步骤。根据你的具体需求和服务器环境,可能需要进行一些调整。