简介:
使用Jenkins自动发布jar包,这里提供一个通用的shell发布脚本
通用脚本示例:
#!/bin/bash
mvn clean install
# 定义变量,项目名称,文件地址,运行路径
PROJECT_NAME="adminxx"
JAR_FILE="/root/.jenkins/workspace/admin/target/$PROJECT_NAME.jar"
RUN_PATH="/home/java-jar"
# 停止旧的Jar包
if [ -f "$RUN_PATH/$PROJECT_NAME.jar" ]; then
PID=$(ps -ef | grep $PROJECT_NAME.jar | grep -v grep | awk '{ print $2 }')
if [ -n "$PID" ]; then
echo "Stopping the application..."
kill $PID
sleep 5s
echo "Application stopped."
fi
fi
BACKUP_DIR="$RUN_PATH/backup"
# 创建备份目录
mkdir -p $BACKUP_DIR
# 备份旧的Jar包
if [ -f "$RUN_PATH/$PROJECT_NAME.jar" ]; then
cp $RUN_PATH/$PROJECT_NAME.jar $BACKUP_DIR/${PROJECT_NAME}_$(date +%Y%m%d%H%M%S).jar
fi
# 删除旧的jar包
echo 'Delete old jar'
rm -rf $RUN_PATH/$PROJECT_NAME
# 拷贝新的Jar包到指定位置
cp $JAR_FILE $RUN_PATH/
echo "Starting the application..."
# 启动应用程序
nohup java -jar $RUN_PATH/$PROJECT_NAME.jar --httpPort=80 > $RUN_PATH/logs/log.txt 2>&1 &
echo "Application started successfully."
脚本解析:
PROJECT_NAME:定义项目名称
JAR_FILE:打包好的文件路径
RUN_PATH:要发布在哪里
其他几乎不需要更改,看个人情况调整吧